From 4e24f364951352b0540496440d11cdcd7e038112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 4 Mar 2021 17:29:59 +0100 Subject: [PATCH] Declare `--jq`, `--template`, `--silent` options mutually exclusive --- pkg/cmd/api/api.go | 22 ++++++++++++++++++---- pkg/cmd/api/api_test.go | 15 +++++++++++++++ pkg/cmdutil/errors.go | 13 +++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 05718458f..f04958c36 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -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 { diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 33c314dea..7f394c60c 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -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) { diff --git a/pkg/cmdutil/errors.go b/pkg/cmdutil/errors.go index aa33e98ef..8ae139ef4 100644 --- a/pkg/cmdutil/errors.go +++ b/pkg/cmdutil/errors.go @@ -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 +}