Skip to content

Commit f254cc3

Browse files
NoamTDmvdan
authored andcommitted
cmd/cue: improve stdin input handling in cue fmt --check
I was playing around with the `gofmt` command, and noticed that it adds an indication in standard input when the -l flag is used: $ cat some_file.go | gofmt -l <standard input> The current `cue fmt --check` implementation intentionally avoids displaying such output, but I think this is less intuitive as if a user runs `cue fmt --check -`, the empty output can be confusing. This change modifies `cue fmt --check` to write `-` in case `-` was specified as an argument. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: I4cb9e1e75a5f2bfc12c723cc909268315151e552 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193777 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 9e121cb commit f254cc3

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

cmd/cue/cmd/fmt.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,13 @@ func newFmtCmd(c *Command) *cobra.Command {
131131
if check && !bytes.Equal(formatted.Bytes(), original) {
132132
foundBadlyFormatted = true
133133

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)
134+
var path string
135+
f := file.Filename
136+
path, err = filepath.Rel(cwd, f)
137+
if err != nil {
138+
path = f
142139
}
140+
fmt.Fprintln(stdout, path)
143141
}
144142
}
145143
}

cmd/cue/cmd/testdata/script/fmt_check.txtar

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ cmpenv stdout expected-output
1515
! exec cue fmt --check not_formatted.cue another/not_formatted.cue
1616
cmpenv stdout expected-output
1717

18-
# stdin fails with no output
18+
# stdin fails with indicative output
1919
stdin not_formatted.cue
2020
! exec cue fmt --check -
21-
! stdout .
21+
cmp stdout stdin-expected-output
22+
23+
# fails with indicative output when both stdin and files provided
24+
stdin not_formatted.cue
25+
! exec cue fmt --check - another/not_formatted.cue
26+
cmpenv stdout stdin-mixed-expected-output
2227

2328
cd ../module
2429

@@ -39,6 +44,11 @@ x: 1
3944
-- standalone/expected-output --
4045
not_formatted.cue
4146
another${/}not_formatted.cue
47+
-- standalone/stdin-expected-output --
48+
-
49+
-- standalone/stdin-mixed-expected-output --
50+
-
51+
another${/}not_formatted.cue
4252
-- module/cue.mod/module.cue --
4353
module: "example.com"
4454
-- module/not_formatted.cue --

0 commit comments

Comments
 (0)