Merge pull request #2242 from turkmenkaan/f/add-play-cmd
cli: add play cmd
This commit is contained in:
commit
5f95c54ca5
5 changed files with 87 additions and 4 deletions
|
|
@ -9,6 +9,7 @@
|
||||||
- CLI: PNG output to stdout is supported using `--stdout-format png -` [#2291](https://github.com/terrastruct/d2/pull/2291)
|
- 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)
|
- 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: 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 🧹
|
#### Improvements 🧹
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@
|
||||||
.Ar layout Op Ar name
|
.Ar layout Op Ar name
|
||||||
.Nm d2
|
.Nm d2
|
||||||
.Ar fmt Ar file.d2 ...
|
.Ar fmt Ar file.d2 ...
|
||||||
|
.Nm d2
|
||||||
|
.Ar play Ar file.d2
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
.Nm
|
.Nm
|
||||||
compiles and renders
|
compiles and renders
|
||||||
|
|
@ -141,6 +143,9 @@ Print version information and exit
|
||||||
.It Fl -stdout-format Ar string
|
.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 (-)
|
Set the output format when writing to stdout. Supported formats are: png, svg. Only used when output is set to stdout (-)
|
||||||
.Ns .
|
.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
|
.El
|
||||||
.Sh SUBCOMMANDS
|
.Sh SUBCOMMANDS
|
||||||
.Bl -tag -width Fl
|
.Bl -tag -width Fl
|
||||||
|
|
@ -155,10 +160,8 @@ Lists available themes
|
||||||
.Ns .
|
.Ns .
|
||||||
.It Ar fmt Ar file.d2 ...
|
.It Ar fmt Ar file.d2 ...
|
||||||
Format all passed files
|
Format all passed files
|
||||||
.It Fl -no-xml-tag Ar false
|
.It Ar play Ar file.d2
|
||||||
Omit XML tag (<?xml ...?>) from output SVG files. Useful when generating SVGs for direct HTML embedding
|
Opens the file in playground, an online web viewer (https://play.d2lang.com)
|
||||||
.Ns .
|
|
||||||
.Ns .
|
|
||||||
.El
|
.El
|
||||||
.Sh ENVIRONMENT VARIABLES
|
.Sh ENVIRONMENT VARIABLES
|
||||||
Many flags can also be set with environment variables.
|
Many flags can also be set with environment variables.
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ Usage:
|
||||||
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png]
|
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png]
|
||||||
%[1]s layout [name]
|
%[1]s layout [name]
|
||||||
%[1]s fmt file.d2 ...
|
%[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
|
%[1]s compiles and renders file.d2 to file.svg | file.png
|
||||||
It defaults to file.svg if an output path is not provided.
|
It defaults to file.svg if an output path is not provided.
|
||||||
|
|
@ -38,6 +39,7 @@ Subcommands:
|
||||||
%[1]s layout [name] - Display long help for a particular layout engine, including its configuration options
|
%[1]s layout [name] - Display long help for a particular layout engine, including its configuration options
|
||||||
%[1]s themes - Lists available themes
|
%[1]s themes - Lists available themes
|
||||||
%[1]s fmt file.d2 ... - Format passed files
|
%[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.
|
See more docs and the source code at https://oss.terrastruct.com/d2.
|
||||||
Hosted icons at https://icons.terrastruct.com.
|
Hosted icons at https://icons.terrastruct.com.
|
||||||
|
|
|
||||||
|
|
@ -171,6 +171,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
return nil
|
return nil
|
||||||
case "fmt":
|
case "fmt":
|
||||||
return fmtCmd(ctx, ms, *checkFlag)
|
return fmtCmd(ctx, ms, *checkFlag)
|
||||||
|
case "play":
|
||||||
|
return playCmd(ctx, ms)
|
||||||
case "version":
|
case "version":
|
||||||
if len(ms.Opts.Flags.Args()) > 1 {
|
if len(ms.Opts.Flags.Args()) > 1 {
|
||||||
return xmain.UsageErrorf("version subcommand accepts no arguments")
|
return xmain.UsageErrorf("version subcommand accepts no arguments")
|
||||||
|
|
|
||||||
75
d2cli/play.go
Normal file
75
d2cli/play.go
Normal file
|
|
@ -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)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue