Skip to content

Commit 330e987

Browse files
authored
chore: add an auth status command for to show whoami output (#670)
1 parent 53c3b0c commit 330e987

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

cmd/auth/status.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Package auth handles commands related to authentication via the CLI
2+
package auth
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"os"
8+
"os/signal"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/alecthomas/kong"
13+
"github.com/buildkite/cli/v3/internal/cli"
14+
"github.com/buildkite/cli/v3/pkg/cmd/factory"
15+
"github.com/buildkite/cli/v3/pkg/cmd/validation"
16+
"github.com/buildkite/cli/v3/pkg/output"
17+
"github.com/buildkite/go-buildkite/v4"
18+
)
19+
20+
type StatusOutput struct {
21+
OrganizationSlug string `json:"organization_slug"`
22+
Token buildkite.AccessToken `json:"token"`
23+
}
24+
25+
func (w StatusOutput) TextOutput() string {
26+
b := strings.Builder{}
27+
28+
b.WriteString(fmt.Sprintf("Current organization: %s\n", w.OrganizationSlug))
29+
b.WriteRune('\n')
30+
b.WriteString(fmt.Sprintf("API Token UUID: %s\n", w.Token.UUID))
31+
b.WriteString(fmt.Sprintf("API Token Description: %s\n", w.Token.Description))
32+
b.WriteString(fmt.Sprintf("API Token Scopes: %v\n", w.Token.Scopes))
33+
b.WriteRune('\n')
34+
b.WriteString(fmt.Sprintf("API Token user name: %s\n", w.Token.User.Name))
35+
b.WriteString(fmt.Sprintf("API Token user email: %s\n", w.Token.User.Email))
36+
37+
return b.String()
38+
}
39+
40+
type StatusCmd struct {
41+
output.OutputFlags
42+
}
43+
44+
func (c *StatusCmd) Help() string {
45+
return `
46+
It returns information on the current session.
47+
48+
Examples:
49+
# List the current token session
50+
$ bk auth status
51+
`
52+
}
53+
54+
func (c *StatusCmd) Run(kongCtx *kong.Context, globals cli.GlobalFlags) error {
55+
f, err := factory.New(factory.WithDebug(globals.EnableDebug()))
56+
if err != nil {
57+
return err
58+
}
59+
60+
if validationErr := validation.ValidateConfiguration(f.Config, kongCtx.Command()); validationErr != nil {
61+
return validationErr
62+
}
63+
64+
format := output.ResolveFormat(c.Output, f.Config.OutputFormat())
65+
66+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
67+
defer stop()
68+
69+
orgSlug := f.Config.OrganizationSlug()
70+
71+
if orgSlug == "" {
72+
orgSlug = "<None>"
73+
}
74+
75+
token, _, err := f.RestAPIClient.AccessTokens.Get(ctx)
76+
if err != nil {
77+
return fmt.Errorf("failed to get access token: %w", err)
78+
}
79+
80+
w := StatusOutput{
81+
OrganizationSlug: orgSlug,
82+
Token: token,
83+
}
84+
85+
err = output.Write(os.Stdout, w, format)
86+
if err != nil {
87+
return fmt.Errorf("failed to write output: %w", err)
88+
}
89+
90+
return nil
91+
}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type CLI struct {
5757
Use use.UseCmd `cmd:"" help:"Select an organization"`
5858
User UserCmd `cmd:"" help:"Invite users to the organization"`
5959
Version VersionCmd `cmd:"" help:"Print the version of the CLI being used"`
60-
Whoami whoami.WhoAmICmd `cmd:"" help:"Print the current user and organization"`
60+
Whoami whoami.WhoAmICmd `cmd:"" help:"Print the current user and organization" hidden:""`
6161
}
6262

6363
type (
@@ -67,6 +67,7 @@ type (
6767
AuthCmd struct {
6868
Login auth.LoginCmd `cmd:"" help:"Login to Buildkite using OAuth"`
6969
Logout auth.LogoutCmd `cmd:"" help:"Logout and remove stored credentials"`
70+
Status auth.StatusCmd `cmd:"" help:"Print the current user auth status"`
7071
}
7172
AgentCmd struct {
7273
Pause agent.PauseCmd `cmd:"" help:"Pause a Buildkite agent."`

0 commit comments

Comments
 (0)