Skip to content

Commit 9e121cb

Browse files
NoamTDmvdan
authored andcommitted
cmd/cue: make cue fmt --check print files immediately
This makes `cue fmt --check` immediately print the path of each badly formatted file to stdout when they're discovered, instead of displaying all files at the end of the run. This makes the command feel slightly more responsive and quicker, as the command "hangs" for less time. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: Ifb0e707b5f57e15bf44269d6de367dcce397a0e5 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193776 Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 41063b7 commit 9e121cb

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

cmd/cue/cmd/fmt.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ func newFmtCmd(c *Command) *cobra.Command {
6262
cfg.Format = opts
6363
cfg.Force = true
6464

65+
var foundBadlyFormatted bool
6566
check := flagCheck.Bool(cmd)
66-
var badlyFormattedFiles []string
67+
cwd, _ := os.Getwd()
68+
stdout := cmd.OutOrStdout()
6769

6870
for _, inst := range builds {
6971
if inst.Err != nil {
@@ -127,25 +129,22 @@ func newFmtCmd(c *Command) *cobra.Command {
127129
}
128130

129131
if check && !bytes.Equal(formatted.Bytes(), original) {
130-
badlyFormattedFiles = append(badlyFormattedFiles, file.Filename)
132+
foundBadlyFormatted = true
133+
134+
if file.Filename != "-" {
135+
f := file.Filename
136+
var path string
137+
path, err = filepath.Rel(cwd, f)
138+
if err != nil {
139+
path = f
140+
}
141+
fmt.Fprintln(stdout, path)
142+
}
131143
}
132144
}
133145
}
134146

135-
if check && len(badlyFormattedFiles) > 0 {
136-
cwd, _ := os.Getwd()
137-
stdout := cmd.OutOrStdout()
138-
for _, f := range badlyFormattedFiles {
139-
if f == "-" {
140-
continue
141-
}
142-
143-
relPath, err := filepath.Rel(cwd, f)
144-
if err != nil {
145-
relPath = f
146-
}
147-
fmt.Fprintln(stdout, relPath)
148-
}
147+
if check && foundBadlyFormatted {
149148
return ErrPrintedError
150149
}
151150

0 commit comments

Comments
 (0)