X Tutup
Skip to content

Commit d8146cd

Browse files
committed
Extract cmdutil package
1 parent 4c762d5 commit d8146cd

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

cmd/gh/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/cli/cli/command"
1313
"github.com/cli/cli/internal/config"
14+
"github.com/cli/cli/pkg/cmdutil"
1415
"github.com/cli/cli/update"
1516
"github.com/cli/cli/utils"
1617
"github.com/mgutz/ansi"
@@ -48,6 +49,10 @@ func main() {
4849
}
4950

5051
func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
52+
if err == cmdutil.SilentError {
53+
return
54+
}
55+
5156
var dnsError *net.DNSError
5257
if errors.As(err, &dnsError) {
5358
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) {
6065

6166
fmt.Fprintln(out, err)
6267

63-
var flagError *command.FlagError
68+
var flagError *cmdutil.FlagError
6469
if errors.As(err, &flagError) || strings.HasPrefix(err.Error(), "unknown command ") {
6570
if !strings.HasSuffix(err.Error(), "\n") {
6671
fmt.Fprintln(out)

cmd/gh/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net"
88
"testing"
99

10-
"github.com/cli/cli/command"
10+
"github.com/cli/cli/pkg/cmdutil"
1111
"github.com/spf13/cobra"
1212
)
1313

@@ -49,7 +49,7 @@ check your internet connection or githubstatus.com
4949
{
5050
name: "Cobra flag error",
5151
args: args{
52-
err: &command.FlagError{Err: errors.New("unknown flag --foo")},
52+
err: &cmdutil.FlagError{Err: errors.New("unknown flag --foo")},
5353
cmd: cmd,
5454
debug: false,
5555
},

command/issue.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,9 @@ var issueStatusCmd = &cobra.Command{
7373
RunE: issueStatus,
7474
}
7575
var issueViewCmd = &cobra.Command{
76-
Use: "view {<number> | <url>}",
77-
Args: func(cmd *cobra.Command, args []string) error {
78-
if len(args) < 1 {
79-
return FlagError{errors.New("issue number or URL required as argument")}
80-
}
81-
return nil
82-
},
76+
Use: "view {<number> | <url>}",
8377
Short: "View an issue",
78+
Args: cobra.ExactArgs(1),
8479
Long: `Display the title, body, and other information about an issue.
8580
8681
With '--web', open the issue in a web browser instead.`,

command/root.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/cli/cli/internal/config"
1414
"github.com/cli/cli/internal/ghrepo"
1515
apiCmd "github.com/cli/cli/pkg/cmd/api"
16+
"github.com/cli/cli/pkg/cmdutil"
1617
"github.com/cli/cli/utils"
1718

1819
"github.com/spf13/cobra"
@@ -60,25 +61,12 @@ func init() {
6061
if err == pflag.ErrHelp {
6162
return err
6263
}
63-
return &FlagError{Err: err}
64+
return &cmdutil.FlagError{Err: err}
6465
})
6566

6667
RootCmd.AddCommand(apiCmd.NewCmdApi())
6768
}
6869

69-
// FlagError is the kind of error raised in flag processing
70-
type FlagError struct {
71-
Err error
72-
}
73-
74-
func (fe FlagError) Error() string {
75-
return fe.Err.Error()
76-
}
77-
78-
func (fe FlagError) Unwrap() error {
79-
return fe.Err
80-
}
81-
8270
// RootCmd is the entry point of command-line execution
8371
var RootCmd = &cobra.Command{
8472
Use: "gh <command> <subcommand> [flags]",

pkg/cmdutil/errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmdutil
2+
3+
import "errors"
4+
5+
// FlagError is the kind of error raised in flag processing
6+
type FlagError struct {
7+
Err error
8+
}
9+
10+
func (fe FlagError) Error() string {
11+
return fe.Err.Error()
12+
}
13+
14+
func (fe FlagError) Unwrap() error {
15+
return fe.Err
16+
}
17+
18+
// SilentError is an error that triggers exit code 1 without any error messaging
19+
var SilentError = errors.New("SilentError")

0 commit comments

Comments
 (0)
X Tutup