Skip to content

Commit e2832ec

Browse files
authored
Merge pull request #13 from ncode/juliano/fix_needs_tag
fix logic of needsTag, update tests and CleanupTags
2 parents 85cbc05 + 3a049b7 commit e2832ec

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

pkg/tagit/tagit.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os/exec"
7+
"slices"
78
"strings"
89
"time"
910

@@ -103,13 +104,7 @@ func (t *TagIt) CleanupTags() error {
103104
if err != nil {
104105
return fmt.Errorf("error getting service: %w", err)
105106
}
106-
registration := t.copyServiceToRegistration(service)
107-
updatedTags, tagged := t.excludeTagged(registration.Tags)
108-
if tagged {
109-
registration.Tags = updatedTags
110-
return t.client.Agent().ServiceRegister(registration)
111-
}
112-
return nil
107+
return t.updateConsulService(service, []string{})
113108
}
114109

115110
// runScript runs a command and returns the output.
@@ -206,8 +201,10 @@ func (t *TagIt) needsTag(current []string, update []string) (updatedTags []strin
206201
if len(diff) == 0 {
207202
return nil, false
208203
}
209-
210-
updatedTags, _ = t.excludeTagged(diff)
204+
currentFiltered, _ := t.excludeTagged(current)
205+
updatedTags = append(currentFiltered, update...)
206+
slices.Sort(updatedTags)
207+
updatedTags = slices.Compact(updatedTags)
211208
return updatedTags, true
212209
}
213210

pkg/tagit/tagit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestNeedsTag(t *testing.T) {
145145
name: "Update Needed",
146146
current: []string{"tag-tag1", "tag-tag2"},
147147
update: []string{"tag-tag1", "tag-tag2", "tag3"},
148-
expectedTags: []string{"tag3"},
148+
expectedTags: []string{"tag-tag1", "tag-tag2", "tag3"},
149149
expectedShould: true,
150150
},
151151
{
@@ -166,7 +166,7 @@ func TestNeedsTag(t *testing.T) {
166166
name: "Mixed Changes",
167167
current: []string{"tag-tag1", "tag2", "tag4"},
168168
update: []string{"tag2", "tag3", "tag5"},
169-
expectedTags: []string{"tag4", "tag3", "tag5"},
169+
expectedTags: []string{"tag2", "tag3", "tag4", "tag5"},
170170
expectedShould: true,
171171
},
172172
}
@@ -660,7 +660,7 @@ func TestCleanupTags(t *testing.T) {
660660
},
661661
tagPrefix: "tag",
662662
expectError: false,
663-
expectTags: []string{"prefix1", "prefix2", "other-tag"},
663+
expectTags: []string{"other-tag", "prefix1", "prefix2"},
664664
},
665665
{
666666
name: "Service Not Found",

0 commit comments

Comments
 (0)