Skip to content

Commit 4eb3d53

Browse files
committed
cmd/cue: drop support for the deprecated short form of cue cmd
`cue foo` used to be a shortcut for `cue cmd foo`, but was deprecated in v0.9.0 following https://cuelang.org/issue/2519. The shortcut introduced a number of problems: * The root `cue` command needed to be significantly more complex; it had to know how to correctly capture flags and arguments for `cue cmd` and pass them along to the subcommand. * Spelling mistakes like `cue evla` instead of `cue eval` could easily result in running arbitrary code if _tool.cue files existed. * In some edge cases we would have to load _tool.cue files outside of `cue cmd` to make sure the user wasn't trying to use a tool command. This also caused confusing error messages, such as the `cue evla` typo resulting in a suggestion to declare the command in a _tool.cue file. The issues above existed so the user could save typing four characters, which doesn't seem worth it. We now only support `cue cmd foo`, which is more explicit and consistent. One test case which covered running a command in "short" code now fails. Two others which only tested fixed bugs in the short form are deleted, as those edge cases are completely gone now. Fixes #2519. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I1d80622f6db3fd0f0fb7494356e5360697b048be Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199634 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 7e589e3 commit 4eb3d53

File tree

8 files changed

+19
-91
lines changed

8 files changed

+19
-91
lines changed

cmd/cue/cmd/cmd.go

+1-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
"cuelang.org/go/cue/errors"
2121
"github.com/spf13/cobra"
22-
"github.com/spf13/pflag"
2322
)
2423

