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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/dns v0.23.1
github.com/stackitcloud/stackit-sdk-go/services/edge v0.13.2
github.com/stackitcloud/stackit-sdk-go/services/git v0.14.1
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.1
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.5
github.com/stackitcloud/stackit-sdk-go/services/intake v0.11.2
github.com/stackitcloud/stackit-sdk-go/services/logs v0.10.2
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.12.2
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.3.1
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.15.0
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.2
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.26.0
github.com/stackitcloud/stackit-sdk-go/services/runcommand v1.10.2
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.19.1
github.com/stackitcloud/stackit-sdk-go/services/serverbackup v1.7.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ github.com/stackitcloud/stackit-sdk-go/services/edge v0.13.2 h1:UDkXmgOAlP3y/u7F
github.com/stackitcloud/stackit-sdk-go/services/edge v0.13.2/go.mod h1:hlUcPvOgH/CzZqH7u4SwV81qiismCVAtvw5pTAf6si4=
github.com/stackitcloud/stackit-sdk-go/services/git v0.14.1 h1:7R5peSRer3Y83mfB6wcdCP0vhEjsq4myUdVhl24q6to=
github.com/stackitcloud/stackit-sdk-go/services/git v0.14.1/go.mod h1:kz2pl17t9MNQx0+fNXg10ZAVfjmvOtsHB7VlAzu/Io8=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.1 h1:P8XhOi8TLA7NbLHhxU8dCJjgQpha/65agos3pI59+vw=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.1/go.mod h1:/QpOsaCOQjwGgQqG8y9xXqryp25AfkorPjInX9xIeOU=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.5 h1:CVRo4/hCy6vLlh6LQjQikTDnh/0KF43nRhKsiicxyXc=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.5/go.mod h1:oJmNZY04vvjrQI+y91ZZWOQiCAREcCldkh6cib+YtP0=
github.com/stackitcloud/stackit-sdk-go/services/intake v0.11.2 h1:wC0J5fHffmbPLzsqjQ08P9Jb10/BbJUm45TSNfAm8jc=
github.com/stackitcloud/stackit-sdk-go/services/intake v0.11.2/go.mod h1:WIgF8d0xz2dIf95k1lMIGCBoyDhr2/AFJFEdnWB8tTo=
github.com/stackitcloud/stackit-sdk-go/services/kms v1.13.1 h1:iEPerniBpji6dGiB31p1WAUxgBf9VBIL9gKocfbBqW8=
Expand All @@ -654,8 +654,8 @@ github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.3.1 h1:6t+EJTR9HOFZG
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.3.1/go.mod h1:oGcpMIs2H/ldTQlTkYJUY4ZaVNi3MMRSMz0XpLDSKcU=
github.com/stackitcloud/stackit-sdk-go/services/redis v1.4.1 h1:s3mXo6FGNEeMWqVPq7yaDgcOakEejRP7X9l7OaK7PEE=
github.com/stackitcloud/stackit-sdk-go/services/redis v1.4.1/go.mod h1:6ihYZA22kityLgM0Pp7qROOomcZeUV0OsayOrd5vVe4=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.2 h1:4UxxJmCSCwV8q4bT4G+D1JH8F9Gm6BKaLGixX5DVcvI=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.2/go.mod h1:NEz3f+GV5G++BE9/MmZCsXJyCih7jtg0pZuSyG2sLEs=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.26.0 h1:TzmBTja200jx6An39UPYBZPvZvLFgk3g1S0XZ1vDeBI=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.26.0/go.mod h1:Y5EM8b9qe/+AKI2LJoFvI0PzkyHO5RiW8nQuQaaNaFA=
github.com/stackitcloud/stackit-sdk-go/services/runcommand v1.10.2 h1:YpTj2lU0sKe7a1mBH9LW30wUijxZZ1z246a7V40zHDc=
github.com/stackitcloud/stackit-sdk-go/services/runcommand v1.10.2/go.mod h1:KQLunYwad9IhSbnXuhkKAldVG9LNxCzIYHR8vMJlKQI=
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.19.1 h1:Mym5vqa3YaF42LbqlVXDFYf/evsNU76l0wpUPGEu+0U=
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/project/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type inputModel struct {
*globalflags.GlobalFlagModel
ParentId *string
Name *string
Labels *map[string]string
Labels *map[string]*string
}

func NewCmd(params *types.CmdParams) *cobra.Command {
Expand Down Expand Up @@ -107,7 +107,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
return nil, &errors.ProjectIdError{}
}

labels := flags.FlagToStringToStringPointer(p, cmd, labelFlag)
labels := flags.FlagToStringToStringPointerPointer(p, cmd, labelFlag)
parentId := flags.FlagToStringPointer(p, cmd, parentIdFlag)
name := flags.FlagToStringPointer(p, cmd, nameFlag)

