Skip to content

Commit e919342

Browse files
committed
cmd/cue: skip mkRunE in the commandGroup wrapper
mkRunE sets up a CUE *Command with a cue.Context, CUE_STATS_FILE, and all the other machinery for runnable commands evaluating CUE. These grouping commands themselves never do anything beyond giving the user friendlier error messages than Cobra, so we can skip that machinery entirely. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I4e5446ba2b6ce69d56b866a6cd850f350fca95b5 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211268 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 6ab9f11 commit e919342

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

cmd/cue/cmd/exp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
func newExpCmd(c *Command) *cobra.Command {
25-
cmd := commandGroup(c, &cobra.Command{
25+
cmd := commandGroup(&cobra.Command{
2626
// Experimental commands are hidden by design.
2727
Hidden: true,
2828

cmd/cue/cmd/get.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
func newGetCmd(c *Command) *cobra.Command {
22-
cmd := commandGroup(c, &cobra.Command{
22+
cmd := commandGroup(&cobra.Command{
2323
Use: "get <language> [packages]",
2424
Short: "add non-CUE dependencies to the current module",
2525
Long: `Get downloads packages or modules for non-CUE languages

cmd/cue/cmd/mod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
func newModCmd(c *Command) *cobra.Command {
22-
cmd := commandGroup(c, &cobra.Command{
22+
cmd := commandGroup(&cobra.Command{
2323
Use: "mod <cmd> [arguments]",
2424
Short: "module maintenance",
2525
Long: `Mod groups commands which operate on CUE modules.

cmd/cue/cmd/refactor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
func newRefactorCmd(c *Command) *cobra.Command {
22-
cmd := commandGroup(c, &cobra.Command{
22+
cmd := commandGroup(&cobra.Command{
2323
// Experimental so far.
2424
Hidden: true,
2525

cmd/cue/cmd/root.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Stats struct {
8989
// such as `cue mod` or `cue refactor`. Note that the user can't actually run these commands to
9090
// do anything useful, but we need RunE for good error messages, as Cobra itself is lacking:
9191
// https://github.com/spf13/cobra/issues/706
92-
func commandGroup(c *Command, cmd *cobra.Command) *cobra.Command {
92+
func commandGroup(cmd *cobra.Command) *cobra.Command {
9393
// If the user ran `cue mod nosuchcommand --nosuchflag`, we run the `cue mod` command
9494
// because `nosuchcommand` was not a known subcommand.
9595
// In such a case, do not fail with "unknown flag"; what matters to the user is that
@@ -98,16 +98,16 @@ func commandGroup(c *Command, cmd *cobra.Command) *cobra.Command {
9898
cmd.FParseErrWhitelist.UnknownFlags = true
9999

100100
name := cmd.Name()
101-
cmd.RunE = mkRunE(c, func(cmd *Command, args []string) error {
102-
stderr := cmd.Stderr()
101+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
102+
stderr := cmd.OutOrStderr()
103103
if len(args) == 0 {
104104
fmt.Fprintf(stderr, "%s must be run as one of its subcommands\n", name)
105105
} else {
106106
fmt.Fprintf(stderr, "%s must be run as one of its subcommands: unknown subcommand %q\n", name, args[0])
107107
}
108108
fmt.Fprintf(stderr, "Run 'cue help %s' for known subcommands.\n", name)
109109
return ErrPrintedError
110-
})
110+
}
111111
return cmd
112112
}
113113

0 commit comments

Comments
 (0)