Skip to content
Merged
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
2 changes: 1 addition & 1 deletion internal/cmd/beta/image/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func outputResult(p *print.Printer, model *inputModel, resp *iaas.ImageCreateRes

return nil
default:
p.Outputf("Created image %q with id %s\n", model.Name, *model.Id)
p.Outputf("Created image %q with id %s\n", model.Name, utils.PtrString(model.Id))
return nil
}
}
15 changes: 11 additions & 4 deletions internal/cmd/beta/key-pair/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create key pair: %w", err)
}

return outputResult(p, model, resp)
return outputResult(p, model.GlobalFlagModel.OutputFormat, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -141,8 +141,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.CreateKeyPairPayload(payload)
}

func outputResult(p *print.Printer, model *inputModel, item *iaas.Keypair) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, item *iaas.Keypair) error {
if item == nil {
return fmt.Errorf("no key pair found")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(item, "", " ")
if err != nil {
Expand All @@ -156,7 +160,10 @@ func outputResult(p *print.Printer, model *inputModel, item *iaas.Keypair) error
}
p.Outputln(string(details))
default:
p.Outputf("Created key pair %q.\nkey pair Fingerprint: %q\n", *item.Name, *item.Fingerprint)
p.Outputf("Created key pair %q.\nkey pair Fingerprint: %q\n",
utils.PtrString(item.Name),
utils.PtrString(item.Fingerprint),
)
}
return nil
}
39 changes: 39 additions & 0 deletions internal/cmd/beta/key-pair/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,42 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
item *iaas.Keypair
outputFormat string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
item: nil,
outputFormat: "",
},
wantErr: true,
},
{
name: "base",
args: args{
item: &iaas.Keypair{},
outputFormat: "",
},
wantErr: false,
},
}

p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
26 changes: 18 additions & 8 deletions internal/cmd/beta/key-pair/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strings"

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

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
Expand Down Expand Up @@ -69,7 +71,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("read key pair: %w", err)
}

return outputResult(p, model.OutputFormat, model.PublicKey, resp)
if keypair := resp; keypair != nil {
return outputResult(p, model.OutputFormat, model.PublicKey, *keypair)
}
p.Outputln("No keypair found.")
return nil
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -107,7 +113,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return apiClient.GetKeyPair(ctx, model.KeyPairName)
}

func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool, keyPair *iaas.Keypair) error {
func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool, keyPair iaas.Keypair) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(keyPair, "", " ")
Expand Down Expand Up @@ -145,10 +151,10 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
return nil
}
table := tables.NewTable()
table.AddRow("KEY PAIR NAME", *keyPair.Name)
table.AddRow("KEY PAIR NAME", utils.PtrString(keyPair.Name))
table.AddSeparator()

