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)
- 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 🧹
#### Bugfixes ⛑️

View file

@ -13,7 +13,7 @@
.Nm d2
.Ar layout Op Ar name
.Nm d2
.Ar fmt Ar file.d2
.Ar fmt Ar file.d2 ...
.Sh DESCRIPTION
.Nm
compiles and renders
@ -83,10 +83,8 @@ 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, including its configuration options.
.It Ar fmt Ar file.d2
Format
.Ar file.d2
.Ns .
.It Ar fmt Ar file.d2 ...
Format all passed files.
.El
.Sh SEE ALSO
.Xr d2plugin-tala 1

8
fmt.go
View file

@ -17,12 +17,10 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {
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 accepts only one argument for the file to be formatted")
return xmain.UsageErrorf("fmt must be passed at least one file to be formatted")
}
inputPath := ms.Opts.Args[0]
for _, inputPath := range ms.Opts.Args {
input, err := ms.ReadPath(inputPath)
if err != nil {
return err
@ -37,6 +35,6 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {
if !bytes.Equal(output, input) {
return ms.WritePath(inputPath, output)
}
}
return nil
}

View file

@ -18,7 +18,7 @@ func help(ms *xmain.State) {
fmt.Fprintf(ms.Stdout, `Usage:
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png]
%[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
It defaults to file.svg if an output path is not provided.
@ -33,7 +33,7 @@ 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, 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
`, filepath.Base(ms.Name), ms.Opts.Defaults())