Skip to content

Commit 3c66259

Browse files
chore(opensearch): adjust outputResult to accept inputModel
relates to STACKITTCLI-372
1 parent 22e38a6 commit 3c66259

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

internal/cmd/opensearch/credentials/create/create.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7676
return fmt.Errorf("create OpenSearch credentials: %w", err)
7777
}
7878

79-
return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, instanceLabel, resp)
79+
return outputResult(params.Printer, *model, instanceLabel, resp)
8080
},
8181
}
8282
configureFlags(cmd)
@@ -112,23 +112,26 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch.
112112
return req
113113
}
114114

115-
func outputResult(p *print.Printer, outputFormat string, showPassword bool, instanceLabel string, resp *opensearch.CredentialsResponse) error {
115+
func outputResult(p *print.Printer, model inputModel, instanceLabel string, resp *opensearch.CredentialsResponse) error {
116+
if model.GlobalFlagModel == nil {
117+
return fmt.Errorf("no global flags defined")
118+
}
116119
if resp == nil || resp.Raw == nil {
117120
return fmt.Errorf("response or response content is nil")
118121
}
119122

120-
if !showPassword {
123+
if !model.ShowPassword {
121124
resp.Raw.Credentials.Password = "hidden"
122125
}
123126

124-
return p.OutputResult(outputFormat, resp, func() error {
127+
return p.OutputResult(model.OutputFormat, resp, func() error {
125128
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, resp.Id)
126129
// The username field cannot be set by the user so we only display it if it's not returned empty
127130
if resp.HasRaw() {
128131
if username := resp.Raw.Credentials.Username; username != "" {
129132
p.Outputf("Username: %s\n", username)
130133
}
131-
if !showPassword {
134+
if !model.ShowPassword {
132135
p.Outputf("Password: <hidden>\n")
133136
} else {
134137
p.Outputf("Password: %s\n", resp.Raw.Credentials.Password)

internal/cmd/opensearch/credentials/create/create_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ func TestBuildRequest(t *testing.T) {
169169

170170
func TestOutputResult(t *testing.T) {
171171
type args struct {
172-
outputFormat string
173-
showPassword bool
172+
model inputModel
174173
instanceLabel string
175174
resp *opensearch.CredentialsResponse
176175
}
@@ -196,7 +195,10 @@ func TestOutputResult(t *testing.T) {
196195
{
197196
name: "empty response should not cause panic when password should be hidden",
198197
args: args{
199-
showPassword: false,
198+
model: inputModel{
199+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
200+
ShowPassword: false,
201+
},
200202
},
201203
wantErr: true,
202204
},
@@ -209,14 +211,17 @@ func TestOutputResult(t *testing.T) {
209211
},
210212
Uri: "https://opensearch.example.com",
211213
},
214+
model: inputModel{
215+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
216+
},
212217
},
213218
wantErr: false,
214219
},
215220
}
216221
params := testparams.NewTestParams()
217222
for _, tt := range tests {
218223
t.Run(tt.name, func(t *testing.T) {
219-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
224+
if err := outputResult(params.Printer, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
220225
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
221226
}
222227
})

0 commit comments

Comments
 (0)