Skip to content

Commit c0702fd

Browse files
committed
feat: support output format in stackit auth get-access-token command
1 parent a1859d6 commit c0702fd

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

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

Lines changed: 64 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
@@ -29,15 +44,61 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
2944
return &cliErr.SessionExpiredError{}
3045
}
3146

47+
// Try to get a valid access token, refreshing if necessary
48+
3249
// Try to get a valid access token, refreshing if necessary
3350
accessToken, err := auth.RefreshAccessToken(params.Printer)
3451
if err != nil {
3552
return err
3653
}
3754

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

0 commit comments

Comments
 (0)