Skip to content

Commit e6adb93

Browse files
authored
add json and yaml output formats to the argus credentials create (#379)
1 parent dc1d321 commit e6adb93

File tree

1 file changed

+34
-9
lines changed
  • internal/cmd/argus/credentials/create

1 file changed

+34
-9
lines changed

internal/cmd/argus/credentials/create/create.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package create
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

8+
"github.com/goccy/go-yaml"
79
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
810
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
911
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -77,15 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7779
return fmt.Errorf("create credentials for Argus instance: %w", err)
7880
}
7981

80-
p.Outputf("Created credentials for instance %q.\n\n", instanceLabel)
81-
// The username field cannot be set by the user so we only display it if it's not returned empty
82-
username := *resp.Credentials.Username
83-
if username != "" {
84-
p.Outputf("Username: %s\n", username)
85-
}
86-
87-
p.Outputf("Password: %s\n", *resp.Credentials.Password)
88-
return nil
82+
return outputResult(p, model, instanceLabel, resp)
8983
},
9084
}
9185
configureFlags(cmd)
@@ -115,3 +109,34 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl
115109
req := apiClient.CreateCredentials(ctx, model.InstanceId, model.ProjectId)
116110
return req
117111
}
112+
113+
func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *argus.CreateCredentialsResponse) error {
114+
switch model.OutputFormat {
115+
case print.JSONOutputFormat:
116+
details, err := json.MarshalIndent(resp, "", " ")
117+
if err != nil {
118+
return fmt.Errorf("marshal Argus credentials: %w", err)
119+
}
120+
p.Outputln(string(details))
121+
122+
return nil
123+
case print.YAMLOutputFormat:
124+
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true))
125+
if err != nil {
126+
return fmt.Errorf("marshal Argus credentials: %w", err)
127+
}
128+
p.Outputln(string(details))
129+
130+
return nil
131+
default:
132+
p.Outputf("Created credentials for instance %q.\n\n", instanceLabel)
133+
// The username field cannot be set by the user so we only display it if it's not returned empty
134+
username := *resp.Credentials.Username
135+
if username != "" {
136+
p.Outputf("Username: %s\n", username)
137+
}
138+
139+
p.Outputf("Password: %s\n", *resp.Credentials.Password)
140+
return nil
141+
}
142+
}

0 commit comments

Comments
 (0)