Skip to content

Commit

Permalink
cmd/fmt: Always write to stdout
Browse files Browse the repository at this point in the history
There was an explicit check that would prevent writing to stdout if
the formatting didn't change anything.

This makes sense for the "write to file" or "diff" options as we can
save work by bailing out early, but if configured to write the file
contents to stdout we should do so in all cases.

Fixes: #2235
Signed-off-by: Patrick East <east.patrick@gmail.com>
  • Loading branch information
patrick-east authored and tsandall committed Apr 2, 2020
1 parent aee2bd9 commit fd79a7e
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ func formatFile(filename string, info os.FileInfo, err error) error {
return newError("failed to parse Rego source file: %v", err)
}

if bytes.Equal(formatted, contents) {
return nil
}

var out io.Writer = os.Stdout
if fmtParams.list {
fmt.Fprintln(out, filename)
Expand Down Expand Up @@ -158,15 +154,15 @@ func formatStdin(r io.Reader, w io.Writer) error {
return err
}

if !bytes.Equal(formatted, contents) {
_, err := w.Write(formatted)
return err
}

return nil
_, err = w.Write(formatted)
return err
}

func doDiff(old, new []byte) (stdout, stderr bytes.Buffer, err error) {
if bytes.Equal(old, new) {
return stdout, stderr, nil
}

o, err := ioutil.TempFile("", ".opafmt")
if err != nil {
return stdout, stderr, err
Expand Down

0 comments on commit fd79a7e

Please sign in to comment.