Skip to content

Commit

Permalink
fmt: Accept multiple files to be formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jan 27, 2023
1 parent afd2768 commit 01dafcf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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 ⛑️
34 changes: 16 additions & 18 deletions fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

output := []byte(d2format.Format(m))
if !bytes.Equal(output, input) {
return ms.WritePath(inputPath, output)
}
}

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

return nil
}

0 comments on commit 01dafcf

Please sign in to comment.