From 6f37a71e39b2c183c39875f2f442b8c8ae2b2526 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Sat, 11 Apr 2020 12:57:07 +0200 Subject: [PATCH] cmd/cue/cmd: fail earlier on faulty flags Change-Id: I2019c18969a22c12dc6e295f67776fc47019bf61 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5646 Reviewed-by: Marcel van Lohuizen --- cmd/cue/cmd/common.go | 28 ++++++++++++++---------- cmd/cue/cmd/custom.go | 2 +- cmd/cue/cmd/def.go | 5 +---- cmd/cue/cmd/eval.go | 5 +---- cmd/cue/cmd/export.go | 5 +---- cmd/cue/cmd/testdata/script/issue312.txt | 6 +++++ 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go index 15ea9ddecb9..67ed7a958ce 100644 --- a/cmd/cue/cmd/common.go +++ b/cmd/cue/cmd/common.go @@ -139,6 +139,9 @@ type buildPlan struct { expressions []ast.Expr // only evaluate these expressions within results schema ast.Expr // selects schema in instance for orphaned values + // outFile defines the file to output to. Default is CUE stdout. + outFile *build.File + encConfig *encoding.Config merge []*build.Instance } @@ -364,18 +367,18 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro } cfg.loadCfg.Stdin = cmd.InOrStdin() - builds := loadFromArgs(cmd, args, cfg.loadCfg) - if builds == nil { - return nil, errors.Newf(token.NoPos, "invalid args") - } - decorateInstances(cmd, flagInject.StringArray(cmd), builds) - p = &buildPlan{cfg: cfg, cmd: cmd, importing: cfg.loadCfg.DataFiles} if err := p.parseFlags(); err != nil { return nil, err } + builds := loadFromArgs(cmd, args, cfg.loadCfg) + if builds == nil { + return nil, errors.Newf(token.NoPos, "invalid args") + } + decorateInstances(cmd, flagInject.StringArray(cmd), builds) + for _, b := range builds { if b.Err != nil { return nil, b.Err @@ -446,24 +449,25 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro return p, nil } -func (b *buildPlan) out(def string) (*build.File, error) { +func (b *buildPlan) parseFlags() (err error) { out := flagOut.String(b.cmd) outFile := flagOutFile.String(b.cmd) if strings.Contains(out, ":") && strings.Contains(outFile, ":") { - return nil, errors.Newf(token.NoPos, + return errors.Newf(token.NoPos, "cannot specify qualifier in both --out and --outfile") } if outFile == "" { - outFile = def + outFile = "-" } if out != "" { outFile = out + ":" + outFile } - return filetypes.ParseFile(outFile, b.cfg.outMode) -} + b.outFile, err = filetypes.ParseFile(outFile, b.cfg.outMode) + if err != nil { + return err + } -func (b *buildPlan) parseFlags() (err error) { for _, e := range flagExpression.StringArray(b.cmd) { expr, err := parser.ParseExpr("--expression", e) if err != nil { diff --git a/cmd/cue/cmd/custom.go b/cmd/cue/cmd/custom.go index b1d51589867..9a6976cc52b 100644 --- a/cmd/cue/cmd/custom.go +++ b/cmd/cue/cmd/custom.go @@ -281,7 +281,7 @@ func executeTasks(cmd *Command, typ, command string, inst *cue.Instance) (err er return true } - // Prevent inifinite walks + // Prevent infinite walks _, vPath := v.Reference() if vPath != nil { vPath := string(keyForReference(vPath...)) diff --git a/cmd/cue/cmd/def.go b/cmd/cue/cmd/def.go index 11927c2f75e..bb21e4a5bb5 100644 --- a/cmd/cue/cmd/def.go +++ b/cmd/cue/cmd/def.go @@ -54,10 +54,7 @@ func runDef(cmd *Command, args []string) error { b, err := parseArgs(cmd, args, &config{outMode: filetypes.Def}) exitOnErr(cmd, err, true) - f, err := b.out("-") - exitOnErr(cmd, err, true) - - e, err := encoding.NewEncoder(f, b.encConfig) + e, err := encoding.NewEncoder(b.outFile, b.encConfig) exitOnErr(cmd, err, true) iter := b.instances() diff --git a/cmd/cue/cmd/eval.go b/cmd/cue/cmd/eval.go index 361bff0cbfa..8d94aa869bb 100644 --- a/cmd/cue/cmd/eval.go +++ b/cmd/cue/cmd/eval.go @@ -108,10 +108,7 @@ func runEval(cmd *Command, args []string) error { } b.encConfig.Format = opts - f, err := b.out("-") - exitOnErr(cmd, err, true) - - e, err := encoding.NewEncoder(f, b.encConfig) + e, err := encoding.NewEncoder(b.outFile, b.encConfig) exitOnErr(cmd, err, true) iter := b.instances() diff --git a/cmd/cue/cmd/export.go b/cmd/cue/cmd/export.go index b53ab3c17df..0f7669baf47 100644 --- a/cmd/cue/cmd/export.go +++ b/cmd/cue/cmd/export.go @@ -107,10 +107,7 @@ func runExport(cmd *Command, args []string) error { b, err := parseArgs(cmd, args, &config{outMode: filetypes.Export}) exitOnErr(cmd, err, true) - f, err := b.out("-") - exitOnErr(cmd, err, true) - - enc, err := encoding.NewEncoder(f, b.encConfig) + enc, err := encoding.NewEncoder(b.outFile, b.encConfig) exitOnErr(cmd, err, true) defer enc.Close() diff --git a/cmd/cue/cmd/testdata/script/issue312.txt b/cmd/cue/cmd/testdata/script/issue312.txt index 3e060a4a766..f7cfd8b12c7 100644 --- a/cmd/cue/cmd/testdata/script/issue312.txt +++ b/cmd/cue/cmd/testdata/script/issue312.txt @@ -1,3 +1,9 @@ +! cue export x.cue -out cue +cmp stderr expect-stderr + +! cue export -out cue +cmp stderr expect-stderr + ! cue export -out x.cue cmp stderr expect-stderr