Skip to content

Commit 078125e

Browse files
committed
cmd/cue: deduplicate logic to implement command groups
That is, the logic that gives useful errors when `cue mod` or `cue mod unknown` are executed. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I4029b3d3c81596fe3609f81f65dd5b090befeb0f Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211266 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 5a359d5 commit 078125e

File tree

5 files changed

+27
-56
lines changed

5 files changed

+27
-56
lines changed

cmd/cue/cmd/exp.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"cuelang.org/go/cue/load"
2119
"cuelang.org/go/internal/encoding/gotypes"
2220

2321
"github.com/spf13/cobra"
2422
)
2523

2624
func newExpCmd(c *Command) *cobra.Command {
27-
cmd := &cobra.Command{
25+
cmd := commandGroup(c, &cobra.Command{
2826
// Experimental commands are hidden by design.
2927
Hidden: true,
3028

@@ -36,17 +34,7 @@ exp groups commands which are still in an experimental stage.
3634
Experimental commands may be changed or removed at any time,
3735
as the objective is to gain experience and then move the feature elsewhere.
3836
`[1:],
39-
RunE: mkRunE(c, func(cmd *Command, args []string) error {
40-
stderr := cmd.Stderr()
41-
if len(args) == 0 {
42-
fmt.Fprintln(stderr, "exp must be run as one of its subcommands")
43-
} else {
44-
fmt.Fprintf(stderr, "exp must be run as one of its subcommands: unknown subcommand %q\n", args[0])
45-
}
46-
fmt.Fprintln(stderr, "Run 'cue help exp' for known subcommands.")
47-
return ErrPrintedError
48-
}),
49-
}
37+
})
5038

5139
cmd.AddCommand(newExpGenGoTypesCmd(c))
5240
return cmd

cmd/cue/cmd/get.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/spf13/cobra"
2119
)
2220

2321
func newGetCmd(c *Command) *cobra.Command {
24-
cmd := &cobra.Command{
22+
cmd := commandGroup(c, &cobra.Command{
2523
Use: "get <language> [packages]",
2624
Short: "add non-CUE dependencies to the current module",
2725
Long: `Get downloads packages or modules for non-CUE languages
@@ -37,17 +35,7 @@ For information on native CUE modules:
3735
3836
cue help modules
3937
`,
40-
RunE: mkRunE(c, func(cmd *Command, args []string) error {
41-
stderr := cmd.Stderr()
42-
if len(args) == 0 {
43-
fmt.Fprintln(stderr, "get must be run as one of its subcommands")
44-
} else {
45-
fmt.Fprintf(stderr, "get must be run as one of its subcommands: unknown subcommand %q\n", args[0])
46-
}
47-
fmt.Fprintln(stderr, "Run 'cue help get' for known subcommands.")
48-
return ErrPrintedError
49-
}),
50-
}
38+
})
5139
cmd.AddCommand(newGoCmd(c))
5240
return cmd
5341
}

cmd/cue/cmd/mod.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/spf13/cobra"
2119
)
2220

2321
func newModCmd(c *Command) *cobra.Command {
24-
cmd := &cobra.Command{
22+
cmd := commandGroup(c, &cobra.Command{
2523
Use: "mod <cmd> [arguments]",
2624
Short: "module maintenance",
2725
Long: `Mod groups commands which operate on CUE modules.
@@ -32,17 +30,7 @@ just 'cue mod'.
3230
See also:
3331
cue help modules
3432
`,
35-
RunE: mkRunE(c, func(cmd *Command, args []string) error {
36-
stderr := cmd.Stderr()
37-
if len(args) == 0 {
38-
fmt.Fprintln(stderr, "mod must be run as one of its subcommands")
39-
} else {
40-
fmt.Fprintf(stderr, "mod must be run as one of its subcommands: unknown subcommand %q\n", args[0])
41-
}
42-
fmt.Fprintln(stderr, "Run 'cue help mod' for known subcommands.")
43-
return ErrPrintedError
44-
}),
45-
}
33+
})
4634

4735
cmd.AddCommand(newModEditCmd(c))
4836
cmd.AddCommand(newModFixCmd(c))

cmd/cue/cmd/refactor.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/spf13/cobra"
2119
)
2220

2321
func newRefactorCmd(c *Command) *cobra.Command {
24-
cmd := &cobra.Command{
22+
cmd := commandGroup(c, &cobra.Command{
2523
// Experimental so far.
2624
Hidden: true,
2725

@@ -31,17 +29,7 @@ func newRefactorCmd(c *Command) *cobra.Command {
3129
This command groups together commands relating
3230
to altering code within the current CUE module.
3331
`[1:],
34-
RunE: mkRunE(c, func(cmd *Command, args []string) error {
35-
stderr := cmd.Stderr()
36-
if len(args) == 0 {
37-
fmt.Fprintln(stderr, "refactor must be run as one of its subcommands")
38-
} else {
39-
fmt.Fprintf(stderr, "refactor must be run as one of its subcommands: unknown subcommand %q\n", args[0])
40-
}
41-
fmt.Fprintln(stderr, "Run 'cue help refactor' for known subcommands.")
42-
return ErrPrintedError
43-
}),
44-
}
32+
})
4533

4634
cmd.AddCommand(newRefactorImportsCmd(c))
4735
return cmd

cmd/cue/cmd/root.go

+19
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ type Stats struct {
8585
}
8686
}
8787

88+
// commandGroup makes cmd runnable in a way that implements commands grouping more subcommands,
89+
// such as `cue mod` or `cue refactor`. Note that the user can't actually run these commands to
90+
// do anything useful, but we need RunE for good error messages, as Cobra itself is lacking:
91+
// https://github.com/spf13/cobra/issues/706
92+
func commandGroup(c *Command, cmd *cobra.Command) *cobra.Command {
93+
name := cmd.Name()
94+
cmd.RunE = mkRunE(c, func(cmd *Command, args []string) error {
95+
stderr := cmd.Stderr()
96+
if len(args) == 0 {
97+
fmt.Fprintf(stderr, "%s must be run as one of its subcommands\n", name)
98+
} else {
99+
fmt.Fprintf(stderr, "%s must be run as one of its subcommands: unknown subcommand %q\n", name, args[0])
100+
}
101+
fmt.Fprintf(stderr, "Run 'cue help %s' for known subcommands.\n", name)
102+
return ErrPrintedError
103+
})
104+
return cmd
105+
}
106+
88107
func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
89108
return func(cmd *cobra.Command, args []string) error {
90109
c.Command = cmd

0 commit comments

Comments
 (0)