diff --git a/main.go b/main.go index 3a91f01..5623e63 100644 --- a/main.go +++ b/main.go @@ -200,7 +200,7 @@ func main() { log.WithFields(log.Fields{"tags": defaultTags}).Infoln("Default Tags") if copyLabelsString != "" { - copyLabels = strings.Split(copyLabelsString, ",") + copyLabels = parseCopyLabels(copyLabelsString) log.Infof("Copying PVC labels to tags: %v", copyLabels) } @@ -367,5 +367,8 @@ func parseCopyLabels(copyLabelsString string) []string { if copyLabelsString == "" { return []string{} } - return strings.Split(copyLabelsString, ",") + // remove empty strings from final list, eg: "foo,,bar" -> ["foo" "bar"]: + return strings.FieldsFunc(copyLabelsString, func(c rune) bool { + return c == ',' + }) } diff --git a/main_test.go b/main_test.go index a911bd8..8b4f046 100644 --- a/main_test.go +++ b/main_test.go @@ -95,11 +95,21 @@ func Test_parseCopyLabels(t *testing.T) { copyLabelsString: "foo,bar", want: []string{"foo", "bar"}, }, + { + name: "empty values in list", + copyLabelsString: "foo,bar", + want: []string{"foo", "bar"}, + }, { name: "copy no labels", copyLabelsString: "", want: []string{}, }, + { + name: "empty values in list", + copyLabelsString: "foo,,bar", + want: []string{"foo", "bar"}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {