diff --git a/cmd/cue/cmd/fix.go b/cmd/cue/cmd/fix.go index ae57c567869..9273f4d57ef 100644 --- a/cmd/cue/cmd/fix.go +++ b/cmd/cue/cmd/fix.go @@ -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 @@ -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")) + } } } } diff --git a/cmd/cue/cmd/testdata/script/fix.txtar b/cmd/cue/cmd/testdata/script/fix.txtar index 9221c2f6473..70b78c29cb1 100644 --- a/cmd/cue/cmd/testdata/script/fix.txtar +++ b/cmd/cue/cmd/testdata/script/fix.txtar @@ -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 @@ -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"]]))]) \ No newline at end of file +out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])