Skip to content

Commit c0fdf75

Browse files
committed
cmd/cue: support cue fix - to fix CUE via stdin and stdout
We were missing the extra few lines of code to handle it, just like other commands such as `cue fmt` or `cue import`. Fixes #3417. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Id2d88b32c02e4218db4ad119489b4f68f1f39783 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200554 Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 67443fb commit c0fdf75

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

cmd/cue/cmd/fix.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runFixAll(cmd *Command, args []string) error {
8989

9090
for _, i := range instances {
9191
for _, f := range i.Files {
92-
if done[f] || !strings.HasSuffix(f.Filename, ".cue") {
92+
if done[f] || (f.Filename != "-" && !strings.HasSuffix(f.Filename, ".cue")) {
9393
continue
9494
}
9595
done[f] = true
@@ -99,9 +99,14 @@ func runFixAll(cmd *Command, args []string) error {
9999
errs = errors.Append(errs, errors.Promote(err, "format"))
100100
}
101101

102-
err = os.WriteFile(f.Filename, b, 0644)
103-
if err != nil {
104-
errs = errors.Append(errs, errors.Promote(err, "write"))
102+
if f.Filename == "-" {
103+
if _, err := cmd.OutOrStdout().Write(b); err != nil {
104+
return err
105+
}
106+
} else {
107+
if err := os.WriteFile(f.Filename, b, 0644); err != nil {
108+
errs = errors.Append(errs, errors.Promote(err, "write"))
109+
}
105110
}
106111
}
107112
}

cmd/cue/cmd/testdata/script/fix.txtar

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
exec cue fix ./...
1+
# Just like other commands, we can fix with stdin/stdout.
2+
stdin p/three.cue
3+
exec cue fix -
4+
cmp stdout p/three.cue.fixed
5+
26
# Make sure we fix all files in a directory, even if they're a mix of packages (or no packages).
7+
exec cue fix ./...
38
cmp p/one.cue p/one.cue.fixed
49
cmp p/two.cue p/two.cue.fixed
510
cmp p/three.cue p/three.cue.fixed
11+
612
-- p/one.cue --
713
package one
814

@@ -28,4 +34,4 @@ out: list.Repeat(["baz"], 3)
2834
-- p/three.cue.fixed --
2935
import "list"
3036

31-
out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])
37+
out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])

0 commit comments

Comments
 (0)