|
| 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 | +} |
0 commit comments