Skip to content

Commit eb0b3df

Browse files
authored
feat: add bk auth token command (#736)
1 parent 08b8020 commit eb0b3df

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

cmd/auth/token.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type (
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."`

0 commit comments

Comments
 (0)