File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package auth
2+
3+ import (
4+ "fmt"
5+ "os"
6+
7+ "github.com/alecthomas/kong"
8+ "github.com/buildkite/cli/v3/internal/cli"
9+ "github.com/buildkite/cli/v3/pkg/cmd/factory"
10+ )
11+
12+ type TokenCmd struct {}
13+
14+ func (c * TokenCmd ) Help () string {
15+ return `
16+ Prints the stored API token for the currently selected organization to stdout.
17+
18+ The token is retrieved from the system keychain (or the BUILDKITE_API_TOKEN
19+ environment variable if set). This is useful for passing the token to other
20+ tools, for example:
21+
22+ Examples:
23+ # Print the current token
24+ $ bk auth token
25+
26+ # Use the token in a curl request
27+ $ curl -H "Authorization: Bearer $(bk auth token)" https://api.buildkite.com/v2/user
28+ `
29+ }
30+
31+ func (c * TokenCmd ) Run (kongCtx * kong.Context , globals cli.GlobalFlags ) error {
32+ f , err := factory .New (factory .WithDebug (globals .EnableDebug ()))
33+ if err != nil {
34+ return err
35+ }
36+
37+ token := f .Config .APIToken ()
38+ if token == "" {
39+ return fmt .Errorf ("no token found; run `bk auth login` to authenticate" )
40+ }
41+
42+ fmt .Fprintln (os .Stdout , token )
43+ return nil
44+ }
Original file line number Diff line number Diff line change 7171 Logout auth.LogoutCmd `cmd:"" help:"Logout and remove stored credentials"`
7272 Status auth.StatusCmd `cmd:"" help:"Print the current user auth status"`
7373 Switch auth.SwitchCmd `cmd:"" help:"Switch to a different organization" aliases:"use"`
74+ Token auth.TokenCmd `cmd:"" help:"Print the stored API token for the current organization"`
7475 }
7576 AgentCmd struct {
7677 Install agent.InstallCmd `cmd:"" help:"Install the buildkite-agent binary locally."`
You can’t perform that action at this time.
0 commit comments