X Tutup
Skip to content

Commit cee26cf

Browse files
author
Nate Smith
authored
Merge pull request cli#2353 from alissonbrunosa/refactoring-custom-error-messages
[Cosmetic] Extract duplicated code to a reusable function
2 parents 9100633 + cf617e8 commit cee26cf

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

pkg/cmd/pr/checkout/checkout.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package checkout
22

33
import (
4-
"errors"
54
"fmt"
65
"net/http"
76
"os"
@@ -44,12 +43,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
4443
cmd := &cobra.Command{
4544
Use: "checkout {<number> | <url> | <branch>}",
4645
Short: "Check out a pull request in git",
47-
Args: func(cmd *cobra.Command, args []string) error {
48-
if len(args) == 0 {
49-
return &cmdutil.FlagError{Err: errors.New("argument required")}
50-
}
51-
return nil
52-
},
46+
Args: cmdutil.MinimumArgs(1, "argument required"),
5347
RunE: func(cmd *cobra.Command, args []string) error {
5448
// support `-R, --repo` override
5549
opts.BaseRepo = f.BaseRepo

pkg/cmd/release/create/create.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package create
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"io/ioutil"
87
"net/http"
@@ -77,13 +76,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
7776
# upload a release asset with a display label
7877
$ gh release create v1.2.3 '/path/to/asset.zip#My display label'
7978
`),
80-
Args: func(cmd *cobra.Command, args []string) error {
81-
if len(args) > 0 {
82-
return nil
83-
}
84-
85-
return &cmdutil.FlagError{Err: errors.New("could not create: no tag name provided")}
86-
},
79+
Args: cmdutil.MinimumArgs(1, "could not create: no tag name provided"),
8780
RunE: func(cmd *cobra.Command, args []string) error {
8881
// support `-R, --repo` override
8982
opts.BaseRepo = f.BaseRepo

pkg/cmd/repo/clone/clone.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package clone
22

33
import (
4-
"errors"
54
"fmt"
65
"net/http"
76
"strings"
@@ -37,13 +36,8 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
3736
cmd := &cobra.Command{
3837
DisableFlagsInUseLine: true,
3938

40-
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
41-
Args: func(cmd *cobra.Command, args []string) error {
42-
if len(args) == 0 {
43-
return &cmdutil.FlagError{Err: errors.New("cannot clone: repository argument required")}
44-
}
45-
return nil
46-
},
39+
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
40+
Args: cmdutil.MinimumArgs(1, "cannot clone: repository argument required"),
4741
Short: "Clone a repository locally",
4842
Long: heredoc.Doc(`
4943
Clone a GitHub repository locally.

pkg/cmdutil/args.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import (
88
"github.com/spf13/pflag"
99
)
1010

11+
func MinimumArgs(n int, msg string) cobra.PositionalArgs {
12+
if msg == "" {
13+
return cobra.MinimumNArgs(1)
14+
}
15+
16+
return func(cmd *cobra.Command, args []string) error {
17+
if len(args) < n {
18+
return &FlagError{Err: errors.New(msg)}
19+
}
20+
return nil
21+
}
22+
}
23+
1124
func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
1225
if len(args) < 1 {
1326
return nil

pkg/cmdutil/args_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmdutil
2+
3+
import "testing"
4+
5+
func TestMinimumArgs(t *testing.T) {
6+
tests := []struct {
7+
N int
8+
Args []string
9+
}{
10+
{
11+
N: 1,
12+
Args: []string{"v1.2.3"},
13+
},
14+
{
15+
N: 2,
16+
Args: []string{"v1.2.3", "cli/cli"},
17+
},
18+
}
19+
20+
for _, test := range tests {
21+
if got := MinimumArgs(test.N, "")(nil, test.Args); got != nil {
22+
t.Errorf("Got: %v, Want: (nil)", got)
23+
}
24+
}
25+
}
26+
27+
func TestMinimumNs_with_error(t *testing.T) {
28+
tests := []struct {
29+
N int
30+
CustomMessage string
31+
WantMessage string
32+
}{
33+
{
34+
N: 1,
35+
CustomMessage: "A custom msg",
36+
WantMessage: "A custom msg",
37+
},
38+
{
39+
N: 1,
40+
CustomMessage: "",
41+
WantMessage: "requires at least 1 arg(s), only received 0",
42+
},
43+
}
44+
45+
for _, test := range tests {
46+
if got := MinimumArgs(test.N, test.CustomMessage)(nil, nil); got.Error() != test.WantMessage {
47+
t.Errorf("Got: %v, Want: %v", got, test.WantMessage)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)
X Tutup