Skip to content

Commit 1859728

Browse files
committed
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.
1 parent f124370 commit 1859728

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pkg/cmd/gist/list/list.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
2727
HttpClient: f.HttpClient,
2828
}
2929

30+
var flagPublic bool
31+
var flagSecret bool
32+
3033
cmd := &cobra.Command{
3134
Use: "list",
3235
Short: "List your gists",
@@ -36,27 +39,23 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
3639
return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
3740
}
3841

39-
pub := cmd.Flags().Changed("public")
40-
secret := cmd.Flags().Changed("secret")
41-
4242
opts.Visibility = "all"
43-
if pub && !secret {
44-
opts.Visibility = "public"
45-
} else if secret && !pub {
43+
if flagSecret {
4644
opts.Visibility = "secret"
45+
} else if flagPublic {
46+
opts.Visibility = "public"
4747
}
4848

4949
if runF != nil {
5050
return runF(opts)
5151
}
52-
5352
return listRun(opts)
5453
},
5554
}
5655

5756
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 10, "Maximum number of gists to fetch")
58-
cmd.Flags().Bool("public", false, "Show only public gists")
59-
cmd.Flags().Bool("secret", false, "Show only secret gists")
57+
cmd.Flags().BoolVar(&flagPublic, "public", false, "Show only public gists")
58+
cmd.Flags().BoolVar(&flagSecret, "secret", false, "Show only secret gists")
6059

6160
return cmd
6261
}

pkg/cmd/gist/list/list_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ func TestNewCmdList(t *testing.T) {
4343
Visibility: "secret",
4444
},
4545
},
46+
{
47+
name: "secret with explicit false value",
48+
cli: "--secret=false",
49+
wants: ListOptions{
50+
Limit: 10,
51+
Visibility: "all",
52+
},
53+
},
4654
{
4755
name: "public and secret",
4856
cli: "--secret --public",
4957
wants: ListOptions{
5058
Limit: 10,
51-
Visibility: "all",
59+
Visibility: "secret",
5260
},
5361
},
5462
{

0 commit comments

Comments
 (0)