2524
// TODO: generate long description from documentation.
@@ -135,48 +134,26 @@ again).
135134
Run "cue help commands" for more details on tasks and workflow commands.
136135
`,
137136
RunE: mkRunE(c, func(cmd *Command, args []string) error {
138-
// The behavior when there's no known subcommand is different
139-
// depending on whether we ran via `cue cmd` or the `cue` shortcut.
140-
isRootCmd := cmd.Command == cmd.root
141-
142137
if len(args) == 0 {
143-
// `cue` should print the top-level help like `cue -h`,
144-
// but `cue cmd` should explain that a custom command is required.
145-
if isRootCmd {
146-
return pflag.ErrHelp
147-
}
148138
w := cmd.OutOrStderr()
149139
fmt.Fprintln(w, "cmd must be run as one of its subcommands")
150140
fmt.Fprintln(w, "Run 'cue help cmd' for known subcommands.")
151141
return ErrPrintedError
152142
}
153143
tools, err := buildTools(cmd, args[1:])
154-
if err != nil && !isRootCmd {
155-
// `cue cmd` fails immediately if there is no CUE package,
156-
// but `cue` does not in order to always show a useful error in `cue typo`.
144+
if err != nil {
157145
return err
158146
}
159147
sub, err := customCommand(cmd, commandSection, args[0], tools)
160148
if err != nil {
161149
w := cmd.OutOrStderr()
162-
cmdline := "cue"
163-
if !isRootCmd {
164-
cmdline += " cmd"
165-
}
166150
fmt.Fprint(w, errors.Details(err, &errors.Config{Cwd: rootWorkingDir}))
167151
fmt.Fprintln(w, `Ensure custom commands are defined in a "_tool.cue" file.`)
168152
fmt.Fprintln(w, "Run 'cue help cmd' to list available custom commands.")
169-
if isRootCmd {
170-
fmt.Fprintln(w, "Run 'cue help' to see the built-in 'cue' commands.")
171-
}
172153
return ErrPrintedError
173154
}
174155
// Presumably the *cobra.Command argument should be cmd.Command,
175156
// as that is the one which will have the right settings applied.
176-
if isRootCmd {
177-
cmd.PrintErrf("The short-form 'cue %[1]s' is deprecated; use 'cue cmd %[1]s'.\n", args[0])
178-
cmd.PrintErrf("See: https://cuelang.org/issue/2519\n")
179-
}
180157
return sub.RunE(cmd.Command, args[1:])
181158
}),
182159
}

cmd/cue/cmd/common.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func newBuildPlan(cmd *Command, cfg *config) (p *buildPlan, err error) {
385385
}
386386
cfg.reFile = re
387387

388-
setTags(cfg.loadCfg, cmd.Flags(), cmd.Parent().Flags())
388+
setTags(cfg.loadCfg, cmd.Flags())
389389

390390
return p, nil
391391
}
@@ -394,16 +394,11 @@ func (p *buildPlan) matchFile(file string) bool {
394394
return p.cfg.reFile.MatchString(file)
395395
}
396396

397-
func setTags(cfg *load.Config, flags ...*pflag.FlagSet) {
398-
// setTags usually receives a subcommmand's flags, plus the parent (root) command's flags,
399-
// so that we can support "cue cmd -t foo=bar mycmd" as well as the short form "cue -t foo=bar mycmd".
400-
// TODO: once we remove "cue cmd" short-hand support, go back to a single FlagSet parameter.
401-
for _, f := range flags {
402-
tags, _ := f.GetStringArray(string(flagInject))
403-
cfg.Tags = append(cfg.Tags, tags...)
404-
if b, _ := f.GetBool(string(flagInjectVars)); b {
405-
cfg.TagVars = load.DefaultTagVars()
406-
}
397+
func setTags(cfg *load.Config, flags *pflag.FlagSet) {
398+
tags, _ := flags.GetStringArray(string(flagInject))
399+
cfg.Tags = append(cfg.Tags, tags...)
400+
if b, _ := flags.GetBool(string(flagInjectVars)); b {
401+
cfg.TagVars = load.DefaultTagVars()
407402
}
408403
}
409404

@@ -780,8 +775,7 @@ func buildTools(cmd *Command, args []string) (*cue.Instance, error) {
780775
}
781776
loadCfg := *cfg.loadCfg
782777
loadCfg.Tools = true
783-
f := cmd.cmd.Flags()
784-
setTags(&loadCfg, f, cmd.cmd.Parent().Flags())
778+
setTags(&loadCfg, cmd.cmdCmd.Flags())
785779

786780
binst := loadFromArgs(args, &loadCfg)
787781
if len(binst) == 0 {

cmd/cue/cmd/root.go

+5-17
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,6 @@ func New(args []string) (*Command, error) {
190190
// TODO: the short help text below seems to refer to `cue cmd`, like helpTemplate.
191191
Short: "cue emits configuration files to user-defined commands.",
192192

193-
// ArbitraryArgs allows us to forward the top-level RunE to cmdCmd.RunE,
194-
// which supports `cue mycmd` as a short-cut for `cue cmd mycmd`.
195-
// Without ArbitraryArgs, cobra fails with "unknown command" errors.
196-
Args: cobra.ArbitraryArgs,
197-
198193
// We print errors ourselves in Main, which allows for ErrPrintedError.
199194
// Similarly, we don't want to print the entire help text on any error.
200195
// We can explicitly trigger help on certain errors via pflag.ErrHelp.
@@ -203,6 +198,7 @@ func New(args []string) (*Command, error) {
203198

204199
// We currently support top-level custom commands like `cue mycmd` as an alias
205200
// for `cue cmd mycmd`, so any sub-command suggestions might be confusing.
201+
// TODO(mvdan): remove this and test it, as we no longer support `cue mycmd`.
206202
DisableSuggestions: true,
207203
}
208204

@@ -214,14 +210,9 @@ func New(args []string) (*Command, error) {
214210
root: cmd,
215211
ctx: cuecontext.New(rootContextOptions...),
216212
}
217-
218-
cmdCmd := newCmdCmd(c)
219-
c.cmd = cmdCmd
213+
c.cmdCmd = newCmdCmd(c)
220214

221215
addGlobalFlags(cmd.PersistentFlags())
222-
// We add the injection flags to the root command for the sake of the short form "cue -t foo=bar mycmd".
223-
// Note that they are not persistent, so that they aren't inherited by sub-commands like "cue fmt".
224-
addInjectionFlags(cmd.Flags(), false, true)
225216

226217
// Cobra's --help flag shows up in help text by default, which is unnecessary.
227218
cmd.InitDefaultHelpFlag()
@@ -240,7 +231,7 @@ func New(args []string) (*Command, error) {
240231
cmd.SetHelpTemplate(helpTemplate)
241232

242233
for _, sub := range []*cobra.Command{
243-
cmdCmd,
234+
c.cmdCmd,
244235
newCompletionCmd(c),
245236
newEvalCmd(c),
246237
newDefCmd(c),
@@ -261,9 +252,6 @@ func New(args []string) (*Command, error) {
261252
cmd.AddCommand(sub)
262253
}
263254

264-
// For `cue mycmd` to be a shortcut for `cue cmd mycmd`.
265-
cmd.RunE = cmdCmd.RunE
266-
267255
cmd.SetArgs(args)
268256
return c, nil
269257
}
@@ -303,8 +291,8 @@ type Command struct {
303291

304292
root *cobra.Command
305293

306-
// Subcommands
307-
cmd *cobra.Command
294+
// _tool.cue subcommands.
295+
cmdCmd *cobra.Command
308296

309297
ctx *cue.Context
310298

cmd/cue/cmd/testdata/script/cmd_notool2.txtar

-19
This file was deleted.

cmd/cue/cmd/testdata/script/cmd_short.txtar

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ exec cue cmd hello
22
stdout 'Hello world!'
33
! stderr .
44

5-
exec cue hello
6-
stdout 'Hello world!'
7-
cmp stderr stderr-warning
5+
! exec cue hello
6+
cmp stderr short-stderr
87

9-
-- stderr-warning --
10-
The short-form 'cue hello' is deprecated; use 'cue cmd hello'.
11-
See: https://cuelang.org/issue/2519
8+
-- short-stderr --
9+
unknown command "hello" for "cue"
1210
-- task_tool.cue --
1311
package home
1412

cmd/cue/cmd/testdata/script/cmd_tags.txtar

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
exec cue cmd -t prod -t name=bar tag tags.cue tags_tool.cue
22
cmp stdout expect-stdout
33

4-
# Same as before, but now with the short form.
5-
# See https://cuelang.org/issue/2510.
6-
exec cue -t prod -t name=bar tag tags.cue tags_tool.cue
7-
cmp stdout expect-stdout
8-
94
# Verify that the new global -t flag added as a fix for issue 2510 above
105
# works with the explicit or implicit "cmd" sub-command,
116
# but not with other sub-commands like "fmt".

cmd/cue/cmd/testdata/script/help.txtar

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ stdout -count=1 '^The cue tools supports the following file types:$'
3535

3636
! exec cue filetypes
3737
! stdout .
38-
stderr -count=1 '^no commands defined'
39-
# TODO(mvdan): once `cue somecmd` is phased out, we can give a better error message here.
40-
# stderr -count=1 '^unknown command'
38+
stderr -count=1 '^unknown command'
4139

4240
# Requesting help for missing commands and sub-commands fails and prints the help text.
4341

cmd/cue/cmd/testdata/script/unknown_args.txtar

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ cmp stderr unknown_cmd.out
77
cmp stderr unknown_flag.out
88

99
-- unknown_cmd.out --
10-
no commands defined
11-
Ensure custom commands are defined in a "_tool.cue" file.
12-
Run 'cue help cmd' to list available custom commands.
13-
Run 'cue help' to see the built-in 'cue' commands.
10+
unknown command "unknown" for "cue"
1411
-- unknown_flag.out --
1512
unknown flag: --unknown

0 commit comments

Comments
 (0)