Skip to content
Open
Show file tree
Hide file tree
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
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
})
}
3 changes: 2 additions & 1 deletion internal/cmd/beta/sfs/resource-pool/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
resp *sfs.CreateResourcePoolResponse
}
Expand Down Expand Up @@ -287,7 +288,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, tt.args.async, 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
})
}
3 changes: 2 additions & 1 deletion internal/cmd/beta/sfs/resource-pool/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
resourcePoolName string
response map[string]interface{}
}
Expand All @@ -201,7 +202,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, tt.args.async, 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
})
}
3 changes: 2 additions & 1 deletion internal/cmd/beta/sfs/resource-pool/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
resp *sfs.UpdateResourcePoolResponse
}
tests := []struct {
Expand Down Expand Up @@ -353,7 +354,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, tt.args.async, 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(),
resp: &git.Instance{},
},
wantErr: false,
Expand Down
23 changes: 3 additions & 20 deletions internal/cmd/kms/key/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package create

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -159,29 +157,14 @@ func outputResult(p *print.Printer, model *inputModel, resp *kms.Key) error {
return fmt.Errorf("response is nil")
}

switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal KMS key: %w", err)
}
p.Outputln(string(details))

case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal KMS key: %w", err)
}
p.Outputln(string(details))

default:
return p.OutputResult(model.OutputFormat, resp, func() error {
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s the KMS key %q. Key ID: %s\n", operationState, utils.PtrString(resp.DisplayName), utils.PtrString(resp.Id))
}
return nil
return nil
})
}

func configureFlags(cmd *cobra.Command) {
Expand Down
22 changes: 3 additions & 19 deletions internal/cmd/kms/key/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package delete

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -127,22 +125,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
return fmt.Errorf("response is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal output to JSON: %w", err)
}
p.Outputln(string(details))
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal output to YAML: %w", err)
}
p.Outputln(string(details))

default:
return p.OutputResult(outputFormat, resp, func() error {
p.Outputf("Deletion of KMS key %s scheduled successfully for the deletion date: %s\n", utils.PtrString(resp.DisplayName), utils.PtrString(resp.DeletionDate))
}
return nil
return nil
})
}
24 changes: 3 additions & 21 deletions internal/cmd/kms/key/importKey/importKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package importKey
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -148,26 +146,10 @@ func outputResult(p *print.Printer, outputFormat, keyRingName, keyName string, r
return fmt.Errorf("response is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal KMS key: %w", err)
}
p.Outputln(string(details))

case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal KMS key: %w", err)
}
p.Outputln(string(details))

default:
return p.OutputResult(outputFormat, resp, func() error {
p.Outputf("Imported a new version for the key %q inside the key ring %q\n", keyName, keyRingName)
}

return nil
return nil
})
}

func configureFlags(cmd *cobra.Command) {
Expand Down
23 changes: 3 additions & 20 deletions internal/cmd/kms/key/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package list

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -106,22 +104,7 @@ func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, r

keys := *resp.Keys

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(keys, "", " ")
if err != nil {
return fmt.Errorf("marshal KMS Keys list: %w", err)
}
p.Outputln(string(details))

case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(keys, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal KMS Keys list: %w", err)
}
p.Outputln(string(details))

default:
return p.OutputResult(outputFormat, keys, func() error {
if len(keys) == 0 {
p.Outputf("No keys found for project %q under the key ring %q\n", projectId, keyRingId)
return nil
Expand All @@ -144,6 +127,6 @@ func outputResult(p *print.Printer, outputFormat, projectId, keyRingId string, r
if err != nil {
return fmt.Errorf("render table: %w", err)
}
}
return nil
return nil
})
}
22 changes: 3 additions & 19 deletions internal/cmd/kms/key/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package restore

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -126,22 +124,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *kms.Key) error {
return fmt.Errorf("response is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal output to JSON: %w", err)
}
p.Outputln(string(details))
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal output to YAML: %w", err)
}
p.Outputln(string(details))

default:
return p.OutputResult(outputFormat, resp, func() error {
p.Outputf("Successfully restored KMS key %q\n", utils.PtrString(resp.DisplayName))
}
return nil
return nil
})
}
Loading
Loading