Fix parsing gist list --public/--secret flags

It's not sufficient to use `Changed("public")` to test if a boolean flag
was activated, since the user might have passed `--public=false`.
Instead, check the true value of the flag.

The `--public` and `--secret` flags should be mutually exclusive, so now
if both are activated, `--secret` takes precedence.
This commit is contained in:
Mislav Marohnić 2020-10-05 20:27:05 +02:00
parent f124370154
commit 1859728f7e
2 changed files with 17 additions and 10 deletions

View file

@ -27,6 +27,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
HttpClient: f.HttpClient,
}
var flagPublic bool
var flagSecret bool
cmd := &cobra.Command{
Use: "list",
Short: "List your gists",
@ -36,27 +39,23 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
}
pub := cmd.Flags().Changed("public")
secret := cmd.Flags().Changed("secret")
opts.Visibility = "all"
if pub && !secret {
opts.Visibility = "public"
} else if secret && !pub {
if flagSecret {
opts.Visibility = "secret"
} else if flagPublic {
opts.Visibility = "public"
}
if runF != nil {
return runF(opts)
}
return listRun(opts)
},
}
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 10, "Maximum number of gists to fetch")
cmd.Flags().Bool("public", false, "Show only public gists")
cmd.Flags().Bool("secret", false, "Show only secret gists")
cmd.Flags().BoolVar(&flagPublic, "public", false, "Show only public gists")
cmd.Flags().BoolVar(&flagSecret, "secret", false, "Show only secret gists")
return cmd
}

View file

@ -43,12 +43,20 @@ func TestNewCmdList(t *testing.T) {
Visibility: "secret",
},
},
{
name: "secret with explicit false value",
cli: "--secret=false",
wants: ListOptions{
Limit: 10,
Visibility: "all",
},
},
{
name: "public and secret",
cli: "--secret --public",
wants: ListOptions{
Limit: 10,
Visibility: "all",
Visibility: "secret",
},
},
{