From cf617e88f427787f98e396a2c7701ad42cd1b3aa Mon Sep 17 00:00:00 2001 From: Alisson Santos Date: Tue, 3 Nov 2020 22:05:04 +0100 Subject: [PATCH] Extract repeated code to util function --- pkg/cmd/pr/checkout/checkout.go | 8 +---- pkg/cmd/release/create/create.go | 9 +----- pkg/cmd/repo/clone/clone.go | 10 ++----- pkg/cmdutil/args.go | 13 +++++++++ pkg/cmdutil/args_test.go | 50 ++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 pkg/cmdutil/args_test.go diff --git a/pkg/cmd/pr/checkout/checkout.go b/pkg/cmd/pr/checkout/checkout.go index 30a59a7ca..096be87a9 100644 --- a/pkg/cmd/pr/checkout/checkout.go +++ b/pkg/cmd/pr/checkout/checkout.go @@ -1,7 +1,6 @@ package checkout import ( - "errors" "fmt" "net/http" "os" @@ -44,12 +43,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr cmd := &cobra.Command{ Use: "checkout { | | }", Short: "Check out a pull request in git", - Args: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return &cmdutil.FlagError{Err: errors.New("argument required")} - } - return nil - }, + Args: cmdutil.MinimumArgs(1, "argument required"), RunE: func(cmd *cobra.Command, args []string) error { // support `-R, --repo` override opts.BaseRepo = f.BaseRepo diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index 20ccf30f1..e8a1ce853 100644 --- a/pkg/cmd/release/create/create.go +++ b/pkg/cmd/release/create/create.go @@ -2,7 +2,6 @@ package create import ( "bytes" - "errors" "fmt" "io/ioutil" "net/http" @@ -77,13 +76,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co # upload a release asset with a display label $ gh release create v1.2.3 '/path/to/asset.zip#My display label' `), - Args: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - return nil - } - - return &cmdutil.FlagError{Err: errors.New("could not create: no tag name provided")} - }, + Args: cmdutil.MinimumArgs(1, "could not create: no tag name provided"), RunE: func(cmd *cobra.Command, args []string) error { // support `-R, --repo` override opts.BaseRepo = f.BaseRepo diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index accd443fa..1ea216b54 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -1,7 +1,6 @@ package clone import ( - "errors" "fmt" "net/http" "strings" @@ -37,13 +36,8 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm cmd := &cobra.Command{ DisableFlagsInUseLine: true, - Use: "clone [] [-- ...]", - Args: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return &cmdutil.FlagError{Err: errors.New("cannot clone: repository argument required")} - } - return nil - }, + Use: "clone [] [-- ...]", + Args: cmdutil.MinimumArgs(1, "cannot clone: repository argument required"), Short: "Clone a repository locally", Long: heredoc.Doc(` Clone a GitHub repository locally. diff --git a/pkg/cmdutil/args.go b/pkg/cmdutil/args.go index c203fb691..65f3ade51 100644 --- a/pkg/cmdutil/args.go +++ b/pkg/cmdutil/args.go @@ -8,6 +8,19 @@ import ( "github.com/spf13/pflag" ) +func MinimumArgs(n int, msg string) cobra.PositionalArgs { + if msg == "" { + return cobra.MinimumNArgs(1) + } + + return func(cmd *cobra.Command, args []string) error { + if len(args) < n { + return &FlagError{Err: errors.New(msg)} + } + return nil + } +} + func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error { if len(args) < 1 { return nil diff --git a/pkg/cmdutil/args_test.go b/pkg/cmdutil/args_test.go new file mode 100644 index 000000000..db0b96510 --- /dev/null +++ b/pkg/cmdutil/args_test.go @@ -0,0 +1,50 @@ +package cmdutil + +import "testing" + +func TestMinimumArgs(t *testing.T) { + tests := []struct { + N int + Args []string + }{ + { + N: 1, + Args: []string{"v1.2.3"}, + }, + { + N: 2, + Args: []string{"v1.2.3", "cli/cli"}, + }, + } + + for _, test := range tests { + if got := MinimumArgs(test.N, "")(nil, test.Args); got != nil { + t.Errorf("Got: %v, Want: (nil)", got) + } + } +} + +func TestMinimumNs_with_error(t *testing.T) { + tests := []struct { + N int + CustomMessage string + WantMessage string + }{ + { + N: 1, + CustomMessage: "A custom msg", + WantMessage: "A custom msg", + }, + { + N: 1, + CustomMessage: "", + WantMessage: "requires at least 1 arg(s), only received 0", + }, + } + + for _, test := range tests { + if got := MinimumArgs(test.N, test.CustomMessage)(nil, nil); got.Error() != test.WantMessage { + t.Errorf("Got: %v, Want: %v", got, test.WantMessage) + } + } +}