Skip to content
10 changes: 7 additions & 3 deletions internal/cmd/beta/kms/version/disable/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
params.Printer.Debug(print.ErrorLevel, "get key version: %v", err)
}

return outputResult(params.Printer, model.OutputFormat, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, resp)
},
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func configureFlags(cmd *cobra.Command) {
cobra.CheckErr(err)
}

func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) error {
func outputResult(p *print.Printer, outputFormat string, async bool, resp *kms.Version) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
Expand All @@ -154,7 +154,11 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) erro
p.Outputln(string(details))

default:
p.Outputf("Disabled version %d of the key %q\n", utils.PtrValue(resp.Number), utils.PtrValue(resp.KeyId))
operationState := "Disabled"
if async {
operationState = "Triggered disable of"
}
p.Outputf("%s version %d of the key %q\n", operationState, utils.PtrValue(resp.Number), utils.PtrValue(resp.KeyId))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/kms/version/disable/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := outputResult(p, tt.outputFormat, tt.resp)
err := outputResult(p, tt.outputFormat, false, tt.resp)
if (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/beta/kms/version/enable/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
params.Printer.Debug(print.ErrorLevel, "get key version: %v", err)
}

return outputResult(params.Printer, model.OutputFormat, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, resp)
},
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func configureFlags(cmd *cobra.Command) {
cobra.CheckErr(err)
}

func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) error {
func outputResult(p *print.Printer, outputFormat string, async bool, resp *kms.Version) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
Expand All @@ -154,7 +154,11 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Version) erro
p.Outputln(string(details))

default:
p.Outputf("Enabled version %d of the key %q\n", utils.PtrValue(resp.Number), utils.PtrValue(resp.KeyId))
operationState := "Enabled"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not your fault - But this switch case in all these KMS commands isn't neccessary. Maybe you can refactor it to also use p.OutputResult? 😅

func (p *Printer) OutputResult(outputFormat string, output any, prettyOutputFunc func() error) error {
switch outputFormat {
case JSONOutputFormat:
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err := encoder.Encode(output)
if err != nil {
return fmt.Errorf("marshal json: %w", err)
}
details := buffer.Bytes()
p.Outputln(string(details))
return nil
case YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(output, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal yaml: %w", err)
}
p.Outputln(string(details))
return nil
default:
return prettyOutputFunc()
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to do that.

if async {
operationState = "Triggered enable of"
}
p.Outputf("%s version %d of the key %q\n", operationState, utils.PtrValue(resp.Number), utils.PtrValue(resp.KeyId))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/kms/version/enable/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := outputResult(p, tt.outputFormat, tt.resp)
err := outputResult(p, tt.outputFormat, false, tt.resp)
if (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/beta/sfs/resource-pool/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ The available performance class values can be obtained by running:
s.Stop()
}

return outputResult(params.Printer, model.OutputFormat, projectLabel, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -170,13 +170,17 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
return &model, nil
}

func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *sfs.CreateResourcePoolResponse) error {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, resp *sfs.CreateResourcePoolResponse) error {
return p.OutputResult(outputFormat, resp, func() error {
if resp == nil || resp.ResourcePool == nil {
p.Outputln("Resource pool response is empty")
return nil
}
p.Outputf("Created resource pool for project %q. Resource pool ID: %s\n", projectLabel, utils.PtrString(resp.ResourcePool.Id))
operationState := "Created"
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s resource pool for project %q. Resource pool ID: %s\n", operationState, projectLabel, utils.PtrString(resp.ResourcePool.Id))
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, false, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/beta/sfs/resource-pool/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
s.Stop()
}

return outputResult(params.Printer, model.OutputFormat, resourcePoolName, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, resourcePoolName, resp)
},
}
return cmd
Expand Down Expand Up @@ -110,9 +110,13 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
return &model, nil
}

