Skip to content

Commit

Permalink
cmd/cue/cmd: better checks for incorrect flags
Browse files Browse the repository at this point in the history
Fixes #969

Change-Id: I1628c5158c996830576dc65e4b6fc667e0009b46
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9821
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mpvl committed May 13, 2021
1 parent 073c6aa commit d5041a1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (p *buildPlan) getDecoders(b *build.Instance) (schemas, values []*decoderIn
files = append(files, b.UnknownFiles...)
}
for _, f := range files {
if !p.matchFile(f.Filename) && f.Filename != "-" {
if !b.User && !p.matchFile(f.Filename) {
continue
}
if p.cfg.overrideDefault {
Expand Down Expand Up @@ -565,6 +565,11 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro
}
}

if len(p.insts) == 0 && flagGlob.String(p.cmd) != "" {
return nil, errors.Newf(token.NoPos,
"use of -n/--name flag without a directory")
}

if b := p.orphanInstance; b != nil {
schemas, values, err := p.getDecoders(b)
for _, d := range append(schemas, values...) {
Expand Down Expand Up @@ -630,6 +635,9 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro
}
p.encConfig.Schema = v
}
} else if p.schema != nil {
return nil, errors.Newf(token.NoPos,
"-d/--schema flag specified without a schema")
}

switch {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func addOrphanFlags(f *pflag.FlagSet) {
f.Bool(string(flagWithContext), false, "import as object with contextual data")
f.StringArrayP(string(flagProtoPath), "I", nil, "paths in which to search for imports")
f.String(string(flagProtoEnum), "int", "mode for rendering enums (int|json)")
f.StringP(string(flagGlob), "n", "", "glob filter for file names")
f.StringP(string(flagGlob), "n", "", "glob filter for non-CUE file names in directories")
f.Bool(string(flagMerge), true, "merge non-CUE files")
}

Expand Down
50 changes: 50 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Issue #969

! cue eval test.json -n ".+\.cue"
cmp stderr expect-stderr1

! cue eval test.json -d '#D2'
cmp stderr expect-stderr2

! cue eval test.json -n ".+\.cue" -d '#D2'
cmp stderr expect-stderr3

! cue eval test.json vector.cue -d '#D1'
cmp stderr expect-stderr4

! cue eval test.json vector.cue -d '#D2'
cmp stderr expect-stderr5

-- test.json --
{
"X": 1,
"Y": 2,
"Z": 3
}
-- vector.cue --
package Vector

#D2: {
X: float
Y: float
}

#D3: {
X: float
Y: float
Z: float
}

-- expect-stderr1 --
use of -n/--name flag without a directory
-- expect-stderr2 --
-d/--schema flag specified without a schema
-- expect-stderr3 --
use of -n/--name flag without a directory
-- expect-stderr4 --
reference "#D1" not found:
--schema:1:1
-- expect-stderr5 --
X: conflicting values 1 and float (mismatched types int and float):
./test.json:2:8
./vector.cue:4:8

0 comments on commit d5041a1

Please sign in to comment.