Skip to content

Commit 93eeaa2

Browse files
authored
chore: move whoami to Kong (#584)
Signed-off-by: Ben McNicholl <[email protected]>
1 parent 0b0696a commit 93eeaa2

File tree

5 files changed

+115
-91
lines changed

5 files changed

+115
-91
lines changed

cmd/whoami/whoami.go

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

main.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/buildkite/cli/v3/cmd/build"
1212
"github.com/buildkite/cli/v3/cmd/cluster"
1313
"github.com/buildkite/cli/v3/cmd/job"
14+
"github.com/buildkite/cli/v3/cmd/whoami"
1415
"github.com/buildkite/cli/v3/internal/cli"
1516
bkErrors "github.com/buildkite/cli/v3/internal/errors"
1617
"github.com/buildkite/cli/v3/internal/version"
@@ -27,20 +28,20 @@ type CLI struct {
2728
Quiet bool `help:"Suppress progress output" short:"q"`
2829
// Verbose bool `help:"Enable verbose error output" short:"V"` // TODO: Implement this, atm this is just a skeleton flag
2930

30-
Agent AgentCmd `cmd:"" help:"Manage agents"`
31-
Api ApiCmd `cmd:"" help:"Interact with the Buildkite API"`
32-
Artifacts ArtifactsCmd `cmd:"" help:"Manage pipeline build artifacts"`
33-
Build BuildCmd `cmd:"" help:"Manage pipeline builds"`
34-
Cluster ClusterCmd `cmd:"" help:"Manage organization clusters"`
35-
Configure ConfigureCmd `cmd:"" help:"Configure Buildkite API token"`
36-
Init InitCmd `cmd:"" help:"Initialize a pipeline.yaml file"`
37-
Job JobCmd `cmd:"" help:"Manage jobs within a build"`
38-
Pipeline PipelineCmd `cmd:"" help:"Manage pipelines"`
39-
Package PackageCmd `cmd:"" help:"Manage packages"`
40-
Use UseCmd `cmd:"" help:"Select an organization"`
41-
User UserCmd `cmd:"" help:"Invite users to the organization"`
42-
Version VersionCmd `cmd:"" help:"Print the version of the CLI being used"`
43-
Whoami WhoamiCmd `cmd:"" help:"Print the current user and organization"`
31+
Agent AgentCmd `cmd:"" help:"Manage agents"`
32+
Api ApiCmd `cmd:"" help:"Interact with the Buildkite API"`
33+
Artifacts ArtifactsCmd `cmd:"" help:"Manage pipeline build artifacts"`
34+
Build BuildCmd `cmd:"" help:"Manage pipeline builds"`
35+
Cluster ClusterCmd `cmd:"" help:"Manage organization clusters"`
36+
Configure ConfigureCmd `cmd:"" help:"Configure Buildkite API token"`
37+
Init InitCmd `cmd:"" help:"Initialize a pipeline.yaml file"`
38+
Job JobCmd `cmd:"" help:"Manage jobs within a build"`
39+
Pipeline PipelineCmd `cmd:"" help:"Manage pipelines"`
40+
Package PackageCmd `cmd:"" help:"Manage packages"`
41+
Use UseCmd `cmd:"" help:"Select an organization"`
42+
User UserCmd `cmd:"" help:"Invite users to the organization"`
43+
Version VersionCmd `cmd:"" help:"Print the version of the CLI being used"`
44+
Whoami whoami.WhoAmICmd `cmd:"" help:"Print the current user and organization"`
4445
}
4546

4647
// Hybrid delegation commands, we should delete from these when native Kong implementations ready
@@ -99,9 +100,6 @@ type (
99100
UseCmd struct {
100101
Args []string `arg:"" optional:"" passthrough:"all"`
101102
}
102-
WhoamiCmd struct {
103-
Args []string `arg:"" optional:"" passthrough:"all"`
104-
}
105103
)
106104

107105
// Delegation methods, we should delete when native Kong implementations ready
@@ -113,7 +111,6 @@ func (a *ApiCmd) Run(cli *CLI) error { return cli.delegateToCobraSystem("a
113111
func (c *ConfigureCmd) Run(cli *CLI) error { return cli.delegateToCobraSystem("configure", c.Args) }
114112
func (i *InitCmd) Run(cli *CLI) error { return cli.delegateToCobraSystem("init", i.Args) }
115113
func (u *UseCmd) Run(cli *CLI) error { return cli.delegateToCobraSystem("use", u.Args) }
116-
func (w *WhoamiCmd) Run(cli *CLI) error { return cli.delegateToCobraSystem("whoami", w.Args) }
117114

118115
// delegateToCobraSystem delegates execution to the legacy Cobra command system.
119116
// This is a temporary bridge during the Kong migration that ensures backwards compatibility
@@ -281,6 +278,10 @@ func isHelpRequest() bool {
281278
return false
282279
}
283280

281+
if len(os.Args) >= 2 && os.Args[1] == "whoami" {
282+
return false
283+
}
284+
284285
if len(os.Args) == 3 && (os.Args[2] == "-h" || os.Args[2] == "--help") {
285286
return true
286287
}

pkg/cmd/pipeline/create.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ func getRepoURLS(f *factory.Factory) []string {
139139
}
140140

141141
func getClusters(ctx context.Context, f *factory.Factory) (map[string]string, error) {
142-
143142
clusterMap := make(map[string]string) // map of cluster name to cluster ID
144143
page := 1
145144
per_page := 30
@@ -254,7 +253,6 @@ func initialisePipelineDryRun() PipelineDryRun {
254253
}
255254

256255
func createPipelineDryRun(ctx context.Context, f *factory.Factory, pipelineName, description, clusterID, repoURL string) error {
257-
258256
pipelineSlug := generateSlug(pipelineName)
259257

260258
pipelineSlug, err := getAvailablePipelineSlug(ctx, f, pipelineSlug, pipelineName)

pkg/cmd/root/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
useCmd "github.com/buildkite/cli/v3/pkg/cmd/use"
1515
"github.com/buildkite/cli/v3/pkg/cmd/user"
1616
versionCmd "github.com/buildkite/cli/v3/pkg/cmd/version"
17-
"github.com/buildkite/cli/v3/pkg/cmd/whoami"
1817
"github.com/spf13/cobra"
1918
)
2019

@@ -60,7 +59,6 @@ func NewCmdRoot(f *factory.Factory) (*cobra.Command, error) {
6059
cmd.AddCommand(promptCmd.NewCmdPrompt(f))
6160
cmd.AddCommand(useCmd.NewCmdUse(f))
6261
cmd.AddCommand(user.CommandUser(f))
63-
cmd.AddCommand(whoami.NewCmdWhoami(f))
6462
cmd.AddCommand(versionCmd.NewCmdVersion(f))
6563

6664
cmd.Flags().BoolP("version", "v", false, "Print the version number")

pkg/cmd/whoami/whoami.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)