Skip to content

Commit 6f6eb6a

Browse files
authored
Allow specifying empty list for CIDRSliceFlag (#170)
* Allow specifying empty list for CIDRSliceFlag * Adapt test * Fix test case name * Ignore empty value if the flag already contains values
1 parent ea4680b commit 6f6eb6a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

internal/pkg/flags/cidrslice.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package flags
22

33
import (
4-
"fmt"
54
"strings"
65

76
"github.com/spf13/pflag"
@@ -25,7 +24,12 @@ func (f *cidrSliceFlag) String() string {
2524

2625
func (f *cidrSliceFlag) Set(value string) error {
2726
if value == "" {
28-
return fmt.Errorf("value cannot be empty")
27+
// If it's the first value to be set to the flag, we set it to an empty list
28+
// Otherwise, we just ignore an empty value
29+
if len(f.value) == 0 {
30+
f.value = []string{}
31+
}
32+
return nil
2933
}
3034

3135
cidrs := strings.Split(value, ",")

internal/pkg/flags/flags_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,17 @@ func TestCIDRSliceFlag(t *testing.T) {
586586
isValid: false,
587587
},
588588
{
589-
description: "invalid empty value",
590-
value1: utils.Ptr(""),
591-
isValid: false,
589+
description: "empty value to specify empty list",
590+
value1: utils.Ptr(""),
591+
isValid: true,
592+
expectedValue: []string{},
593+
},
594+
{
595+
description: "valid value and empty value",
596+
value1: utils.Ptr("198.51.100.14/24"),
597+
value2: utils.Ptr(""),
598+
isValid: true,
599+
expectedValue: []string{"198.51.100.14/24"},
592600
},
593601
{
594602
description: "invalid empty value in list",

0 commit comments

Comments
 (0)