Skip to content

Commit 0312bf7

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 0312bf7

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

cmd/cue/cmd/fmt.go

Lines changed: 40 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 modifiedFiles []string
65+
5966
for _, inst := range builds {
6067
if inst.Err != nil {
6168
switch {
@@ -67,7 +74,20 @@ func newFmtCmd(c *Command) *cobra.Command {
6774
}
6875
}
6976
for _, file := range inst.BuildFiles {
70-
files := []*ast.File{}
77+
var original []byte
78+
var formatted bytes.Buffer
79+
if check {
80+
if bs, ok := file.Source.([]byte); ok {
81+
original = bs
82+
} else {
83+
original, err = source.ReadAll(file.Filename, file.Source)
84+
exitOnErr(cmd, err, true)
85+
file.Source = original
86+
}
87+
cfg.Out = &formatted
88+
}
89+
90+
var files []*ast.File
7191
d := encoding.NewDecoder(file, &cfg)
7292
for ; !d.Done(); d.Next() {
7393
f := d.File()
@@ -93,13 +113,32 @@ func newFmtCmd(c *Command) *cobra.Command {
93113
err := e.EncodeFile(f)
94114
exitOnErr(cmd, err, false)
95115
}
116+
96117
if err := e.Close(); err != nil {
97118
exitOnErr(cmd, err, true)
98119
}
120+
121+
if check && !bytes.Equal(formatted.Bytes(), original) {
122+
modifiedFiles = append(modifiedFiles, file.Filename)
123+
}
99124
}
100125
}
126+
127+
if check && len(modifiedFiles) > 0 {
128+
stdout := cmd.OutOrStdout()
129+
for _, f := range modifiedFiles {
130+
if f != "-" {
131+
fmt.Fprintln(stdout, f)
132+
}
133+
}
134+
os.Exit(1)
135+
}
136+
101137
return nil
102138
}),
103139
}
140+
141+
cmd.Flags().Bool(string(flagCheck), false, "exits with non-zero status if any files are not formatted")
142+
104143
return cmd
105144
}
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)