Skip to content

Commit 8bc2e14

Browse files
author
Yago Carlos Fernandez Gou
committed
address review comments
1 parent e9887a4 commit 8bc2e14

File tree

7 files changed

+116
-19
lines changed

7 files changed

+116
-19
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestParseLabels(t *testing.T) {
10+
tests := []struct {
11+
description string
12+
input string
13+
expectedMap map[string]string
14+
expectError bool
15+
}{
16+
{
17+
description: "single label",
18+
input: "key1=val1",
19+
expectedMap: map[string]string{"key1": "val1"},
20+
expectError: false,
21+
},
22+
{
23+
description: "multiple labels",
24+
input: "key1=val1,key2=val2",
25+
expectedMap: map[string]string{"key1": "val1", "key2": "val2"},
26+
expectError: false,
27+
},
28+
{
29+
description: "empty value",
30+
input: "key1=",
31+
expectedMap: map[string]string{"key1": ""},
32+
expectError: false,
33+
},
34+
{
35+
description: "value with equals sign",
36+
input: "key1=value=with=equals",
37+
expectedMap: map[string]string{"key1": "value=with=equals"},
38+
expectError: false,
39+
},
40+
{
41+
description: "special case: empty string to clear labels",
42+
input: "",
43+
expectedMap: map[string]string{}, // Should be an empty map, not nil
44+
expectError: false,
45+
},
46+
{
47+
description: "invalid format - no equals",
48+
input: "key1val1",
49+
expectedMap: nil,
50+
expectError: true,
51+
},
52+
{
53+
description: "invalid format - empty key",
54+
input: "=val1",
55+
expectedMap: nil,
56+
expectError: true,
57+
},
58+
{
59+
description: "mixed valid and invalid pair",
60+
input: "key1=val1,key2",
61+
expectedMap: nil,
62+
expectError: true,
63+
},
64+
{
65+
description: "invalid format - leading comma",
66+
input: ",key1=val1",
67+
expectedMap: nil,
68+
expectError: true,
69+
},
70+
{
71+
description: "invalid format - trailing comma",
72+
input: "key1=val1,",
73+
expectedMap: nil,
74+
expectError: true,
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.description, func(t *testing.T) {
80+
parsedMap, err := ParseLabels(tt.input)
81+
82+
if !tt.expectError && err != nil {
83+
t.Fatalf("did not expect an error, but got: %v", err)
84+
}
85+
86+
if tt.expectError && err == nil {
87+
t.Fatalf("expected an error, but got nil")
88+
}
89+
90+
if diff := cmp.Diff(tt.expectedMap, parsedMap); diff != "" {
91+
t.Errorf("map mismatch (-want +got):\n%s", diff)
92+
}
93+
})
94+
}
95+
}

internal/cmd/intake/intake.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,22 @@ import (
55
"github.com/stackitcloud/stackit-cli/internal/cmd/intake/runner"
66
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
77
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
109
)
1110

1211
// NewCmd creates the 'stackit intake' command
1312
func NewCmd(params *params.CmdParams) *cobra.Command {
1413
cmd := &cobra.Command{
1514
Use: "intake",
16-
Short: "Provides functionality for STACKIT Intake",
17-
Long: "Provides functionality for STACKIT Intake.",
15+
Short: "Provides functionality for intake",
16+
Long: "Provides functionality for intake.",
1817
Args: args.NoArgs,
19-
Example: examples.Build(
20-
examples.NewExample(
21-
``,
22-
"$ stackit intake"),
23-
),
24-
Run: utils.CmdHelp,
18+
Run: utils.CmdHelp,
2519
}
20+
addSubcommands(cmd, params)
21+
return cmd
22+
}
2623

27-
// Sub-commands
24+
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
2825
cmd.AddCommand(runner.NewCmd(params))
29-
30-
return cmd
3126
}

internal/cmd/intake/runner/create/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
2020
"github.com/stackitcloud/stackit-cli/internal/pkg/services/intake/client"
21+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2122
)
2223

2324
const (
@@ -85,9 +86,8 @@ func NewCreateCmd(p *params.CmdParams) *cobra.Command {
8586
if err != nil {
8687
return fmt.Errorf("create Intake Runner: %w", err)
8788
}
88-
runnerId := *resp.Id
8989

90-
return outputResult(p.Printer, model.OutputFormat, projectLabel, runnerId, resp)
90+
return outputResult(p.Printer, model.OutputFormat, projectLabel, resp)
9191
},
9292
}
9393
configureFlags(cmd)
@@ -150,7 +150,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIC
150150
return req
151151
}
152152

153-
func outputResult(p *print.Printer, outputFormat, projectLabel, runnerId string, resp *intake.IntakeRunnerResponse) error {
153+
func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *intake.IntakeRunnerResponse) error {
154154
switch outputFormat {
155155
case print.JSONOutputFormat:
156156
details, err := json.MarshalIndent(resp, "", " ")
@@ -169,7 +169,7 @@ func outputResult(p *print.Printer, outputFormat, projectLabel, runnerId string,
169169

170170
return nil
171171
default:
172-
p.Outputf("Created Intake Runner for project %q. Runner ID: %s\n", projectLabel, runnerId)
172+
p.Outputf("Created Intake Runner for project %q. Runner ID: %s\n", projectLabel, utils.PtrString(resp.Id))
173173
return nil
174174
}
175175
}

internal/cmd/intake/runner/create/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func TestOutputResult(t *testing.T) {
289289
p.Cmd = NewCreateCmd(&params.CmdParams{Printer: p})
290290
for _, tt := range tests {
291291
t.Run(tt.name, func(t *testing.T) {
292-
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.runnerId, tt.args.resp); (err != nil) != tt.wantErr {
292+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
293293
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
294294
}
295295
})

internal/cmd/intake/runner/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewDeleteCmd(p *params.CmdParams) *cobra.Command {
6565
return fmt.Errorf("delete Intake Runner: %w", err)
6666
}
6767

68-
p.Printer.Info("Deletion request for Intake Runner %q sent successfully.\n", model.RunnerId)
68+
p.Printer.Outputf("Deletion request for Intake Runner %q sent successfully.\n", model.RunnerId)
6969
return nil
7070
},
7171
}

internal/cmd/intake/runner/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewListCmd(p *params.CmdParams) *cobra.Command {
7474
p.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
7575
projectLabel = model.ProjectId
7676
}
77-
p.Printer.Info("No Intake Runners found for project %q\n", projectLabel)
77+
p.Printer.Outputf("No Intake Runners found for project %q\n", projectLabel)
7878
return nil
7979
}
8080

internal/cmd/intake/runner/list/list_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ func TestOutputResult(t *testing.T) {
208208
args: args{runners: nil},
209209
wantErr: false,
210210
},
211+
{
212+
name: "empty intake runner in slice",
213+
args: args{
214+
runners: []intake.IntakeRunnerResponse{{}},
215+
},
216+
wantErr: false,
217+
},
211218
}
212219
p := print.NewPrinter()
213220
p.Cmd = NewListCmd(&params.CmdParams{Printer: p})

0 commit comments

Comments
 (0)