Skip to content

Commit 8ce4706

Browse files
authored
feat(auth): support output format in stackit auth get-access-token command (#889)
relates to #888
1 parent a1859d6 commit 8ce4706

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

internal/cmd/auth/get-access-token/get_access_token.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package getaccesstoken
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/goccy/go-yaml"
48
"github.com/spf13/cobra"
59
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
610
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
711
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
812
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
913
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1016
)
1117

18+
type inputModel struct {
19+
*globalflags.GlobalFlagModel
20+
}
21+
1222
func NewCmd(params *params.CmdParams) *cobra.Command {
1323
cmd := &cobra.Command{
1424
Use: "get-access-token",
@@ -20,7 +30,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
2030
`Print a short-lived access token`,
2131
"$ stackit auth get-access-token"),
2232
),
23-
RunE: func(_ *cobra.Command, _ []string) error {
33+
RunE: func(cmd *cobra.Command, _ []string) error {
34+
model, err := parseInput(params.Printer, cmd)
35+
if err != nil {
36+
return err
37+
}
38+
2439
userSessionExpired, err := auth.UserSessionExpired()
2540
if err != nil {
2641
return err
@@ -35,9 +50,52 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3550
return err
3651
}
3752

38-
params.Printer.Outputf("%s\n", accessToken)
39-
return nil
53+
switch model.OutputFormat {
54+
case print.JSONOutputFormat:
55+
details, err := json.MarshalIndent(map[string]string{
56+
"access_token": accessToken,
57+
}, "", " ")
58+
if err != nil {
59+
return fmt.Errorf("marshal image list: %w", err)
60+
}
61+
params.Printer.Outputln(string(details))
62+
63+
return nil
64+
case print.YAMLOutputFormat:
65+
details, err := yaml.MarshalWithOptions(map[string]string{
66+
"access_token": accessToken,
67+
}, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
68+
if err != nil {
69+
return fmt.Errorf("marshal image list: %w", err)
70+
}
71+
params.Printer.Outputln(string(details))
72+
73+
return nil
74+
default:
75+
params.Printer.Outputln(accessToken)
76+
77+
return nil
78+
}
4079
},
4180
}
4281
return cmd
4382
}
83+
84+
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
85+
globalFlags := globalflags.Parse(p, cmd)
86+
87+
model := inputModel{
88+
GlobalFlagModel: globalFlags,
89+
}
90+
91+
if p.IsVerbosityDebug() {
92+
modelStr, err := print.BuildDebugStrFromInputModel(model)
93+
if err != nil {
94+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
95+
} else {
96+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
97+
}
98+
}
99+
100+
return &model, nil
101+
}

0 commit comments

Comments
 (0)