From b38e12ef616ed7c969c38554bd51f38341760bf5 Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Sat, 13 Sep 2025 21:23:42 +0200 Subject: [PATCH] move flag validation to RunE --- pkg/cmd/auth/status/status.go | 11 +++++---- pkg/cmd/auth/status/status_test.go | 36 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index bed9299b8..07ff8bb1e 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -159,6 +159,13 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co To change the active account for a host, see %[1]sgh auth switch%[1]s. `, "`"), RunE: func(cmd *cobra.Command, args []string) error { + if err := cmdutil.MutuallyExclusive( + "`--json` and `--show-token` cannot be used together. To include the token in the JSON output, use `--json token`.", + opts.Exporter != nil, + opts.ShowToken, + ); err != nil { + return err + } if runF != nil { return runF(opts) } @@ -189,10 +196,6 @@ func statusRun(opts *StatusOptions) error { cs := opts.IO.ColorScheme() if opts.Exporter != nil { - if opts.ShowToken { - fmt.Fprintf(stderr, "`--json` and `--show-token` cannot be used together. To include the token in the JSON output, use `--json token`.") - return nil - } opts.ShowToken = true } diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 43d552394..126e7fa7e 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -22,9 +22,11 @@ import ( func Test_NewCmdStatus(t *testing.T) { tests := []struct { - name string - cli string - wants StatusOptions + name string + cli string + wants StatusOptions + wantErr error + wantErrOut string }{ { name: "no arguments", @@ -52,6 +54,11 @@ func Test_NewCmdStatus(t *testing.T) { Active: true, }, }, + { + name: "both --show-token and --json flags", + cli: "--show-token --json state,token", + wantErr: cmdutil.FlagErrorf("`--json` and `--show-token` cannot be used together. To include the token in the JSON output, use `--json token`."), + }, } for _, tt := range tests { @@ -76,11 +83,15 @@ func Test_NewCmdStatus(t *testing.T) { cmd.SetErr(&bytes.Buffer{}) _, err = cmd.ExecuteC() - assert.NoError(t, err) + if tt.wantErr == nil { + assert.NoError(t, err) - assert.Equal(t, tt.wants.Hostname, gotOpts.Hostname) - assert.Equal(t, tt.wants.ShowToken, gotOpts.ShowToken) - assert.Equal(t, tt.wants.Active, gotOpts.Active) + assert.Equal(t, tt.wants.Hostname, gotOpts.Hostname) + assert.Equal(t, tt.wants.ShowToken, gotOpts.ShowToken) + assert.Equal(t, tt.wants.Active, gotOpts.Active) + } else { + assert.Equal(t, tt.wantErr, err) + } }) } } @@ -662,17 +673,6 @@ func Test_statusRun(t *testing.T) { }, wantOut: `{"github.com":[{"active":true,"host":"github.com","login":"monalisa","state":"success","token":"abc123"}]}` + "\n", }, - { - name: "Both show token and json flags", - opts: StatusOptions{ - ShowToken: true, - Exporter: defaultJsonExporter(), - }, - cfgStubs: func(t *testing.T, c gh.Config) { - login(t, c, "github.com", "monalisa", "abc123", "https") - }, - wantErrOut: "`--json` and `--show-token` cannot be used together. To include the token in the JSON output, use `--json token`.", - }, } for _, tt := range tests {