Declare --jq, --template, --silent options mutually exclusive

This commit is contained in:
Mislav Marohnić 2021-03-04 17:29:59 +01:00
parent d89756c94c
commit 4e24f36495
3 changed files with 46 additions and 4 deletions

View file

@ -181,15 +181,29 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
if c.Flags().Changed("hostname") {
if err := ghinstance.HostnameValidator(opts.Hostname); err != nil {
return &cmdutil.FlagError{Err: fmt.Errorf("error parsing --hostname: %w", err)}
return &cmdutil.FlagError{Err: fmt.Errorf("error parsing `--hostname`: %w", err)}
}
}
if opts.Paginate && !strings.EqualFold(opts.RequestMethod, "GET") && opts.RequestPath != "graphql" {
return &cmdutil.FlagError{Err: errors.New(`the '--paginate' option is not supported for non-GET requests`)}
return &cmdutil.FlagError{Err: errors.New("the `--paginate` option is not supported for non-GET requests")}
}
if opts.Paginate && opts.RequestInputFile != "" {
return &cmdutil.FlagError{Err: errors.New(`the '--paginate' option is not supported with '--input'`)}
if err := cmdutil.MutuallyExclusive(
"the `--paginate` option is not supported with `--input`",
opts.Paginate,
opts.RequestInputFile != "",
); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive(
"only one of `--template`, `--jq`, or `--silent` may be used",
opts.Silent,
opts.FilterOutput != "",
opts.Template != "",
); err != nil {
return err
}
if runF != nil {

View file

@ -298,6 +298,21 @@ func Test_NewCmdApi(t *testing.T) {
},
wantsErr: false,
},
{
name: "--silent with --jq",
cli: "user --silent -q .foo",
wantsErr: true,
},
{
name: "--silent with --template",
cli: "user --silent -t '{{.foo}}'",
wantsErr: true,
},
{
name: "--jq with --template",
cli: "user --jq .foo -t '{{.foo}}'",
wantsErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View file

@ -28,3 +28,16 @@ var CancelError = errors.New("CancelError")
func IsUserCancellation(err error) bool {
return errors.Is(err, CancelError) || errors.Is(err, terminal.InterruptErr)
}
func MutuallyExclusive(message string, conditions ...bool) error {
numTrue := 0
for _, ok := range conditions {
if ok {
numTrue++
}
}
if numTrue > 1 {
return &FlagError{Err: errors.New(message)}
}
return nil
}