fmt: Accept multiple files to be formatted

Closes #718
This commit is contained in:
Anmol Sethi 2023-01-27 06:48:01 -08:00
parent 12ee7ae278
commit 0507946672
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
4 changed files with 21 additions and 23 deletions

View file

@ -4,6 +4,8 @@
- The [Dockerfile](./docs/INSTALL.md#docker) now supports rendering PNGs [#594](https://github.com/terrastruct/d2/issues/594) - The [Dockerfile](./docs/INSTALL.md#docker) now supports rendering PNGs [#594](https://github.com/terrastruct/d2/issues/594)
- There was a minor breaking change as part of this where the default working directory of the Dockerfile is now `/home/debian/src` instead of `/root/src` to allow UID remapping with [`fixuid`](https://github.com/boxboat/fixuid). - There was a minor breaking change as part of this where the default working directory of the Dockerfile is now `/home/debian/src` instead of `/root/src` to allow UID remapping with [`fixuid`](https://github.com/boxboat/fixuid).
- `d2 fmt` accepts multiple files to be formatted [#718](https://github.com/terrastruct/d2/issues/718)
#### Improvements 🧹 #### Improvements 🧹
#### Bugfixes ⛑️ #### Bugfixes ⛑️

View file

@ -13,7 +13,7 @@
.Nm d2 .Nm d2
.Ar layout Op Ar name .Ar layout Op Ar name
.Nm d2 .Nm d2
.Ar fmt Ar file.d2 .Ar fmt Ar file.d2 ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
compiles and renders compiles and renders
@ -83,10 +83,8 @@ Print version information and exit.
Lists available layout engine options with short help. Lists available layout engine options with short help.
.It Ar layout Op Ar name .It Ar layout Op Ar name
Display long help for a particular layout engine, including its configuration options. Display long help for a particular layout engine, including its configuration options.
.It Ar fmt Ar file.d2 .It Ar fmt Ar file.d2 ...
Format Format all passed files.
.Ar file.d2
.Ns .
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr d2plugin-tala 1 .Xr d2plugin-tala 1

30
fmt.go
View file

@ -17,26 +17,24 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {
ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:]) ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:])
if len(ms.Opts.Args) == 0 { if len(ms.Opts.Args) == 0 {
return xmain.UsageErrorf("fmt must be passed the file to be formatted") return xmain.UsageErrorf("fmt must be passed at least one file to be formatted")
} else if len(ms.Opts.Args) > 1 {
return xmain.UsageErrorf("fmt accepts only one argument for the file to be formatted")
} }
inputPath := ms.Opts.Args[0] for _, inputPath := range ms.Opts.Args {
input, err := ms.ReadPath(inputPath) input, err := ms.ReadPath(inputPath)
if err != nil { if err != nil {
return err return err
} }
m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil) m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil)
if err != nil { if err != nil {
return err return err
} }
output := []byte(d2format.Format(m)) output := []byte(d2format.Format(m))
if !bytes.Equal(output, input) { if !bytes.Equal(output, input) {
return ms.WritePath(inputPath, output) return ms.WritePath(inputPath, output)
}
} }
return nil return nil
} }

View file

@ -18,7 +18,7 @@ func help(ms *xmain.State) {
fmt.Fprintf(ms.Stdout, `Usage: fmt.Fprintf(ms.Stdout, `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 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.
@ -33,7 +33,7 @@ Flags:
Subcommands: Subcommands:
%[1]s layout - Lists available layout engine options with short help %[1]s layout - Lists available layout engine options with short help
%[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 fmt file.d2 - Format file.d2 %[1]s fmt file.d2 ... - Format passed files
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
`, filepath.Base(ms.Name), ms.Opts.Defaults()) `, filepath.Base(ms.Name), ms.Opts.Defaults())