Skip to content

Commit

Permalink
cmd/cue: make cue fmt --check print files immediately
Browse files Browse the repository at this point in the history
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 <noam.tzvi.dolovich@gmail.com>
Change-Id: Ifb0e707b5f57e15bf44269d6de367dcce397a0e5
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193776
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
NoamTD authored and mvdan committed Apr 26, 2024
1 parent 41063b7 commit 9e121cb
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func newFmtCmd(c *Command) *cobra.Command {
cfg.Format = opts
cfg.Force = true

var foundBadlyFormatted bool
check := flagCheck.Bool(cmd)
var badlyFormattedFiles []string
cwd, _ := os.Getwd()
stdout := cmd.OutOrStdout()

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

if check && !bytes.Equal(formatted.Bytes(), original) {
badlyFormattedFiles = append(badlyFormattedFiles, file.Filename)
foundBadlyFormatted = true

if file.Filename != "-" {
f := file.Filename
var path string
path, err = filepath.Rel(cwd, f)
if err != nil {
path = f
}
fmt.Fprintln(stdout, path)
}
}
}
}

if check && len(badlyFormattedFiles) > 0 {
cwd, _ := os.Getwd()
stdout := cmd.OutOrStdout()
for _, f := range badlyFormattedFiles {
if f == "-" {
continue
}

relPath, err := filepath.Rel(cwd, f)
if err != nil {
relPath = f
}
fmt.Fprintln(stdout, relPath)
}
if check && foundBadlyFormatted {
return ErrPrintedError
}

Expand Down

0 comments on commit 9e121cb

Please sign in to comment.