diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 2139bda1e..73ee084e9 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -129,6 +129,7 @@ type StatusOptions struct { Hostname string ShowToken bool + Active bool } func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command { @@ -163,6 +164,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "Check only a specific hostname's auth status") cmd.Flags().BoolVarP(&opts.ShowToken, "show-token", "t", false, "Display the auth token") + cmd.Flags().BoolVarP(&opts.Active, "active", "a", false, "Display the active account only") return cmd } @@ -224,6 +226,10 @@ func statusRun(opts *StatusOptions) error { err = cmdutil.SilentError } + if opts.Active { + continue + } + users := authCfg.UsersForHost(hostname) for _, username := range users { if username == activeUser { diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 03c26ca04..3f16baf46 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -44,6 +44,13 @@ func Test_NewCmdStatus(t *testing.T) { ShowToken: true, }, }, + { + name: "active", + cli: "--active", + wants: StatusOptions{ + Active: true, + }, + }, } for _, tt := range tests { @@ -382,14 +389,14 @@ func Test_statusRun(t *testing.T) { { name: "multiple hosts with multiple accounts with environment tokens and with errors", opts: StatusOptions{}, - env: map[string]string{"GH_ENTERPRISE_TOKEN": "gho_abc123"}, + env: map[string]string{"GH_ENTERPRISE_TOKEN": "gho_abc123"}, // monalisa-ghe-2 cfgStubs: func(t *testing.T, c gh.Config) { login(t, c, "github.com", "monalisa", "gho_def456", "https") login(t, c, "github.com", "monalisa-2", "gho_ghi789", "https") login(t, c, "ghe.io", "monalisa-ghe", "gho_xyz123", "ssh") }, httpStubs: func(reg *httpmock.Registry) { - // Get scopes for monalia-2 + // Get scopes for monalisa-2 reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) // Get scopes for monalisa reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo")) @@ -433,6 +440,94 @@ func Test_statusRun(t *testing.T) { - To forget about this account, run: gh auth logout -h ghe.io -u monalisa-ghe `), }, + { + name: "multiple accounts on a host, only active users", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + }, + wantOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + `), + }, + { + name: "multiple hosts with multiple accounts, only active users", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "ssh") + login(t, c, "ghe.io", "monalisa-ghe-2", "gho_abc123", "ssh") + }, + httpStubs: func(reg *httpmock.Registry) { + // Get scopes for monalisa-2 + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + // Get scopes for monalisa-ghe-2 + reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org")) + }, + wantOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + + ghe.io + ✓ Logged in to ghe.io account monalisa-ghe-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: ssh + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + `), + }, + { + name: "multiple hosts with multiple accounts, only active users with errors", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "ssh") + login(t, c, "ghe.io", "monalisa-ghe-2", "gho_abc123", "ssh") + }, + httpStubs: func(reg *httpmock.Registry) { + // Get scopes for monalisa-2 + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + // Error getting scopes for monalisa-ghe-2 + reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.StatusStringResponse(404, "{}")) + }, + wantErr: cmdutil.SilentError, + wantErrOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + + ghe.io + X Failed to log in to ghe.io account monalisa-ghe-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - The token in GH_CONFIG_DIR/hosts.yml is invalid. + - To re-authenticate, run: gh auth login -h ghe.io + - To forget about this account, run: gh auth logout -h ghe.io -u monalisa-ghe-2 + `), + }, } for _, tt := range tests {