Skip to content

Commit

Permalink
cmd/cue/cmd: fail earlier on faulty flags
Browse files Browse the repository at this point in the history
Change-Id: I2019c18969a22c12dc6e295f67776fc47019bf61
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5646
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
mpvl committed Apr 11, 2020
1 parent 8dcec3c commit 6f37a71
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
28 changes: 16 additions & 12 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
Expand Down
5 changes: 1 addition & 4 deletions cmd/cue/cmd/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions cmd/cue/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions cmd/cue/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions cmd/cue/cmd/testdata/script/issue312.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 6f37a71

Please sign in to comment.