diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 9f09438d5..08d2cff1a 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -19,7 +19,7 @@ func newCodeCmd() *cobra.Command { codeCmd := &cobra.Command{ Use: "code", Short: "Open a codespace in VS Code", - Args: cobra.MaximumNArgs(1), + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { return code(codespace, useInsiders) }, diff --git a/cmd/ghcs/common.go b/cmd/ghcs/common.go index 4ebc89a2d..371ca30b8 100644 --- a/cmd/ghcs/common.go +++ b/cmd/ghcs/common.go @@ -13,6 +13,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" "github.com/github/ghcs/internal/api" + "github.com/spf13/cobra" "golang.org/x/term" ) @@ -144,3 +145,12 @@ func checkAuthorizedKeys(ctx context.Context, client *api.API, user string) erro } return nil // success } + +var ErrTooManyArgs = errors.New("the command accepts no arguments") + +func noArgsConstraint(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return ErrTooManyArgs + } + return nil +} diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index ae64b94a3..345489f6b 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -28,7 +28,7 @@ func newCreateCmd() *cobra.Command { createCmd := &cobra.Command{ Use: "create", Short: "Create a codespace", - Args: cobra.NoArgs, + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { return create(opts) }, diff --git a/cmd/ghcs/delete.go b/cmd/ghcs/delete.go index 94aaaf214..b5d25e7bb 100644 --- a/cmd/ghcs/delete.go +++ b/cmd/ghcs/delete.go @@ -53,7 +53,7 @@ func newDeleteCmd() *cobra.Command { deleteCmd := &cobra.Command{ Use: "delete", Short: "Delete a codespace", - Args: cobra.NoArgs, + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { if opts.deleteAll && opts.repoFilter != "" { return errors.New("both --all and --repo is not supported") diff --git a/cmd/ghcs/list.go b/cmd/ghcs/list.go index 85eabaef5..065b7aa6d 100644 --- a/cmd/ghcs/list.go +++ b/cmd/ghcs/list.go @@ -20,7 +20,7 @@ func newListCmd() *cobra.Command { listCmd := &cobra.Command{ Use: "list", Short: "List your codespaces", - Args: cobra.NoArgs, + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { return list(opts) }, diff --git a/cmd/ghcs/logs.go b/cmd/ghcs/logs.go index 4bc83001b..0cddc6377 100644 --- a/cmd/ghcs/logs.go +++ b/cmd/ghcs/logs.go @@ -24,7 +24,7 @@ func newLogsCmd() *cobra.Command { logsCmd := &cobra.Command{ Use: "logs", Short: "Access codespace logs", - Args: cobra.MaximumNArgs(1), + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { return logs(context.Background(), log, codespace, follow) }, diff --git a/cmd/ghcs/main/main.go b/cmd/ghcs/main/main.go index 01dde1270..6b890d740 100644 --- a/cmd/ghcs/main/main.go +++ b/cmd/ghcs/main/main.go @@ -7,20 +7,25 @@ import ( "os" "github.com/github/ghcs/cmd/ghcs" + "github.com/spf13/cobra" ) func main() { rootCmd := ghcs.NewRootCmd() - if err := rootCmd.Execute(); err != nil { - explainError(os.Stderr, err) + if cmd, err := rootCmd.ExecuteC(); err != nil { + explainError(os.Stderr, err, cmd) os.Exit(1) } } -func explainError(w io.Writer, err error) { +func explainError(w io.Writer, err error, cmd *cobra.Command) { if errors.Is(err, ghcs.ErrTokenMissing) { fmt.Fprintln(w, "The GITHUB_TOKEN environment variable is required. Create a Personal Access Token at https://github.com/settings/tokens/new?scopes=repo") fmt.Fprintln(w, "Make sure to enable SSO for your organizations after creating the token.") return } + if errors.Is(err, ghcs.ErrTooManyArgs) { + _ = cmd.Usage() + return + } } diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 24ec7a6e8..f423245bd 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -31,7 +31,7 @@ func newPortsCmd() *cobra.Command { portsCmd := &cobra.Command{ Use: "ports", Short: "List ports in a codespace", - Args: cobra.NoArgs, + Args: noArgsConstraint, RunE: func(cmd *cobra.Command, args []string) error { return ports(codespace, asJSON) }, @@ -158,7 +158,7 @@ func newPortsPublicCmd() *cobra.Command { return &cobra.Command{ Use: "public ", Short: "Mark port as public", - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { codespace, err := cmd.Flags().GetString("codespace") if err != nil { @@ -179,7 +179,7 @@ func newPortsPrivateCmd() *cobra.Command { return &cobra.Command{ Use: "private ", Short: "Mark port as private", - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { codespace, err := cmd.Flags().GetString("codespace") if err != nil {