Skip to content

Commit dfe6349

Browse files
committed
feature: implement calling service and output of results for list
1 parent abd0911 commit dfe6349

File tree

1 file changed

+53
-8
lines changed
  • internal/cmd/auth/security_group/list

1 file changed

+53
-8
lines changed

internal/cmd/auth/security_group/list/list.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package list
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"strings"
68

9+
"github.com/goccy/go-yaml"
710
"github.com/spf13/cobra"
811
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
912
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -13,6 +16,7 @@ import (
1316
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1417
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1518
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1620
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1721
)
1822

@@ -65,19 +69,17 @@ func executeList(cmd *cobra.Command, p *print.Printer, _ []string) error {
6569
}
6670

6771
// Call API
68-
req := buildRequest(ctx, model, apiClient)
69-
_, err = req.Execute()
72+
request := buildRequest(ctx, model, apiClient)
73+
response, err := request.Execute()
7074
if err != nil {
7175
return fmt.Errorf("list security group: %w", err)
7276
}
73-
74-
operationState := "Enabled"
75-
if model.Async {
76-
operationState = "Triggered enablement of"
77+
if items := response.GetItems(); items == nil || len(*items) > 0 {
78+
p.Info("no security groups found for %q", projectLabel)
79+
} else {
80+
outputResult(p, model.OutputFormat, *items)
7781
}
78-
p.Info("%s security group for %q\n", operationState, projectLabel)
7982

80-
panic("todo: implement client invocation and output")
8183
return nil
8284
}
8385

@@ -112,3 +114,46 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
112114
return request
113115

114116
}
117+
func outputResult(p *print.Printer, outputFormat string, items []iaas.SecurityGroup) error {
118+
switch outputFormat {
119+
case print.JSONOutputFormat:
120+
details, err := json.MarshalIndent(items, "", " ")
121+
if err != nil {
122+
return fmt.Errorf("marshal PostgreSQL Flex instance list: %w", err)
123+
}
124+
p.Outputln(string(details))
125+
126+
return nil
127+
case print.YAMLOutputFormat:
128+
details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true))
129+
if err != nil {
130+
return fmt.Errorf("marshal PostgreSQL Flex instance list: %w", err)
131+
}
132+
p.Outputln(string(details))
133+
134+
return nil
135+
default:
136+
table := tables.NewTable()
137+
table.SetHeader("ID", "NAME", "LABELS", "STATEFUL")
138+
for _, item := range items {
139+
table.AddRow(item.Id, item.Name, concatLabels(item.Labels), item.Stateful)
140+
}
141+
err := table.Display(p)
142+
if err != nil {
143+
return fmt.Errorf("render table: %w", err)
144+
}
145+
146+
return nil
147+
}
148+
}
149+
150+
func concatLabels(item *map[string]any) string {
151+
if item == nil {
152+
return ""
153+
}
154+
var builder strings.Builder
155+
for k, v := range *item {
156+
builder.WriteString(fmt.Sprintf("%s=%v ", k, v))
157+
}
158+
return builder.String()
159+
}

0 commit comments

Comments
 (0)