Extract cmdutil package

This commit is contained in:
Mislav Marohnić 2020-05-19 22:31:02 +02:00
parent 4c762d5bd7
commit d8146cd16e
5 changed files with 31 additions and 24 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/cli/cli/command"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/update"
"github.com/cli/cli/utils"
"github.com/mgutz/ansi"
@ -48,6 +49,10 @@ func main() {
}
func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
if err == cmdutil.SilentError {
return
}
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
fmt.Fprintf(out, "error connecting to %s\n", dnsError.Name)
@ -60,7 +65,7 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
fmt.Fprintln(out, err)
var flagError *command.FlagError
var flagError *cmdutil.FlagError
if errors.As(err, &flagError) || strings.HasPrefix(err.Error(), "unknown command ") {
if !strings.HasSuffix(err.Error(), "\n") {
fmt.Fprintln(out)

View file

@ -7,7 +7,7 @@ import (
"net"
"testing"
"github.com/cli/cli/command"
"github.com/cli/cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
@ -49,7 +49,7 @@ check your internet connection or githubstatus.com
{
name: "Cobra flag error",
args: args{
err: &command.FlagError{Err: errors.New("unknown flag --foo")},
err: &cmdutil.FlagError{Err: errors.New("unknown flag --foo")},
cmd: cmd,
debug: false,
},

View file

@ -73,14 +73,9 @@ var issueStatusCmd = &cobra.Command{
RunE: issueStatus,
}
var issueViewCmd = &cobra.Command{
Use: "view {<number> | <url>}",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return FlagError{errors.New("issue number or URL required as argument")}
}
return nil
},
Use: "view {<number> | <url>}",
Short: "View an issue",
Args: cobra.ExactArgs(1),
Long: `Display the title, body, and other information about an issue.
With '--web', open the issue in a web browser instead.`,

View file

@ -13,6 +13,7 @@ import (
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
apiCmd "github.com/cli/cli/pkg/cmd/api"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
@ -60,25 +61,12 @@ func init() {
if err == pflag.ErrHelp {
return err
}
return &FlagError{Err: err}
return &cmdutil.FlagError{Err: err}
})
RootCmd.AddCommand(apiCmd.NewCmdApi())
}
// FlagError is the kind of error raised in flag processing
type FlagError struct {
Err error
}
func (fe FlagError) Error() string {
return fe.Err.Error()
}
func (fe FlagError) Unwrap() error {
return fe.Err
}
// RootCmd is the entry point of command-line execution
var RootCmd = &cobra.Command{
Use: "gh <command> <subcommand> [flags]",

19
pkg/cmdutil/errors.go Normal file
View file

@ -0,0 +1,19 @@
package cmdutil
import "errors"
// FlagError is the kind of error raised in flag processing
type FlagError struct {
Err error
}
func (fe FlagError) Error() string {
return fe.Err.Error()
}
func (fe FlagError) Unwrap() error {
return fe.Err
}
// SilentError is an error that triggers exit code 1 without any error messaging
var SilentError = errors.New("SilentError")