Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected error with --select-tag and multiple files #571

Merged
merged 2 commits into from
Jan 26, 2022
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
5 changes: 3 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ func determineSelectorTag(targetContent file.Content, config dump.Config) ([]str
if targetContent.Info != nil {
if len(targetContent.Info.SelectorTags) > 0 {
if len(config.SelectorTags) > 0 {
utils.RemoveDuplicates(&targetContent.Info.SelectorTags)
sort.Strings(config.SelectorTags)
sort.Strings(targetContent.Info.SelectorTags)
if !reflect.DeepEqual(config.SelectorTags, targetContent.Info.SelectorTags) {
return nil, fmt.Errorf(`tags specified in the state file and via --select-tags flag are different.
return nil, fmt.Errorf(`tags specified in the state file (%v) and via --select-tags flag (%v) are different.
decK expects tags to be specified in either via flag or via state file.
In case both are specified, they must match`)
In case both are specified, they must match`, targetContent.Info.SelectorTags, config.SelectorTags)
}
// Both present and equal, return whichever
return targetContent.Info.SelectorTags, nil
Expand Down
18 changes: 18 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ func TestDetermineSelectorTag(t *testing.T) {
want: []string{"foo"},
wantErr: false,
},
{
name: "config has one tag and file has duplicates",
args: args{
dumpConfig: dump.Config{SelectorTags: []string{"foo"}},
fileContent: file.Content{Info: &file.Info{SelectorTags: []string{"foo", "foo"}}},
},
want: []string{"foo"},
wantErr: false,
},
{
name: "config has multiple tags and file has duplicates",
args: args{
dumpConfig: dump.Config{SelectorTags: []string{"foo", "bar"}},
fileContent: file.Content{Info: &file.Info{SelectorTags: []string{"foo", "bar", "foo", "bar"}}},
},
want: []string{"bar", "foo"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ func CallGetAll(obj interface{}) (reflect.Value, error) {
result = reflect.ValueOf(entities)
return result, nil
}

func alreadyInSlice(elem string, slice []string) bool {
for _, s := range slice {
if s == elem {
return true
}
}
return false
}

// RemoveDuplicates removes duplicated elements from a slice.
func RemoveDuplicates(slice *[]string) {
newSlice := []string{}
for _, s := range *slice {
if alreadyInSlice(s, newSlice) {
continue
}
newSlice = append(newSlice, s)
}
*slice = newSlice
}