From 80b9d1b057443124d2bb622891fd7d6db91e6f84 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 02:47:18 -0800 Subject: [PATCH 1/5] ci/dev.sh: Add --- .gitignore | 1 + ci/dev.sh | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100755 ci/dev.sh diff --git a/.gitignore b/.gitignore index 67d08864e..7af7b3572 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.got.json *.got.svg e2e_report.html +bin diff --git a/ci/dev.sh b/ci/dev.sh new file mode 100755 index 000000000..cbc71bf79 --- /dev/null +++ b/ci/dev.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +cd -- "$(dirname "$0")/.." +. ./ci/sub/lib.sh + +sh_c go build --tags=dev -o=bin/d2 . +sh_c ./bin/d2 "$@" From 2df2c781e91d60e95d80a5eb8c4b72c3fb0745a2 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 02:47:41 -0800 Subject: [PATCH 2/5] lib/xmain: Print usage info in separate log --- lib/xmain/xmain.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/xmain/xmain.go b/lib/xmain/xmain.go index 80de10137..b0a1aeb3a 100644 --- a/lib/xmain/xmain.go +++ b/lib/xmain/xmain.go @@ -66,10 +66,10 @@ func Main(run RunFunc) { } if msg != "" { - if usage { - msg = fmt.Sprintf("%s\n%s", msg, "Run with --help to see usage.") - } ms.Log.Error.Print(msg) + if usage { + ms.Log.Error.Print("Run with --help to see usage.") + } } os.Exit(code) } From fcb41107db884f7e085e8af89e2a410e0e1bdb04 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 02:48:30 -0800 Subject: [PATCH 3/5] d2format: Expose in CLI under fmt subcommand Closes #289 --- autofmt.go | 36 +++++++++++++++++++++++++++++++++++ ci/release/changelogs/next.md | 2 ++ ci/release/template/man/d2.1 | 6 ++++++ help.go | 9 ++++++--- main.go | 19 +++++++++--------- 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 autofmt.go diff --git a/autofmt.go b/autofmt.go new file mode 100644 index 000000000..749c07981 --- /dev/null +++ b/autofmt.go @@ -0,0 +1,36 @@ +package main + +import ( + "bytes" + "context" + + "oss.terrastruct.com/xdefer" + + "oss.terrastruct.com/d2/d2format" + "oss.terrastruct.com/d2/d2parser" + "oss.terrastruct.com/d2/lib/xmain" +) + +func autofmt(ctx context.Context, ms *xmain.State) (err error) { + defer xdefer.Errorf(&err, "autofmt failed") + + ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:]) + if len(ms.Opts.Args) == 0 { + return xmain.UsageErrorf("fmt must be passed the file to be formatted") + } else if len(ms.Opts.Args) > 1 { + return xmain.UsageErrorf("fmt only accepts one argument for the file to be formatted") + } + + inputPath := ms.Opts.Args[0] + input, err := ms.ReadPath(inputPath) + if err != nil { + return err + } + + m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil) + if err != nil { + return err + } + + return ms.WritePath(inputPath, []byte(d2format.Format(m))) +} diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 87fc8f796..10eb2455f 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,5 +1,7 @@ #### Features 🚀 +- autoformat is supported on the CLI with the `fmt` subcommand. + [#292](https://github.com/terrastruct/d2/pull/292) - Latex is now supported. See [docs](https://d2lang.com/tour/text) for more. [#229](https://github.com/terrastruct/d2/pull/229) - `direction` keyword is now supported to specify `up`, `down`, `right`, `left` layouts. See diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index d46ca20bf..8347bd8fe 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -12,6 +12,8 @@ .Op Ar file.svg | file.png .Nm d2 .Ar layout Op Ar name +.Nm d2 +.Ar fmt Ar file.d2 .Sh DESCRIPTION .Nm compiles and renders @@ -75,6 +77,10 @@ Print version information and exit. Lists available layout engine options with short help. .It Ar layout Op Ar name Display long help for a particular layout engine. +.It Ar fmt Ar file.d2 +Autoformat +.Ar file.d2 +.Ns . .El .Sh SEE ALSO .Xr d2plugin-tala 1 diff --git a/help.go b/help.go index 6c1cec897..aff1a7219 100644 --- a/help.go +++ b/help.go @@ -15,7 +15,9 @@ import ( func help(ms *xmain.State) { fmt.Fprintf(ms.Stdout, `Usage: - %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 fmt 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. @@ -29,10 +31,11 @@ Flags: Subcommands: %[1]s layout - Lists available layout engine options with short help - %[1]s layout [layout name] - Display long help for a particular layout engine + %[1]s layout [name] - Display long help for a particular layout engine + %[1]s fmt file.d2 - Autoformat file.d2 See more docs and the source code at https://oss.terrastruct.com/d2 -`, ms.Name, ms.Opts.Defaults()) +`, filepath.Base(ms.Name), ms.Opts.Defaults()) } func layoutHelp(ctx context.Context, ms *xmain.State) error { diff --git a/main.go b/main.go index 81f8aaab0..b30f539e1 100644 --- a/main.go +++ b/main.go @@ -65,18 +65,23 @@ func run(ctx context.Context, ms *xmain.State) (err error) { return xmain.UsageErrorf("failed to parse flags: %v", err) } + if errors.Is(err, pflag.ErrHelp) { + help(ms) + return nil + } + if len(ms.Opts.Flags.Args()) > 0 { switch ms.Opts.Flags.Arg(0) { case "layout": return layoutHelp(ctx, ms) + case "fmt": + return autofmt(ctx, ms) + case "version": + fmt.Println(version.Version) + return nil } } - if errors.Is(err, pflag.ErrHelp) { - help(ms) - return nil - } - if *debugFlag { ms.Env.Setenv("DEBUG", "1") } @@ -96,10 +101,6 @@ func run(ctx context.Context, ms *xmain.State) (err error) { } if len(ms.Opts.Flags.Args()) >= 1 { - if ms.Opts.Flags.Arg(0) == "version" { - fmt.Println(version.Version) - return nil - } inputPath = ms.Opts.Flags.Arg(0) } if len(ms.Opts.Flags.Args()) >= 2 { From f7623a205758a8c3905adab1b612fc519d38c083 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 02:57:57 -0800 Subject: [PATCH 4/5] main: Error on arguments to version subcmd --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index b30f539e1..34af23f59 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,9 @@ func run(ctx context.Context, ms *xmain.State) (err error) { case "fmt": return autofmt(ctx, ms) case "version": + if len(ms.Opts.Flags.Args()) > 1 { + return xmain.UsageErrorf("version subcommand accepts no arguments") + } fmt.Println(version.Version) return nil } From cdffedb9df333e2518379ea99ed8790c6bd87103 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 09:21:16 -0800 Subject: [PATCH 5/5] pr292: Review fixes --- ci/release/changelogs/next.md | 2 +- ci/release/template/man/d2.1 | 2 +- ci/sub | 2 +- autofmt.go => fmt.go | 6 +++--- help.go | 4 ++-- main.go | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) rename autofmt.go => fmt.go (79%) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 10eb2455f..2a3b66356 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,6 +1,6 @@ #### Features 🚀 -- autoformat is supported on the CLI with the `fmt` subcommand. +- Formatting of d2 scripts is supported on the CLI with the `fmt` subcommand. [#292](https://github.com/terrastruct/d2/pull/292) - Latex is now supported. See [docs](https://d2lang.com/tour/text) for more. [#229](https://github.com/terrastruct/d2/pull/229) diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index 8347bd8fe..275eca9da 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -78,7 +78,7 @@ Lists available layout engine options with short help. .It Ar layout Op Ar name Display long help for a particular layout engine. .It Ar fmt Ar file.d2 -Autoformat +Format .Ar file.d2 .Ns . .El diff --git a/ci/sub b/ci/sub index 702b77122..a39a67857 160000 --- a/ci/sub +++ b/ci/sub @@ -1 +1 @@ -Subproject commit 702b771228f41e5485b5f3f593ce79f6efcb555d +Subproject commit a39a678570a0454d6bf63f9012fb5ec107e24df5 diff --git a/autofmt.go b/fmt.go similarity index 79% rename from autofmt.go rename to fmt.go index 749c07981..49fd5677b 100644 --- a/autofmt.go +++ b/fmt.go @@ -11,14 +11,14 @@ import ( "oss.terrastruct.com/d2/lib/xmain" ) -func autofmt(ctx context.Context, ms *xmain.State) (err error) { - defer xdefer.Errorf(&err, "autofmt failed") +func fmtCmd(ctx context.Context, ms *xmain.State) (err error) { + defer xdefer.Errorf(&err, "failed to fmt") ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:]) if len(ms.Opts.Args) == 0 { return xmain.UsageErrorf("fmt must be passed the file to be formatted") } else if len(ms.Opts.Args) > 1 { - return xmain.UsageErrorf("fmt only accepts one argument for the file to be formatted") + return xmain.UsageErrorf("fmt accepts only one argument for the file to be formatted") } inputPath := ms.Opts.Args[0] diff --git a/help.go b/help.go index aff1a7219..6f2f93637 100644 --- a/help.go +++ b/help.go @@ -32,13 +32,13 @@ Flags: Subcommands: %[1]s layout - Lists available layout engine options with short help %[1]s layout [name] - Display long help for a particular layout engine - %[1]s fmt file.d2 - Autoformat file.d2 + %[1]s fmt file.d2 - Format file.d2 See more docs and the source code at https://oss.terrastruct.com/d2 `, filepath.Base(ms.Name), ms.Opts.Defaults()) } -func layoutHelp(ctx context.Context, ms *xmain.State) error { +func layoutCmd(ctx context.Context, ms *xmain.State) error { if len(ms.Opts.Flags.Args()) == 1 { return shortLayoutHelp(ctx, ms) } else if len(ms.Opts.Flags.Args()) == 2 { diff --git a/main.go b/main.go index 34af23f59..29ace7b16 100644 --- a/main.go +++ b/main.go @@ -73,9 +73,9 @@ func run(ctx context.Context, ms *xmain.State) (err error) { if len(ms.Opts.Flags.Args()) > 0 { switch ms.Opts.Flags.Arg(0) { case "layout": - return layoutHelp(ctx, ms) + return layoutCmd(ctx, ms) case "fmt": - return autofmt(ctx, ms) + return fmtCmd(ctx, ms) case "version": if len(ms.Opts.Flags.Args()) > 1 { return xmain.UsageErrorf("version subcommand accepts no arguments")