Skip to content

feat(auth): support output format in stackit auth get-access-token command #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions internal/cmd/auth/get-access-token/get_access_token.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package getaccesstoken

import (
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"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"
)

type inputModel struct {
*globalflags.GlobalFlagModel
}

func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "get-access-token",
Expand All @@ -20,7 +30,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
`Print a short-lived access token`,
"$ stackit auth get-access-token"),
),
RunE: func(_ *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}

userSessionExpired, err := auth.UserSessionExpired()
if err != nil {
return err
Expand All @@ -35,9 +50,52 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
return err
}

params.Printer.Outputf("%s\n", accessToken)
return nil
switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(map[string]string{
"access_token": accessToken,
}, "", " ")
if err != nil {
return fmt.Errorf("marshal image list: %w", err)
}
params.Printer.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(map[string]string{
"access_token": accessToken,
}, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal image list: %w", err)
}
params.Printer.Outputln(string(details))

return nil
default:
params.Printer.Outputln(accessToken)

return nil
}
},
}
return cmd
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)

model := inputModel{
GlobalFlagModel: globalFlags,
}

if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}

return &model, nil
}
Loading