|
| 1 | +package getaccesstoken |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 6 | + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" |
| 7 | + cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 12 | +) |
| 13 | + |
| 14 | +type inputModel struct { |
| 15 | + *globalflags.GlobalFlagModel |
| 16 | +} |
| 17 | + |
| 18 | +func NewCmd(p *types.CmdParams) *cobra.Command { |
| 19 | + cmd := &cobra.Command{ |
| 20 | + Use: "get-access-token", |
| 21 | + Short: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK", |
| 22 | + Long: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK which can be used e.g. for API calls.", |
| 23 | + Args: args.NoArgs, |
| 24 | + Example: examples.Build( |
| 25 | + examples.NewExample( |
| 26 | + `Print a short-lived access token for the STACKIT Terraform Provider and SDK`, |
| 27 | + "$ stackit auth api get-access-token"), |
| 28 | + ), |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + model, err := parseInput(p.Printer, cmd, args) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + userSessionExpired, err := auth.UserSessionExpiredWithContext(auth.StorageContextAPI) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if userSessionExpired { |
| 40 | + return &cliErr.SessionExpiredError{} |
| 41 | + } |
| 42 | + |
| 43 | + accessToken, err := auth.GetValidAccessTokenWithContext(p.Printer, auth.StorageContextAPI) |
| 44 | + if err != nil { |
| 45 | + p.Printer.Debug(print.ErrorLevel, "get valid access token: %v", err) |
| 46 | + return &cliErr.SessionExpiredError{} |
| 47 | + } |
| 48 | + |
| 49 | + result := map[string]string{ |
| 50 | + "access_token": accessToken, |
| 51 | + } |
| 52 | + return p.Printer.OutputResult(model.OutputFormat, result, func() error { |
| 53 | + p.Printer.Outputln(accessToken) |
| 54 | + return nil |
| 55 | + }) |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + // hide project id flag from help command because it could mislead users |
| 60 | + cmd.SetHelpFunc(func(command *cobra.Command, strings []string) { |
| 61 | + cobra.CheckErr(command.Flags().MarkHidden(globalflags.ProjectIdFlag)) |
| 62 | + command.Parent().HelpFunc()(command, strings) |
| 63 | + }) |
| 64 | + |
| 65 | + return cmd |
| 66 | +} |
| 67 | + |
| 68 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 69 | + globalFlags := globalflags.Parse(p, cmd) |
| 70 | + |
| 71 | + model := inputModel{ |
| 72 | + GlobalFlagModel: globalFlags, |
| 73 | + } |
| 74 | + |
| 75 | + p.DebugInputModel(model) |
| 76 | + return &model, nil |
| 77 | +} |
0 commit comments