func outputResult(p *print.Printer, outputFormat, resourcePoolName string, response map[string]interface{}) error {
func outputResult(p *print.Printer, outputFormat string, async bool, resourcePoolName string, response map[string]interface{}) error {
return p.OutputResult(outputFormat, response, func() error {
p.Outputf("Deleted resource pool %q\n", resourcePoolName)
operationState := "Deleted"
if async {
operationState = "Triggered deletion of"
}
p.Outputf("%s resource pool %q\n", operationState, resourcePoolName)
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.resourcePoolName, tt.args.response); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, false, tt.args.resourcePoolName, tt.args.response); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/beta/sfs/resource-pool/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The available performance class values can be obtained by running:
s.Stop()
}

return outputResult(params.Printer, model.OutputFormat, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -165,13 +165,17 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
return &model, nil
}

func outputResult(p *print.Printer, outputFormat string, resp *sfs.UpdateResourcePoolResponse) error {
func outputResult(p *print.Printer, outputFormat string, async bool, resp *sfs.UpdateResourcePoolResponse) error {
return p.OutputResult(outputFormat, resp, func() error {
if resp == nil || resp.ResourcePool == nil {
p.Outputln("Resource pool response is empty")
return nil
}
p.Outputf("Updated resource pool %s\n", utils.PtrString(resp.ResourcePool.Name))
operationState := "Updated"
if async {
operationState = "Triggered update of"
}
p.Outputf("%s resource pool %s\n", operationState, utils.PtrString(resp.ResourcePool.Name))
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, false, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
13 changes: 12 additions & 1 deletion internal/cmd/git/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,18 @@ func outputResult(p *print.Printer, model *inputModel, resp *git.Instance) error
}

return p.OutputResult(outputFormat, resp, func() error {
p.Outputf("Created instance %q with id %s\n", model.Name, utils.PtrString(model.Id))
if resp == nil {
return nil
}
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
id := utils.PtrString(model.Id)
if resp.Id != nil {
id = *resp.Id
}
p.Outputf("%s instance %q with id %s\n", operationState, model.Name, id)
return nil
})
}
2 changes: 1 addition & 1 deletion internal/cmd/git/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestOutputResult(t *testing.T) {
{
name: "empty input",
args: args{
model: &inputModel{},
model: fixtureInputModel(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change that? 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that model.GlobalFlagModel.Async is not nil. It would panic because of "if model.Async" in ‎internal/cmd/git/instance/create/create.go:160

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was the pattern for tests, where the GlobalFlagModel pointer should not be nil.

resp: &git.Instance{},
},
wantErr: false,
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/mongodbflex/backup/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
s.Stop()
}

params.Printer.Outputf("Restored instance %q with backup %q\n", model.InstanceId, model.BackupId)
operationState := "Restored"
if model.Async {
operationState = "Triggered restore of"
}
params.Printer.Outputf("%s instance %q with backup %q\n", operationState, model.InstanceId, model.BackupId)
return nil
}

Expand All @@ -129,7 +133,11 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
s.Stop()
}

params.Printer.Outputf("Cloned instance %q from backup with timestamp %q\n", model.InstanceId, model.Timestamp)
operationState := "Cloned"
if model.Async {
operationState = "Triggered clone of"
}
params.Printer.Outputf("%s instance %q from backup with timestamp %q\n", operationState, model.InstanceId, model.Timestamp)
return nil
},
}
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/network-area/region/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
s.Stop()
}

return outputResult(params.Printer, model.OutputFormat, model.Region, networkAreaLabel, *resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, model.Region, networkAreaLabel, *resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -186,9 +186,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.CreateNetworkAreaRegionPayload(payload)
}

func outputResult(p *print.Printer, outputFormat, region, networkAreaLabel string, regionalArea iaas.RegionalArea) error {
func outputResult(p *print.Printer, outputFormat string, async bool, region, networkAreaLabel string, regionalArea iaas.RegionalArea) error {
return p.OutputResult(outputFormat, regionalArea, func() error {
p.Outputf("Create region configuration for SNA %q.\nRegion: %s\n", networkAreaLabel, region)
operationState := "Created"
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s region configuration for SNA %q.\nRegion: %s\n", operationState, networkAreaLabel, region)
return nil
})
}
3 changes: 2 additions & 1 deletion internal/cmd/network-area/region/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func TestBuildRequest(t *testing.T) {
func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
region string
networkAreaLabel string
regionalArea iaas.RegionalArea
Expand Down Expand Up @@ -300,7 +301,7 @@ func Test_outputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.region, tt.args.networkAreaLabel, tt.args.regionalArea); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.region, tt.args.networkAreaLabel, tt.args.regionalArea); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/server/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
s.Stop()
}

return outputResult(params.Printer, model.OutputFormat, projectLabel, resp)
return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -322,12 +322,16 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.CreateServerPayload(payload)
}

func outputResult(p *print.Printer, outputFormat, projectLabel string, server *iaas.Server) error {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, server *iaas.Server) error {
if server == nil {
return fmt.Errorf("server response is empty")
}
return p.OutputResult(outputFormat, server, func() error {
p.Outputf("Created server for project %q.\nServer ID: %s\n", projectLabel, utils.PtrString(server.Id))
operationState := "Created"
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s server for project %q.\nServer ID: %s\n", operationState, projectLabel, utils.PtrString(server.Id))
return nil
})
}
3 changes: 2 additions & 1 deletion internal/cmd/server/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to have it like this in all the other tests so that you don't just hardcode it to false. But that's just an optional nitpick I would say 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point! I agree and will update the tests to be consistent.

projectLabel string
server *iaas.Server
}
Expand All @@ -407,7 +408,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.server); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.server); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/volume/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, volu
return fmt.Errorf("volume response is empty")
}
return p.OutputResult(model.OutputFormat, volume, func() error {
p.Outputf("Created volume for project %q.\nVolume ID: %s\n", projectLabel, utils.PtrString(volume.Id))
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s volume for project %q.\nVolume ID: %s\n", operationState, projectLabel, utils.PtrString(volume.Id))
return nil
})
}
Loading