Merge pull request #2353 from alissonbrunosa/refactoring-custom-error-messages

[Cosmetic] Extract duplicated code to a reusable function
This commit is contained in:
Nate Smith 2020-11-03 19:13:11 -06:00 committed by GitHub
commit cee26cf172
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 23 deletions

View file

@ -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 {<number> | <url> | <branch>}",
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

View file

@ -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

View file

@ -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 <repository> [<directory>] [-- <gitflags>...]",
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 <repository> [<directory>] [-- <gitflags>...]",
Args: cmdutil.MinimumArgs(1, "cannot clone: repository argument required"),
Short: "Clone a repository locally",
Long: heredoc.Doc(`
Clone a GitHub repository locally.

View file

@ -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

50
pkg/cmdutil/args_test.go Normal file
View file

@ -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)
}
}
}