Skip to content

Commit

Permalink
cmd/cue: support cue fix - to fix CUE via stdin and stdout
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: Id2d88b32c02e4218db4ad119489b4f68f1f39783
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200554
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Sep 3, 2024
1 parent 67443fb commit c0fdf75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 9 additions & 4 deletions cmd/cue/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func runFixAll(cmd *Command, args []string) error {

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

err = os.WriteFile(f.Filename, b, 0644)
if err != nil {
errs = errors.Append(errs, errors.Promote(err, "write"))
if f.Filename == "-" {
if _, err := cmd.OutOrStdout().Write(b); err != nil {
return err
}
} else {
if err := os.WriteFile(f.Filename, b, 0644); err != nil {
errs = errors.Append(errs, errors.Promote(err, "write"))
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/cue/cmd/testdata/script/fix.txtar
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
exec cue fix ./...
# Just like other commands, we can fix with stdin/stdout.
stdin p/three.cue
exec cue fix -
cmp stdout p/three.cue.fixed

# Make sure we fix all files in a directory, even if they're a mix of packages (or no packages).
exec cue fix ./...
cmp p/one.cue p/one.cue.fixed
cmp p/two.cue p/two.cue.fixed
cmp p/three.cue p/three.cue.fixed

-- p/one.cue --
package one

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

out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])
out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])

0 comments on commit c0fdf75

Please sign in to comment.