diff --git a/cmd/ci_lint.go b/cmd/ci_lint.go index 45d323c..a630691 100644 --- a/cmd/ci_lint.go +++ b/cmd/ci_lint.go @@ -12,7 +12,7 @@ var ciLintCmd = &cobra.Command{ Short: "Validate the .gitlab-ci.yml", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a file")} + return &errs.ArgError{Err: errors.New("requires a file")} } return nil }, diff --git a/cmd/clone.go b/cmd/clone.go index b7c69e3..8ad9ff6 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -12,7 +12,7 @@ var cloneCmd = &cobra.Command{ Short: "Clone a repository from GitLab", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a repository")} + return &errs.ArgError{Err: errors.New("requires a repository")} } return nil }, diff --git a/cmd/depend_go.go b/cmd/depend_go.go index d46e149..2085b3b 100644 --- a/cmd/depend_go.go +++ b/cmd/depend_go.go @@ -12,7 +12,7 @@ var dependGOCmd = &cobra.Command{ Short: "List go module package (example.com/hello) dependency", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a package")} + return &errs.ArgError{Err: errors.New("requires a package")} } return nil }, diff --git a/cmd/depend_php.go b/cmd/depend_php.go index b4e7426..a0a18a8 100644 --- a/cmd/depend_php.go +++ b/cmd/depend_php.go @@ -12,7 +12,7 @@ var dependPHPCmd = &cobra.Command{ Short: "List composer package (vendor/name) dependency", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a package")} + return &errs.ArgError{Err: errors.New("requires a package")} } return nil }, diff --git a/cmd/mr_open.go b/cmd/mr_open.go index 98b8f82..066b0bc 100644 --- a/cmd/mr_open.go +++ b/cmd/mr_open.go @@ -15,10 +15,10 @@ var mrOpenCmd = &cobra.Command{ DisableFlagsInUseLine: true, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("missing MRID")} + return &errs.ArgError{Err: errors.New("missing MRID")} } if _, err := strconv.Atoi(args[0]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])} } return nil }, diff --git a/cmd/mr_search.go b/cmd/mr_search.go index d161810..f1a9656 100644 --- a/cmd/mr_search.go +++ b/cmd/mr_search.go @@ -12,7 +12,7 @@ var mrSearchCmd = &cobra.Command{ Short: "Search merge requests", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a query")} + return &errs.ArgError{Err: errors.New("requires a query")} } return nil }, diff --git a/cmd/mr_show.go b/cmd/mr_show.go index 7a4d6f4..6d5a9a7 100644 --- a/cmd/mr_show.go +++ b/cmd/mr_show.go @@ -15,10 +15,10 @@ var mrShowCmd = &cobra.Command{ DisableFlagsInUseLine: true, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("missing MRID")} + return &errs.ArgError{Err: errors.New("missing MRID")} } if _, err := strconv.Atoi(args[0]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])} } return nil }, diff --git a/cmd/own_mr_open.go b/cmd/own_mr_open.go index 4f9ab03..00ee6e0 100644 --- a/cmd/own_mr_open.go +++ b/cmd/own_mr_open.go @@ -15,16 +15,16 @@ var ownMrOpenCmd = &cobra.Command{ DisableFlagsInUseLine: true, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("missing MRID")} + return &errs.ArgError{Err: errors.New("missing MRID")} } if _, err := strconv.Atoi(args[0]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])} } if len(args) < 2 { - return &errs.FlagError{Err: errors.New("missing PID")} + return &errs.ArgError{Err: errors.New("missing PID")} } if _, err := strconv.Atoi(args[1]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid PID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid PID %q", args[0])} } return nil }, diff --git a/cmd/own_mr_show.go b/cmd/own_mr_show.go index d6133b3..517651b 100644 --- a/cmd/own_mr_show.go +++ b/cmd/own_mr_show.go @@ -15,16 +15,16 @@ var ownMrShowCmd = &cobra.Command{ DisableFlagsInUseLine: true, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("missing MRID")} + return &errs.ArgError{Err: errors.New("missing MRID")} } if _, err := strconv.Atoi(args[0]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])} } if len(args) < 2 { - return &errs.FlagError{Err: errors.New("missing PID")} + return &errs.ArgError{Err: errors.New("missing PID")} } if _, err := strconv.Atoi(args[1]); err != nil { - return &errs.FlagError{Err: fmt.Errorf("invalid PID %q", args[0])} + return &errs.ArgError{Err: fmt.Errorf("invalid PID %q", args[0])} } return nil }, diff --git a/cmd/project_search.go b/cmd/project_search.go index afe9096..3182c3b 100644 --- a/cmd/project_search.go +++ b/cmd/project_search.go @@ -12,7 +12,7 @@ var projectSearchCmd = &cobra.Command{ Short: "Search projects", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return &errs.FlagError{Err: errors.New("requires a query")} + return &errs.ArgError{Err: errors.New("requires a query")} } return nil }, diff --git a/internal/errors/handler.go b/internal/errors/handler.go index 35e273e..28d2889 100644 --- a/internal/errors/handler.go +++ b/internal/errors/handler.go @@ -20,11 +20,26 @@ func (e *FlagError) Unwrap() error { return e.Err } +type ArgError struct { + Err error +} + +func (e *ArgError) Error() string { + return e.Err.Error() +} + +func (e *ArgError) Unwrap() error { + return e.Err +} + func Handle(cmd *cobra.Command, err error) { fmt.Println(err) - var flagError *FlagError - if errors.As(err, &flagError) || isUnknownCommand(err) || isDefaultFlagError(err) { + var ( + flagError *FlagError + argError *ArgError + ) + if errors.As(err, &flagError) || errors.As(err, &argError) || isUnknownCommand(err) || isDefaultFlagError(err) { fmt.Println() fmt.Println(cmd.UsageString()) }