Skip to content

Commit

Permalink
cmd/cue/cmd: disallow commands in non-tool files
Browse files Browse the repository at this point in the history
This is not strictly necessary, but it is mostly to be consistent
with v0.2 and not relax restrictions we may want to preserve
before the v0.3 release.

Fixes #654

Change-Id: I7701bd3ce80ca44475cced8cb4e639160b2afd78
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9063
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Mar 24, 2021
1 parent a1551b0 commit b00c91f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
20 changes: 20 additions & 0 deletions cmd/cue/cmd/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
itask "cuelang.org/go/internal/task"
_ "cuelang.org/go/pkg/tool/cli" // Register tasks
_ "cuelang.org/go/pkg/tool/exec"
Expand Down Expand Up @@ -70,6 +71,25 @@ func addCustom(c *Command, parent *cobra.Command, typ, name string, tools *cue.I
if !o.Exists() {
return nil, o.Err()
}

// Ensure there is at least one tool file.
// TODO: remove this block to allow commands to be defined in any file.
outer:
for _, v := range []cue.Value{tools.Lookup(typ), o} {
_, vv := internal.CoreValue(v)
w := vv.(*adt.Vertex)
for _, c := range w.Conjuncts {
src := c.Source()
if src == nil {
continue
}
if strings.HasSuffix(src.Pos().Filename(), "_tool.cue") {
break outer
}
}
return nil, errors.New("no command %q defined in a *_tool.cue file")
}

docs := o.Doc()
var usage, short, long string
if len(docs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_issue650.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package blah
}
-- cue.mod/module.cue --
module: "mod.com"
-- x.cue --
-- x_tool.cue --
package kube

import (
Expand Down
2 changes: 0 additions & 2 deletions cmd/cue/cmd/testdata/script/cmd_notool.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
skip 'error messages'

! cue cmd notool
! stdout .
cmp stderr cmd_baddisplay.out
Expand Down
6 changes: 2 additions & 4 deletions cmd/cue/cmd/testdata/script/cmd_notool2.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
skip 'wrong error message'

! cue notool
! stdout .
cmp stderr cmd_baddisplay.out

-- cmd_baddisplay.out --
cmd must be run as one of its subcommands: unknown subcommand "notool"
command "notool" is not defined
Ensure commands are defined in a "_tool.cue" file.
Run 'cue help cmd' for known subcommands.
Run 'cue help' to show available commands.
-- task.cue --
package home
message: "Hello world!"
Expand Down

0 comments on commit b00c91f

Please sign in to comment.