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

Merged
merged 6 commits into from
Feb 4, 2025
Merged
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
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- CLI: PNG output to stdout is supported using `--stdout-format png -` [#2291](https://github.com/terrastruct/d2/pull/2291)
- Globs: `&connected` and `&leaf` filters are implemented [#2299](https://github.com/terrastruct/d2/pull/2299)
- CLI: add --no-xml-tag for direct HTML embedding [#2302](https://github.com/terrastruct/d2/pull/2302)
- CLI: `play` cmd added for opening d2 input in online playground [#2242](https://github.com/terrastruct/d2/pull/2242)

#### Improvements 🧹

Expand Down
11 changes: 7 additions & 4 deletions ci/release/template/man/d2.1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
.Ar layout Op Ar name
.Nm d2
.Ar fmt Ar file.d2 ...
.Nm d2
.Ar play Ar file.d2
.Sh DESCRIPTION
.Nm
compiles and renders
Expand Down Expand Up @@ -141,6 +143,9 @@ Print version information and exit
.It Fl -stdout-format Ar string
Set the output format when writing to stdout. Supported formats are: png, svg. Only used when output is set to stdout (-)
.Ns .
.It Fl -no-xml-tag Ar false
Omit XML tag (<?xml ...?>) from output SVG files. Useful when generating SVGs for direct HTML embedding
.Ns .
.El
.Sh SUBCOMMANDS
.Bl -tag -width Fl
Expand All @@ -155,10 +160,8 @@ Lists available themes
.Ns .
.It Ar fmt Ar file.d2 ...
Format all passed files
.It Fl -no-xml-tag Ar false
Omit XML tag (<?xml ...?>) from output SVG files. Useful when generating SVGs for direct HTML embedding
.Ns .
.Ns .
.It Ar play Ar file.d2
Opens the file in playground, an online web viewer (https://play.d2lang.com)
.El
.Sh ENVIRONMENT VARIABLES
Many flags can also be set with environment variables.
Expand Down
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, an online web viewer (https://play.d2lang.com)

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 @@ -171,6 +171,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return nil
case "fmt":
return fmtCmd(ctx, ms, *checkFlag)
case "play":
return playCmd(ctx, ms)
case "version":
if len(ms.Opts.Flags.Args()) > 1 {
return xmain.UsageErrorf("version subcommand accepts no arguments")
Expand Down
75 changes: 75 additions & 0 deletions d2cli/play.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package d2cli

import (
"context"
"fmt"
"io"
"os"

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

func playCmd(ctx context.Context, ms *xmain.State) error {
if len(ms.Opts.Flags.Args()) != 2 {
return xmain.UsageErrorf("play must be passed one argument: either a filepath or '-' for 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 := readInput(filepath)
if err != nil {
return err
}

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

url := fmt.Sprintf("https://play.d2lang.com/?script=%s&sketch=%d&theme=%d&", encoded, sketchNumber, theme)
openBrowser(ctx, ms, url)
alixander marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func readInput(filepath string) (string, error) {
if filepath == "-" {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("error reading from stdin: %w", err)
}
return string(data), nil
}

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)
}
}