Skip to content

Commit

Permalink
fix logic of needsTag, update tests and CleanupTags
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Feb 19, 2024
1 parent 85cbc05 commit 3a049b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 6 additions & 9 deletions pkg/tagit/tagit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os/exec"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -103,13 +104,7 @@ func (t *TagIt) CleanupTags() error {
if err != nil {
return fmt.Errorf("error getting service: %w", err)
}
registration := t.copyServiceToRegistration(service)
updatedTags, tagged := t.excludeTagged(registration.Tags)
if tagged {
registration.Tags = updatedTags
return t.client.Agent().ServiceRegister(registration)
}
return nil
return t.updateConsulService(service, []string{})
}

// runScript runs a command and returns the output.
Expand Down Expand Up @@ -206,8 +201,10 @@ func (t *TagIt) needsTag(current []string, update []string) (updatedTags []strin
if len(diff) == 0 {
return nil, false
}

updatedTags, _ = t.excludeTagged(diff)
currentFiltered, _ := t.excludeTagged(current)
updatedTags = append(currentFiltered, update...)
slices.Sort(updatedTags)
updatedTags = slices.Compact(updatedTags)
return updatedTags, true
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/tagit/tagit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestNeedsTag(t *testing.T) {
name: "Update Needed",
current: []string{"tag-tag1", "tag-tag2"},
update: []string{"tag-tag1", "tag-tag2", "tag3"},
expectedTags: []string{"tag3"},
expectedTags: []string{"tag-tag1", "tag-tag2", "tag3"},
expectedShould: true,
},
{
Expand All @@ -166,7 +166,7 @@ func TestNeedsTag(t *testing.T) {
name: "Mixed Changes",
current: []string{"tag-tag1", "tag2", "tag4"},
update: []string{"tag2", "tag3", "tag5"},
expectedTags: []string{"tag4", "tag3", "tag5"},
expectedTags: []string{"tag2", "tag3", "tag4", "tag5"},
expectedShould: true,
},
}
Expand Down Expand Up @@ -660,7 +660,7 @@ func TestCleanupTags(t *testing.T) {
},
tagPrefix: "tag",
expectError: false,
expectTags: []string{"prefix1", "prefix2", "other-tag"},
expectTags: []string{"other-tag", "prefix1", "prefix2"},
},
{
name: "Service Not Found",
Expand Down

0 comments on commit 3a049b7

Please sign in to comment.