Skip to content

Commit f35e704

Browse files
committed
cmd/cue: add fmt subcommand --check mode
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 <[email protected]>
1 parent 3e6a8be commit f35e704

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

cmd/cue/cmd/fmt.go

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

1717
import (
18+
"bytes"
19+
"fmt"
1820
"github.com/spf13/cobra"
21+
"os"
1922

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

62+
check := flagCheck.Bool(cmd)
63+
var modifiedFiles []string
64+
5965
for _, inst := range builds {
6066
if inst.Err != nil {
6167
switch {
@@ -67,7 +73,7 @@ func newFmtCmd(c *Command) *cobra.Command {
6773
}
6874
}
6975
for _, file := range inst.BuildFiles {
70-
files := []*ast.File{}
76+
var files []*ast.File
7177
d := encoding.NewDecoder(file, &cfg)
7278
for ; !d.Done(); d.Next() {
7379
f := d.File()
@@ -86,20 +92,56 @@ func newFmtCmd(c *Command) *cobra.Command {
8692
exitOnErr(cmd, err, true)
8793
}
8894

95+
var formatted bytes.Buffer
96+
if check {
97+
cfg.Out = &formatted
98+
}
99+
89100
e, err := encoding.NewEncoder(file, &cfg)
90101
exitOnErr(cmd, err, true)
91102

92103
for _, f := range files {
93104
err := e.EncodeFile(f)
94105
exitOnErr(cmd, err, false)
95106
}
107+
108+
if check {
109+
var originalBytes []byte
110+
if file.Filename != "-" {
111+
originalBytes, err = os.ReadFile(file.Filename)
112+
exitOnErr(cmd, err, true)
113+
} else {
114+
originalBytes = file.Source.([]byte)
115+
}
116+
117+
if !bytes.Equal(formatted.Bytes(), originalBytes) {
118+
modifiedFiles = append(modifiedFiles, file.Filename)
119+
}
120+
}
121+
96122
if err := e.Close(); err != nil {
97123
exitOnErr(cmd, err, true)
98124
}
99125
}
100126
}
127+
128+
if check {
129+
if len(modifiedFiles) > 0 {
130+
for _, f := range modifiedFiles {
131+
if f != "-" {
132+
fmt.Fprintln(cmd.OutOrStdout(), f)
133+
}
134+
}
135+
136+
os.Exit(1)
137+
}
138+
}
139+
101140
return nil
102141
}),
103142
}
143+
144+
cmd.Flags().Bool(string(flagCheck), false, "exits with non-zero status if any files are not formatted")
145+
104146
return cmd
105147
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# succeeds when file is formatted
2+
exec cue fmt --check formatted.cue
3+
4+
# files are not modified
5+
exec stat -f %c formatted.cue
6+
cp stdout formatted_time_before
7+
exec cue fmt formatted.cue
8+
exec stat -f %c formatted.cue
9+
cmp stdout formatted_time_before
10+
11+
stdin formatted.cue
12+
exec cue fmt --check -
13+
14+
-- formatted.cue --
15+
foo: "bar"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# fails and displays non formatted files
2+
! exec cue fmt --check not_formatted.cue another_not_formatted.cue
3+
cp stdout actual-stdout
4+
grep not_formatted.cue actual-stdout
5+
grep another_not_formatted.cue actual-stdout
6+
! grep correct.cue actual-stdout
7+
8+
# stdin fails with no output
9+
stdin not_formatted.cue
10+
! exec cue fmt --check -
11+
cmp stdout expect-empty-stdout
12+
13+
-- correct.cue --
14+
foo: "bar"
15+
-- not_formatted.cue --
16+
foo: "bar"
17+
-- another_not_formatted.cue --
18+
bar: "baz"
19+
x: 1
20+
-- expect-empty-stdout --

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)