Expand All @@ -126,10 +126,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
}
}

if !labelValueRegex.MatchString(value) {
if value == nil || !labelValueRegex.MatchString(*value) {
return nil, &errors.FlagValidationError{
Flag: labelFlag,
Details: fmt.Sprintf("label value %s for key %s didn't match the required regex expression %s", value, key, labelValueRegex),
Details: fmt.Sprintf("label value %v for key %s didn't match the required regex expression %s", value, key, labelValueRegex),
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions internal/cmd/project/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

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

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -41,8 +40,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
ProjectId: testProjectId,
Verbosity: globalflags.VerbosityDefault,
},
ParentId: utils.Ptr(testParentId),
Name: utils.Ptr(nameFlag),
ParentId: new(testParentId),
Name: new(nameFlag),
}
for _, mod := range mods {
mod(model)
Expand All @@ -53,8 +52,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
func fixtureRequest(mods ...func(request *resourcemanager.ApiPartialUpdateProjectRequest)) resourcemanager.ApiPartialUpdateProjectRequest {
request := testClient.DefaultAPI.PartialUpdateProject(testCtx, testProjectId)
request = request.PartialUpdateProjectPayload(resourcemanager.PartialUpdateProjectPayload{
ContainerParentId: utils.Ptr(testParentId),
Name: utils.Ptr(nameFlag),
ContainerParentId: new(testParentId),
Name: new(nameFlag),
})
for _, mod := range mods {
mod(&request)
Expand Down Expand Up @@ -101,9 +100,9 @@ func TestParseInput(t *testing.T) {
labelValues: []string{"key=value", "foo=bar"},
expectedModel: fixtureInputModel(
func(model *inputModel) {
model.Labels = &map[string]string{
"key": "value",
"foo": "bar",
model.Labels = &map[string]*string{
"key": new("value"),
"foo": new("bar"),
}
}),
isValid: true,
Expand All @@ -114,9 +113,9 @@ func TestParseInput(t *testing.T) {
labelValues: []string{"key=value,foo=bar"},
expectedModel: fixtureInputModel(
func(model *inputModel) {
model.Labels = &map[string]string{
"key": "value",
"foo": "bar",
model.Labels = &map[string]*string{
"key": new("value"),
"foo": new("bar"),
}
}),
isValid: true,
Expand Down
20 changes: 20 additions & 0 deletions internal/pkg/flags/flag_to_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ func FlagToStringToStringPointer(p *print.Printer, cmd *cobra.Command, flag stri
return nil
}

// FlagToStringToStringPointerPointer returns a pointer to the flag's value as a map of string pointers.
// Returns nil if the flag is not set, cannot be converted, or does not exist.
func FlagToStringToStringPointerPointer(p *print.Printer, cmd *cobra.Command, flag string) *map[string]*string { //nolint:gocritic //convenient for setting the SDK payload
value, err := cmd.Flags().GetStringToString(flag)
if err != nil {
p.Debug(print.ErrorLevel, "convert flag to string to string pointer pointer: %v", err)
return nil
}
if !cmd.Flag(flag).Changed {
return nil
}

pointerValue := make(map[string]*string, len(value))
for key, item := range value {
item := item
pointerValue[key] = &item
}
return &pointerValue
}

func FlagToStringToAny(p *print.Printer, cmd *cobra.Command, flag string) map[string]any {
value, err := cmd.Flags().GetStringToString(flag)
r := make(map[string]any, len(value))
Expand Down
37 changes: 37 additions & 0 deletions internal/pkg/flags/flag_to_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ func TestFlagToStringToStringPointer(t *testing.T) {
}
}

func TestFlagToStringToStringPointerPointer(t *testing.T) {
const flagName = "labels"

tests := []struct {
name string
flagValue *string
want *map[string]*string
}{
{name: "flag unset"},
{
name: "flag set",
flagValue: utils.Ptr("foo=bar,label1=value1"),
want: &map[string]*string{
"foo": utils.Ptr("bar"),
"label1": utils.Ptr("value1"),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := testparams.NewTestParams()
cmd := &cobra.Command{Use: "test"}
cmd.Flags().StringToString(flagName, nil, "Labels")
if tt.flagValue != nil {
if err := cmd.Flags().Set(flagName, *tt.flagValue); err != nil {
t.Fatal(err)
}
}

if got := FlagToStringToStringPointerPointer(params.Printer, cmd, flagName); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FlagToStringToStringPointerPointer() = %v, want %v", got, tt.want)
}
})
}
}

func TestFlagToStringArrayValue(t *testing.T) {
const flagName = "geofencing"
tests := []struct {
Expand Down
Loading