Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add play cmd #2242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions d2cli/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage:
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png]
%[1]s layout [name]
%[1]s fmt file.d2 ...
%[1]s play [--theme=0] [--sketch] file.d2

%[1]s compiles and renders file.d2 to file.svg | file.png
It defaults to file.svg if an output path is not provided.
Expand All @@ -38,6 +39,7 @@ Subcommands:
%[1]s layout [name] - Display long help for a particular layout engine, including its configuration options
%[1]s themes - Lists available themes
%[1]s fmt file.d2 ... - Format passed files
%[1]s play file.d2 - Opens the file in playground
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

playground might be unknown to folks. I'd add a description like Opens the file in the d2 playground, an online web viewer


See more docs and the source code at https://oss.terrastruct.com/d2.
Hosted icons at https://icons.terrastruct.com.
Expand Down
2 changes: 2 additions & 0 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return nil
case "fmt":
return fmtCmd(ctx, ms)
case "play":
return playSubcommand(ctx, ms)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

playCmd to conform with above naming standards

case "version":
if len(ms.Opts.Flags.Args()) > 1 {
return xmain.UsageErrorf("version subcommand accepts no arguments")
Expand Down
67 changes: 67 additions & 0 deletions d2cli/play.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package d2cli

import (
"context"
"fmt"
"os"

"oss.terrastruct.com/d2/lib/urlenc"
"oss.terrastruct.com/util-go/xbrowser"
"oss.terrastruct.com/util-go/xmain"
)

func playSubcommand(ctx context.Context, ms *xmain.State) error {
if len(ms.Opts.Flags.Args()) != 2 {
return xmain.UsageErrorf("play must be passed one file to open")
}
Comment on lines +14 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should also allow stdin

filepath := ms.Opts.Flags.Args()[1]

theme, err := ms.Opts.Flags.GetInt64("theme")
if err != nil {
return err
}

sketch, err := ms.Opts.Flags.GetBool("sketch")
if err != nil {
return err
}

var sketchNumber int
if sketch {
sketchNumber = 1
} else {
sketchNumber = 0
}

fileRaw, err := readFile(filepath)
if err != nil {
return err
}

encoded, err := urlenc.Encode(fileRaw)
if err != nil {
return err
}

url := fmt.Sprintf("https://play.d2lang.com/?l=&script=%s&sketch=%d&theme=%d&", encoded, sketchNumber, theme)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the l=?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in the URL provided in the issue. I'll remove.

openBrowser(ctx, ms, url)
alixander marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func readFile(filepath string) (string, error) {
data, err := os.ReadFile(filepath)
if err != nil {
return "", xmain.UsageErrorf(err.Error())
}

return string(data), nil
}

func openBrowser(ctx context.Context, ms *xmain.State, url string) {
ms.Log.Info.Printf("opening playground: %s", url)

err := xbrowser.Open(ctx, ms.Env, url)
if err != nil {
ms.Log.Warn.Printf("failed to open browser to %v: %v", url, err)
}
}
Loading