Skip to content

Commit

Permalink
handle empty strings in copy-labels list
Browse files Browse the repository at this point in the history
  • Loading branch information
joemiller committed May 21, 2024
1 parent 389b69f commit 566b9d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 == ','
})
}
10 changes: 10 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 566b9d1

Please sign in to comment.