if *keyPair.Labels != nil && len(*keyPair.Labels) > 0 {
if keyPair.Labels != nil && len(*keyPair.Labels) > 0 {
var labels []string
for key, value := range *keyPair.Labels {
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
Expand All @@ -157,17 +163,21 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
table.AddSeparator()
}

table.AddRow("FINGERPRINT", *keyPair.Fingerprint)
table.AddRow("FINGERPRINT", utils.PtrString(keyPair.Fingerprint))
table.AddSeparator()

truncatedPublicKey := (*keyPair.PublicKey)[:maxLengthPublicKey] + "..."
truncatedPublicKey := ""
if keyPair.PublicKey != nil {
truncatedPublicKey = (*keyPair.PublicKey)[:maxLengthPublicKey] + "..."
}

table.AddRow("PUBLIC KEY", truncatedPublicKey)
table.AddSeparator()

table.AddRow("CREATED AT", *keyPair.CreatedAt)
table.AddRow("CREATED AT", utils.PtrString(keyPair.CreatedAt))
table.AddSeparator()

table.AddRow("UPDATED AT", *keyPair.UpdatedAt)
table.AddRow("UPDATED AT", utils.PtrString(keyPair.UpdatedAt))
table.AddSeparator()

p.Outputln(table.Render())
Expand Down
32 changes: 32 additions & 0 deletions internal/cmd/beta/key-pair/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,35 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
showOnlyPublicKey bool
keyPair iaas.Keypair
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "base",
args: args{
outputFormat: "",
showOnlyPublicKey: false,
keyPair: iaas.Keypair{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.showOnlyPublicKey, tt.args.keyPair); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
16 changes: 13 additions & 3 deletions internal/cmd/beta/key-pair/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strings"

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

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand Down Expand Up @@ -158,11 +160,19 @@ func outputResult(p *print.Printer, outputFormat string, keyPairs []iaas.Keypair
keyPair := keyPairs[idx]

var labels []string
for key, value := range *keyPair.Labels {
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
if keyPair.Labels != nil {
for key, value := range *keyPair.Labels {
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
}
}

table.AddRow(*keyPair.Name, strings.Join(labels, ", "), *keyPair.Fingerprint, *keyPair.CreatedAt, *keyPair.UpdatedAt)
table.AddRow(
utils.PtrString(keyPair.Name),
strings.Join(labels, ", "),
utils.PtrString(keyPair.Fingerprint),
utils.PtrString(keyPair.CreatedAt),
utils.PtrString(keyPair.UpdatedAt),
)
}

p.Outputln(table.Render())
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/beta/key-pair/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
keyPairs []iaas.Keypair
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
outputFormat: "",
keyPairs: []iaas.Keypair{
{},
},
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := print.NewPrinter()
p.Cmd = NewCmd(p)

if err := outputResult(p, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
26 changes: 15 additions & 11 deletions internal/cmd/beta/key-pair/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
if err != nil {
return err
}
model := parseInput(p, cmd, args)

// Configure API client
apiClient, err := client.ConfigureClient(p)
Expand All @@ -63,13 +60,16 @@ func NewCmd(p *print.Printer) *cobra.Command {
}

// Call API
req := buildRequest(ctx, model, apiClient)
req := buildRequest(ctx, &model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("update key pair: %w", err)
}
if resp == nil {
return fmt.Errorf("response is nil")
}

return outputResult(p, model, resp)
return outputResult(p, model, *resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -100,7 +100,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.UpdateKeyPairPayload(payload)
}

func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) inputModel {
keyPairName := inputArgs[0]
globalFlags := globalflags.Parse(p, cmd)

Expand All @@ -119,11 +119,15 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
}
}

return &model, nil
return model
}

func outputResult(p *print.Printer, model *inputModel, keyPair *iaas.Keypair) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, model inputModel, keyPair iaas.Keypair) error {
var outputFormat string
if model.GlobalFlagModel != nil {
outputFormat = model.GlobalFlagModel.OutputFormat
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(keyPair, "", " ")
if err != nil {
Expand All @@ -137,7 +141,7 @@ func outputResult(p *print.Printer, model *inputModel, keyPair *iaas.Keypair) er
}
p.Outputln(string(details))
default:
p.Outputf("Updated labels of key pair %q\n", *model.KeyPairName)
p.Outputf("Updated labels of key pair %q\n", utils.PtrString(model.KeyPairName))
}
return nil
}
45 changes: 37 additions & 8 deletions internal/cmd/beta/key-pair/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,12 @@ func TestParseInput(t *testing.T) {
t.Fatalf("error validating args: %v", err)
}

model, err := parseInput(p, cmd, tt.argValues)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error parsing flags: %v", err)
}
model := parseInput(p, cmd, tt.argValues)

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
diff := cmp.Diff(&model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
Expand Down Expand Up @@ -188,3 +182,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
keyPair iaas.Keypair
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "base",
args: args{
model: inputModel{},
keyPair: iaas.Keypair{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.model, tt.args.keyPair); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading