Skip to content

Commit

Permalink
cmd/cue: allow "fmt" to show unformatted files
Browse files Browse the repository at this point in the history
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
  • Loading branch information
NoamTD committed Mar 30, 2024
1 parent 2887786 commit 5096bfb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
42 changes: 41 additions & 1 deletion cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
package cmd

import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"os"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
Expand Down Expand Up @@ -56,6 +59,9 @@ func newFmtCmd(c *Command) *cobra.Command {
cfg.Format = opts
cfg.Force = true

check := flagCheck.Bool(cmd)
var modifiedFiles []string

for _, inst := range builds {
if inst.Err != nil {
switch {
Expand All @@ -67,7 +73,7 @@ func newFmtCmd(c *Command) *cobra.Command {
}
}
for _, file := range inst.BuildFiles {
files := []*ast.File{}
var files []*ast.File
d := encoding.NewDecoder(file, &cfg)
for ; !d.Done(); d.Next() {
f := d.File()
Expand All @@ -86,20 +92,54 @@ func newFmtCmd(c *Command) *cobra.Command {
exitOnErr(cmd, err, true)
}

var formatted bytes.Buffer
if check {
cfg.Out = &formatted
}

e, err := encoding.NewEncoder(file, &cfg)
exitOnErr(cmd, err, true)

for _, f := range files {
err := e.EncodeFile(f)
exitOnErr(cmd, err, false)
}

if check {
var originalBytes []byte
if file.Filename != "-" {
originalBytes, err = os.ReadFile(file.Filename)
exitOnErr(cmd, err, true)
} else {
originalBytes = file.Source.([]byte)
}

if !bytes.Equal(formatted.Bytes(), originalBytes) {
modifiedFiles = append(modifiedFiles, file.Filename)
}
}

if err := e.Close(); err != nil {
exitOnErr(cmd, err, true)
}
}
}

if check && len(modifiedFiles) > 0 {
stdout := cmd.OutOrStdout()
for _, f := range modifiedFiles {
if f != "-" {
fmt.Fprintln(stdout, f)
}
}
os.Exit(1)
}

return nil
}),
}

cmd.Flags().Bool(string(flagCheck), false, "exits with non-zero status if any files are not formatted")

return cmd
}
15 changes: 15 additions & 0 deletions cmd/cue/cmd/testdata/script/fmt_check.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# succeeds when file is formatted
exec cue fmt --check formatted.cue

# files are not modified
exec stat -f %c formatted.cue
cp stdout formatted_time_before
exec cue fmt formatted.cue
exec stat -f %c formatted.cue
cmp stdout formatted_time_before

stdin formatted.cue
exec cue fmt --check -

-- formatted.cue --
foo: "bar"
20 changes: 20 additions & 0 deletions cmd/cue/cmd/testdata/script/fmt_check_fail.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# fails and displays non formatted files
! exec cue fmt --check not_formatted.cue another_not_formatted.cue
cp stdout actual-stdout
grep not_formatted.cue actual-stdout
grep another_not_formatted.cue actual-stdout
! grep correct.cue actual-stdout

# stdin fails with no output
stdin not_formatted.cue
! exec cue fmt --check -
cmp stdout expect-empty-stdout

-- correct.cue --
foo: "bar"
-- not_formatted.cue --
foo: "bar"
-- another_not_formatted.cue --
bar: "baz"
x: 1
-- expect-empty-stdout --
2 changes: 1 addition & 1 deletion cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ func (d *EmbedDecl) End() token.Pos { return d.Expr.End() }
// ----------------------------------------------------------------------------
// Files and packages

// A File node represents a Go source file.
// A File node represents a Cue source file.
//
// The Comments list contains all comments in the source file in order of
// appearance, including the comments that are pointed to from other nodes
Expand Down

0 comments on commit 5096bfb

Please sign in to comment.