Skip to content

Commit 1a614f3

Browse files
fix: return rc=2 for unsupported commands (#595)
Signed-off-by: Michael Todorovic <[email protected]>
1 parent 235a7e0 commit 1a614f3

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

cmd/controllers/controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ package controllers
55

66
import (
77
"github.com/padok-team/burrito/internal/burrito"
8-
8+
cmdUtils "github.com/padok-team/burrito/internal/utils/cmd"
99
"github.com/spf13/cobra"
1010
)
1111

1212
func BuildControllersCmd(app *burrito.App) *cobra.Command {
1313
cmd := &cobra.Command{
1414
Use: "controllers",
1515
Short: "cmd to use burrito's controllers",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
// If we reach this point, it means no subcommand was matched
18+
cmdUtils.UnsupportedCommand(cmd, args)
19+
return cmd.Help()
20+
},
1621
}
1722
cmd.AddCommand(buildControllersStartCmd(app))
1823
return cmd

cmd/datastore/datastore.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ package datastore
55

66
import (
77
"github.com/padok-team/burrito/internal/burrito"
8+
cmdUtils "github.com/padok-team/burrito/internal/utils/cmd"
89
"github.com/spf13/cobra"
910
)
1011

1112
func BuildDatastoreCmd(app *burrito.App) *cobra.Command {
1213
cmd := &cobra.Command{
1314
Use: "datastore",
1415
Short: "cmd to use burrito's datastore",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
// If we reach this point, it means no subcommand was matched
18+
cmdUtils.UnsupportedCommand(cmd, args)
19+
return cmd.Help()
20+
},
1521
}
1622
cmd.AddCommand(buildDatastoreStartCmd(app))
1723
return cmd

cmd/runner/runner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ package runner
55

66
import (
77
"github.com/padok-team/burrito/internal/burrito"
8+
cmdUtils "github.com/padok-team/burrito/internal/utils/cmd"
89
"github.com/spf13/cobra"
910
)
1011

1112
func BuildRunnerCmd(app *burrito.App) *cobra.Command {
1213
cmd := &cobra.Command{
1314
Use: "runner",
1415
Short: "cmd to use burrito's runner",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
// If we reach this point, it means no subcommand was matched
18+
cmdUtils.UnsupportedCommand(cmd, args)
19+
return cmd.Help()
20+
},
1521
}
1622
cmd.AddCommand(buildRunnerStartCmd(app))
1723
return cmd

cmd/server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package server
22

33
import (
44
"github.com/padok-team/burrito/internal/burrito"
5+
cmdUtils "github.com/padok-team/burrito/internal/utils/cmd"
56
"github.com/spf13/cobra"
67
)
78

89
func BuildServerCmd(app *burrito.App) *cobra.Command {
910
cmd := &cobra.Command{
1011
Use: "server",
1112
Short: "cmd to use burrito's server",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
// If we reach this point, it means no subcommand was matched
15+
cmdUtils.UnsupportedCommand(cmd, args)
16+
return cmd.Help()
17+
},
1218
}
1319
cmd.AddCommand(buildServerStartCmd(app))
1420
return cmd

internal/utils/cmd/cmd.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
7+
8+
"github.com/spf13/cobra"
69
)
710

811
func Verbose(cmd *exec.Cmd) {
912
cmd.Stdout = os.Stdout
1013
cmd.Stderr = os.Stderr
1114
}
15+
16+
func UnsupportedCommand(cmd *cobra.Command, args []string) {
17+
if len(args) > 0 {
18+
fmt.Fprintf(os.Stderr, "Error: unknown %s subcommand: %s\n", cmd.Use, args[0])
19+
}
20+
fmt.Fprintf(os.Stderr, "Run 'burrito %s --help' for usage\n", cmd.Use)
21+
if err := cmd.Help(); err != nil {
22+
fmt.Fprintf(os.Stderr, "Error displaying help: %v\n", err)
23+
os.Exit(1)
24+
}
25+
os.Exit(2)
26+
}

0 commit comments

Comments
 (0)