diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index ffdd95f72..efd058b8e 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -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 ⛑️ diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index b1bcb709c..2d4876a23 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -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 diff --git a/fmt.go b/fmt.go index 58a4f671b..3bf93731d 100644 --- a/fmt.go +++ b/fmt.go @@ -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:]) 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] - input, err := ms.ReadPath(inputPath) - if err != nil { - return err - } + for _, inputPath := range ms.Opts.Args { + input, err := ms.ReadPath(inputPath) + if err != nil { + return err + } - m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil) - if err != nil { - return err - } + m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil) + if err != nil { + return err + } - output := []byte(d2format.Format(m)) - if !bytes.Equal(output, input) { - return ms.WritePath(inputPath, output) + output := []byte(d2format.Format(m)) + if !bytes.Equal(output, input) { + return ms.WritePath(inputPath, output) + } } - return nil } diff --git a/help.go b/help.go index 3efa76b02..7a5446047 100644 --- a/help.go +++ b/help.go @@ -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())