|
| 1 | +package status |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 14 | +) |
| 15 | + |
| 16 | +type inputModel struct { |
| 17 | + *globalflags.GlobalFlagModel |
| 18 | +} |
| 19 | + |
| 20 | +type statusOutput struct { |
| 21 | + Authenticated bool `json:"authenticated"` |
| 22 | + Email string `json:"email,omitempty"` |
| 23 | + AuthFlow string `json:"auth_flow,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: "status", |
| 29 | + Short: "Shows authentication status for the STACKIT Terraform Provider and SDK", |
| 30 | + Long: "Shows authentication status for the STACKIT Terraform Provider and SDK, including whether you are authenticated and with which account.", |
| 31 | + Args: args.NoArgs, |
| 32 | + Example: examples.Build( |
| 33 | + examples.NewExample( |
| 34 | + `Show authentication status for the STACKIT Terraform Provider and SDK`, |
| 35 | + "$ stackit auth api status"), |
| 36 | + ), |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + model, err := parseInput(params.Printer, cmd, args) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + // Check if access token exists (primary credential check) |
| 44 | + accessToken, err := auth.GetAuthFieldWithContext(auth.StorageContextAPI, auth.ACCESS_TOKEN) |
| 45 | + if err != nil || accessToken == "" { |
| 46 | + // Not authenticated |
| 47 | + return outputStatus(params.Printer, model, statusOutput{ |
| 48 | + Authenticated: false, |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + // Get optional fields for display |
| 53 | + flow, _ := auth.GetAuthFlowWithContext(auth.StorageContextAPI) |
| 54 | + email, err := auth.GetAuthFieldWithContext(auth.StorageContextAPI, auth.USER_EMAIL) |
| 55 | + if err != nil { |
| 56 | + email = "" |
| 57 | + } |
| 58 | + |
| 59 | + return outputStatus(params.Printer, model, statusOutput{ |
| 60 | + Authenticated: true, |
| 61 | + Email: email, |
| 62 | + AuthFlow: string(flow), |
| 63 | + }) |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + // hide project id flag from help command because it could mislead users |
| 68 | + cmd.SetHelpFunc(func(command *cobra.Command, strings []string) { |
| 69 | + _ = command.Flags().MarkHidden(globalflags.ProjectIdFlag) // nolint:errcheck // there's no chance to handle the error here |
| 70 | + command.Parent().HelpFunc()(command, strings) |
| 71 | + }) |
| 72 | + |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 77 | + globalFlags := globalflags.Parse(p, cmd) |
| 78 | + |
| 79 | + model := inputModel{ |
| 80 | + GlobalFlagModel: globalFlags, |
| 81 | + } |
| 82 | + |
| 83 | + p.DebugInputModel(model) |
| 84 | + return &model, nil |
| 85 | +} |
| 86 | + |
| 87 | +func outputStatus(p *print.Printer, model *inputModel, status statusOutput) error { |
| 88 | + switch model.OutputFormat { |
| 89 | + case print.JSONOutputFormat: |
| 90 | + details, err := json.MarshalIndent(status, "", " ") |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("marshal status: %w", err) |
| 93 | + } |
| 94 | + p.Outputln(string(details)) |
| 95 | + return nil |
| 96 | + default: |
| 97 | + if status.Authenticated { |
| 98 | + p.Outputln("API Authentication Status: Authenticated") |
| 99 | + if status.Email != "" { |
| 100 | + p.Outputf("Email: %s\n", status.Email) |
| 101 | + } |
| 102 | + p.Outputf("Auth Flow: %s\n", status.AuthFlow) |
| 103 | + } else { |
| 104 | + p.Outputln("API Authentication Status: Not authenticated") |
| 105 | + p.Outputln("\nTo authenticate, run: stackit auth api login") |
| 106 | + } |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
0 commit comments