d2format: Expose in CLI under fmt subcommand

Closes #289
This commit is contained in:
Anmol Sethi 2022-12-01 02:48:30 -08:00
parent 2df2c781e9
commit fcb41107db
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
5 changed files with 60 additions and 12 deletions

36
autofmt.go Normal file
View file

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

View file

@ -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

View file

@ -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

View file

@ -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 {

19
main.go
View file

@ -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 {