Merge pull request #184 from github/args-constraint

Consistently institute constraints for position arguments and improve error message
This commit is contained in:
Mislav Marohnić 2021-09-24 16:02:36 +02:00 committed by GitHub
commit 8807b3a73a
8 changed files with 26 additions and 11 deletions

View file

@ -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)
},

View file

@ -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
}

View file

@ -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)
},

View file

@ -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")

View file

@ -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)
},

View file

@ -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)
},

View file

@ -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
}
}

View file

@ -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 <port>",
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 <port>",
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 {