From c90eda9d6dd07bc52a2c354430a63971fab424d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Wed, 20 Mar 2024 17:47:42 +0000 Subject: [PATCH 1/4] Allow specifying empty list for CIDRSliceFlag --- internal/pkg/flags/cidrslice.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/pkg/flags/cidrslice.go b/internal/pkg/flags/cidrslice.go index d8e9a0594..5c6fd1f96 100644 --- a/internal/pkg/flags/cidrslice.go +++ b/internal/pkg/flags/cidrslice.go @@ -1,7 +1,6 @@ package flags import ( - "fmt" "strings" "github.com/spf13/pflag" @@ -25,7 +24,8 @@ func (f *cidrSliceFlag) String() string { func (f *cidrSliceFlag) Set(value string) error { if value == "" { - return fmt.Errorf("value cannot be empty") + f.value = []string{} + return nil } cidrs := strings.Split(value, ",") From 926a5b6e04aa85ac72004fffd59749a8d91fa949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Wed, 20 Mar 2024 17:57:58 +0000 Subject: [PATCH 2/4] Adapt test --- internal/pkg/flags/flags_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/pkg/flags/flags_test.go b/internal/pkg/flags/flags_test.go index b7326a135..9b2e72d64 100644 --- a/internal/pkg/flags/flags_test.go +++ b/internal/pkg/flags/flags_test.go @@ -586,9 +586,10 @@ func TestCIDRSliceFlag(t *testing.T) { isValid: false, }, { - description: "invalid empty value", - value1: utils.Ptr(""), - isValid: false, + description: "invalid empty value", + value1: utils.Ptr(""), + isValid: true, + expectedValue: []string{}, }, { description: "invalid empty value in list", From f9429fdcfd1280e2328b32ddf781bb7d598dfb43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Wed, 20 Mar 2024 17:58:29 +0000 Subject: [PATCH 3/4] Fix test case name --- internal/pkg/flags/flags_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/flags/flags_test.go b/internal/pkg/flags/flags_test.go index 9b2e72d64..0f7f74b23 100644 --- a/internal/pkg/flags/flags_test.go +++ b/internal/pkg/flags/flags_test.go @@ -586,7 +586,7 @@ func TestCIDRSliceFlag(t *testing.T) { isValid: false, }, { - description: "invalid empty value", + description: "empty value to specify empty list", value1: utils.Ptr(""), isValid: true, expectedValue: []string{}, From a8b17661b88d240362915b72bb04ce73da4dec46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Wed, 20 Mar 2024 18:21:52 +0000 Subject: [PATCH 4/4] Ignore empty value if the flag already contains values --- internal/pkg/flags/cidrslice.go | 6 +++++- internal/pkg/flags/flags_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/pkg/flags/cidrslice.go b/internal/pkg/flags/cidrslice.go index 5c6fd1f96..6bb595ff3 100644 --- a/internal/pkg/flags/cidrslice.go +++ b/internal/pkg/flags/cidrslice.go @@ -24,7 +24,11 @@ func (f *cidrSliceFlag) String() string { func (f *cidrSliceFlag) Set(value string) error { if value == "" { - f.value = []string{} + // If it's the first value to be set to the flag, we set it to an empty list + // Otherwise, we just ignore an empty value + if len(f.value) == 0 { + f.value = []string{} + } return nil } diff --git a/internal/pkg/flags/flags_test.go b/internal/pkg/flags/flags_test.go index 0f7f74b23..1e77f616b 100644 --- a/internal/pkg/flags/flags_test.go +++ b/internal/pkg/flags/flags_test.go @@ -591,6 +591,13 @@ func TestCIDRSliceFlag(t *testing.T) { isValid: true, expectedValue: []string{}, }, + { + description: "valid value and empty value", + value1: utils.Ptr("198.51.100.14/24"), + value2: utils.Ptr(""), + isValid: true, + expectedValue: []string{"198.51.100.14/24"}, + }, { description: "invalid empty value in list", value1: utils.Ptr("198.51.100.14/24,198.51.100.14/32,"),