Skip to content

Commit 3b0fe86

Browse files
committed
cmd/cue: add a fmt --check flag to list badly formatted files
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. fixes #363. Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9 Signed-off-by: Noam Dolovich <[email protected]>
1 parent 2887786 commit 3b0fe86

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

cmd/cue/cmd/fmt.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
package cmd
1616

1717
import (
18+
"bytes"
19+
"cuelang.org/go/internal/source"
20+
"fmt"
1821
"github.com/spf13/cobra"
22+
"os"
1923

2024
"cuelang.org/go/cue/ast"
2125
"cuelang.org/go/cue/build"
@@ -56,6 +60,9 @@ func newFmtCmd(c *Command) *cobra.Command {
5660
cfg.Format = opts
5761
cfg.Force = true
5862

63+
check := flagCheck.Bool(cmd)
64+
var badlyFormattedFiles []string
65+
5966
for _, inst := range builds {
6067
if inst.Err != nil {
6168
switch {
@@ -67,7 +74,21 @@ func newFmtCmd(c *Command) *cobra.Command {
6774
}
6875
}
6976
for _, file := range inst.BuildFiles {
70-
files := []*ast.File{}
77+
// When using --check, we need to buffer the input and output bytes to compare them.
78+
var original []byte
79+
var formatted bytes.Buffer
80+
if check {
81+
if bs, ok := file.Source.([]byte); ok {
82+
original = bs
83+
} else {
84+
original, err = source.ReadAll(file.Filename, file.Source)
85+
exitOnErr(cmd, err, true)
86+
file.Source = original
87+
}
88+
cfg.Out = &formatted
89+
}
90+
91+
var files []*ast.File
7192
d := encoding.NewDecoder(file, &cfg)
7293
for ; !d.Done(); d.Next() {
7394
f := d.File()
@@ -93,13 +114,32 @@ func newFmtCmd(c *Command) *cobra.Command {
93114
err := e.EncodeFile(f)
94115
exitOnErr(cmd, err, false)
95116
}
117+
96118
if err := e.Close(); err != nil {
97119
exitOnErr(cmd, err, true)
98120
}
121+
122+
if check && !bytes.Equal(formatted.Bytes(), original) {
123+
badlyFormattedFiles = append(badlyFormattedFiles, file.Filename)
124+
}
99125
}
100126
}
127+
128+
if check && len(badlyFormattedFiles) > 0 {
129+
stdout := cmd.OutOrStdout()
130+
for _, f := range badlyFormattedFiles {
131+
if f != "-" {
132+
fmt.Fprintln(stdout, f)
133+
}
134+
}
135+
os.Exit(1)
136+
}
137+
101138
return nil
102139
}),
103140
}
141+
142+
cmd.Flags().Bool(string(flagCheck), false, "exits with non-zero status if any files are not formatted")
143+
104144
return cmd
105145
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# succeeds when file is formatted
2+
exec cue fmt --check formatted.cue
3+
4+
stdin formatted.cue
5+
exec cue fmt --check -
6+
7+
# fails and displays non formatted files
8+
! exec cue fmt --check not_formatted.cue another_not_formatted.cue
9+
cmpenv stdout expected-output
10+
11+
# files are not modified with --check
12+
# running twice returns the same file list
13+
! exec cue fmt --check not_formatted.cue another_not_formatted.cue
14+
cmpenv stdout expected-output
15+
16+
# stdin fails with no output
17+
stdin not_formatted.cue
18+
! exec cue fmt --check -
19+
! stdout .
20+
21+
-- formatted.cue --
22+
foo: "bar"
23+
-- not_formatted.cue --
24+
foo: "bar"
25+
-- another_not_formatted.cue --
26+
bar: "baz"
27+
x: 1
28+
-- expected-output --
29+
$WORK${/}another_not_formatted.cue
30+
$WORK${/}not_formatted.cue

cue/ast/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ func (d *EmbedDecl) End() token.Pos { return d.Expr.End() }
951951
// ----------------------------------------------------------------------------
952952
// Files and packages
953953

954-
// A File node represents a Go source file.
954+
// A File node represents a CUE source file.
955955
//
956956
// The Comments list contains all comments in the source file in order of
957957
// appearance, including the comments that are pointed to from other nodes

0 commit comments

Comments
 (0)