From 569cb0b8aa5b65fa137fe2d2cbecdf8e164961f4 Mon Sep 17 00:00:00 2001 From: turkmenkaan Date: Tue, 10 Dec 2024 20:00:48 -0500 Subject: [PATCH 1/2] add play cmd --- d2cli/help.go | 1 + d2cli/main.go | 2 ++ d2cli/play.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 d2cli/play.go diff --git a/d2cli/help.go b/d2cli/help.go index a786cd7b4e..8338b3af33 100644 --- a/d2cli/help.go +++ b/d2cli/help.go @@ -38,6 +38,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 See more docs and the source code at https://oss.terrastruct.com/d2. Hosted icons at https://icons.terrastruct.com. diff --git a/d2cli/main.go b/d2cli/main.go index eeefd6ae91..7823db6b84 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -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) case "version": if len(ms.Opts.Flags.Args()) > 1 { return xmain.UsageErrorf("version subcommand accepts no arguments") diff --git a/d2cli/play.go b/d2cli/play.go new file mode 100644 index 0000000000..9bbd6cf251 --- /dev/null +++ b/d2cli/play.go @@ -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") + } + 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) + openBrowser(ctx, ms, url) + 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) + } +} From 7c2b8221ba24caee29812e9ea79a96cb97b80edd Mon Sep 17 00:00:00 2001 From: turkmenkaan Date: Tue, 10 Dec 2024 20:04:27 -0500 Subject: [PATCH 2/2] add more help info --- d2cli/help.go | 1 + 1 file changed, 1 insertion(+) diff --git a/d2cli/help.go b/d2cli/help.go index 8338b3af33..010118c222 100644 --- a/d2cli/help.go +++ b/d2cli/help.go @@ -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.