From a55f7af92c5e35491ed002e26b7105caf6d1fa5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 22 Sep 2021 19:36:25 +0200 Subject: [PATCH 1/2] Correct wrong args constraints --- cmd/ghcs/code.go | 2 +- cmd/ghcs/logs.go | 2 +- cmd/ghcs/ports.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 9f09438d5..4a4259e42 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: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return code(codespace, useInsiders) }, diff --git a/cmd/ghcs/logs.go b/cmd/ghcs/logs.go index 514c36966..74a763a72 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: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return logs(context.Background(), log, codespace, follow) }, diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index aeecf0a07..2e93d78ae 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -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 { From 7a91ba5942f6535ce840312594ec5fcc630be5d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 22 Sep 2021 19:51:12 +0200 Subject: [PATCH 2/2] Print usage help when args given to "NoArgs" commands --- cmd/ghcs/code.go | 2 +- cmd/ghcs/common.go | 10 ++++++++++ cmd/ghcs/create.go | 2 +- cmd/ghcs/delete.go | 2 +- cmd/ghcs/list.go | 2 +- cmd/ghcs/logs.go | 2 +- cmd/ghcs/main/main.go | 11 ++++++++--- cmd/ghcs/ports.go | 2 +- 8 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 4a4259e42..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.NoArgs, + 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 45aa794e6..fbc5e099a 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 4f1c71842..defc46883 100644 --- a/cmd/ghcs/delete.go +++ b/cmd/ghcs/delete.go @@ -50,7 +50,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 74a763a72..01f677cf2 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.NoArgs, + 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 2e93d78ae..1e6021809 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) },