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