Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions cmd/whoami/whoami.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package whoami

import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/alecthomas/kong"
"github.com/buildkite/cli/v3/internal/version"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/cli/v3/pkg/cmd/validation"
"github.com/buildkite/cli/v3/pkg/output"
"github.com/buildkite/go-buildkite/v4"
)

type WhoAmIOutput struct {
OrganizationSlug string `json:"organization_slug"`
Token buildkite.AccessToken `json:"token"`
}

func (w WhoAmIOutput) TextOutput() string {
b := strings.Builder{}

b.WriteString(fmt.Sprintf("Current organization: %s\n", w.OrganizationSlug))
b.WriteRune('\n')
b.WriteString(fmt.Sprintf("API Token UUID: %s\n", w.Token.UUID))
b.WriteString(fmt.Sprintf("API Token Description: %s\n", w.Token.Description))
b.WriteString(fmt.Sprintf("API Token Scopes: %v\n", w.Token.Scopes))
b.WriteRune('\n')
b.WriteString(fmt.Sprintf("API Token user name: %s\n", w.Token.User.Name))
b.WriteString(fmt.Sprintf("API Token user email: %s\n", w.Token.User.Email))

return b.String()
}

type WhoAmICmd struct {
Output string `help:"Output format. One of: json, yaml, text" short:"o" default:"${output_default_format}"`
}

func (c *WhoAmICmd) Help() string {
return `
It returns information on the current session.

Examples:
# List the current token session
$ bk whoami

# List the current token session in JSON format
$ bk whoami -o json
`
}

func (c *WhoAmICmd) Run(kongCtx *kong.Context) error {
f, err := factory.New(version.Version)
if err != nil {
return err
}

if err := validation.ValidateConfiguration(f.Config, kongCtx.Command()); err != nil {
return err
}

format := output.Format(c.Output)
if format != output.FormatJSON && format != output.FormatYAML && format != output.FormatText {
return fmt.Errorf("invalid output format: %s", c.Output)
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

orgSlug := f.Config.OrganizationSlug()

if orgSlug == "" {
orgSlug = "<None>"
}

token, _, err := f.RestAPIClient.AccessTokens.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get access token: %w", err)
}

w := WhoAmIOutput{
OrganizationSlug: orgSlug,
Token: token,
}

err = output.Write(os.Stdout, w, format)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}

return nil
}
37 changes: 19 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/buildkite/cli/v3/cmd/build"
"github.com/buildkite/cli/v3/cmd/cluster"
"github.com/buildkite/cli/v3/cmd/job"
"github.com/buildkite/cli/v3/cmd/whoami"
"github.com/buildkite/cli/v3/internal/cli"
bkErrors "github.com/buildkite/cli/v3/internal/errors"
"github.com/buildkite/cli/v3/internal/version"
Expand All @@ -27,20 +28,20 @@ type CLI struct {
Quiet bool `help:"Suppress progress output" short:"q"`
// Verbose bool `help:"Enable verbose error output" short:"V"` // TODO: Implement this, atm this is just a skeleton flag

Agent AgentCmd `cmd:"" help:"Manage agents"`
Api ApiCmd `cmd:"" help:"Interact with the Buildkite API"`
Artifacts ArtifactsCmd `cmd:"" help:"Manage pipeline build artifacts"`
Build BuildCmd `cmd:"" help:"Manage pipeline builds"`
Cluster ClusterCmd `cmd:"" help:"Manage organization clusters"`
Configure ConfigureCmd `cmd:"" help:"Configure Buildkite API token"`
Init InitCmd `cmd:"" help:"Initialize a pipeline.yaml file"`
Job JobCmd `cmd:"" help:"Manage jobs within a build"`
Pipeline PipelineCmd `cmd:"" help:"Manage pipelines"`
Package PackageCmd `cmd:"" help:"Manage packages"`
Use UseCmd `cmd:"" help:"Select an organization"`
User UserCmd `cmd:"" help:"Invite users to the organization"`
Version VersionCmd `cmd:"" help:"Print the version of the CLI being used"`
Whoami WhoamiCmd `cmd:"" help:"Print the current user and organization"`
Agent AgentCmd `cmd:"" help:"Manage agents"`
Api ApiCmd `cmd:"" help:"Interact with the Buildkite API"`
Artifacts ArtifactsCmd `cmd:"" help:"Manage pipeline build artifacts"`
Build BuildCmd `cmd:"" help:"Manage pipeline builds"`
Cluster ClusterCmd `cmd:"" help:"Manage organization clusters"`
Configure ConfigureCmd `cmd:"" help:"Configure Buildkite API token"`
Init InitCmd `cmd:"" help:"Initialize a pipeline.yaml file"`
Job JobCmd `cmd:"" help:"Manage jobs within a build"`
Pipeline PipelineCmd `cmd:"" help:"Manage pipelines"`
Package PackageCmd `cmd:"" help:"Manage packages"`
Use UseCmd `cmd:"" help:"Select an organization"`
User UserCmd `cmd:"" help:"Invite users to the organization"`
Version VersionCmd `cmd:"" help:"Print the version of the CLI being used"`
Whoami whoami.WhoAmICmd `cmd:"" help:"Print the current user and organization"`
}

// Hybrid delegation commands, we should delete from these when native Kong implementations ready
Expand Down Expand Up @@ -99,9 +100,6 @@ type (
UseCmd struct {
Args []string `arg:"" optional:"" passthrough:"all"`
}
WhoamiCmd struct {
Args []string `arg:"" optional:"" passthrough:"all"`
}
)

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

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

if len(os.Args) >= 2 && os.Args[1] == "whoami" {
return false
}

if len(os.Args) == 3 && (os.Args[2] == "-h" || os.Args[2] == "--help") {
return true
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func getRepoURLS(f *factory.Factory) []string {
}

func getClusters(ctx context.Context, f *factory.Factory) (map[string]string, error) {

clusterMap := make(map[string]string) // map of cluster name to cluster ID
page := 1
per_page := 30
Expand Down Expand Up @@ -254,7 +253,6 @@ func initialisePipelineDryRun() PipelineDryRun {
}

func createPipelineDryRun(ctx context.Context, f *factory.Factory, pipelineName, description, clusterID, repoURL string) error {

pipelineSlug := generateSlug(pipelineName)

pipelineSlug, err := getAvailablePipelineSlug(ctx, f, pipelineSlug, pipelineName)
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
useCmd "github.com/buildkite/cli/v3/pkg/cmd/use"
"github.com/buildkite/cli/v3/pkg/cmd/user"
versionCmd "github.com/buildkite/cli/v3/pkg/cmd/version"
"github.com/buildkite/cli/v3/pkg/cmd/whoami"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -60,7 +59,6 @@ func NewCmdRoot(f *factory.Factory) (*cobra.Command, error) {
cmd.AddCommand(promptCmd.NewCmdPrompt(f))
cmd.AddCommand(useCmd.NewCmdUse(f))
cmd.AddCommand(user.CommandUser(f))
cmd.AddCommand(whoami.NewCmdWhoami(f))
cmd.AddCommand(versionCmd.NewCmdVersion(f))

cmd.Flags().BoolP("version", "v", false, "Print the version number")
Expand Down
69 changes: 0 additions & 69 deletions pkg/cmd/whoami/whoami.go

This file was deleted.