Skip to content

Commit f6dd1b7

Browse files
committed
store/index: return error from UnTag (docker/model-distribution#109)
1 parent e9d85bd commit f6dd1b7

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

pkg/distribution/internal/store/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func (i Index) Tag(reference string, tag string) (Index, error) {
3939
return result, nil
4040
}
4141

42-
func (i Index) UnTag(tag string) (name.Tag, Index) {
42+
func (i Index) UnTag(tag string) (name.Tag, Index, error) {
4343
tagRef, err := name.NewTag(tag)
4444
if err != nil {
45-
return name.Tag{}, Index{}
45+
return name.Tag{}, Index{}, err
4646
}
4747

4848
result := Index{
@@ -52,7 +52,7 @@ func (i Index) UnTag(tag string) (name.Tag, Index) {
5252
result.Models = append(result.Models, entry.UnTag(tagRef))
5353
}
5454

55-
return tagRef, result
55+
return tagRef, result, nil
5656
}
5757

5858
func (i Index) Find(reference string) (IndexEntry, int, bool) {

pkg/distribution/internal/store/index_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,26 @@ func TestUntag(t *testing.T) {
146146
},
147147
},
148148
}
149-
tag, idx := idx.UnTag("other-tag")
150-
if len(idx.Models) != 2 {
151-
t.Fatalf("Expected 2 models, got %d", len(idx.Models))
152-
}
153-
if len(idx.Models[0].Tags) != 1 {
154-
t.Fatalf("Expected 1 tag, got %d", len(idx.Models[0].Tags))
155-
}
156-
if tag.String() != "other-tag" {
157-
t.Fatalf("Expected tag 'other-tag', got '%s'", tag)
158-
}
149+
t.Run("UnTagging existing tag", func(t *testing.T) {
150+
tag, idx, err := idx.UnTag("other-tag")
151+
if err != nil {
152+
t.Fatalf("Error untagging entry: %v", err)
153+
}
154+
if len(idx.Models) != 2 {
155+
t.Fatalf("Expected 2 models, got %d", len(idx.Models))
156+
}
157+
if len(idx.Models[0].Tags) != 1 {
158+
t.Fatalf("Expected 1 tag, got %d", len(idx.Models[0].Tags))
159+
}
160+
if tag.String() != "other-tag" {
161+
t.Fatalf("Expected tag 'other-tag', got '%s'", tag)
162+
}
163+
})
164+
t.Run("UnTagging invalid tag", func(t *testing.T) {
165+
_, _, err := idx.UnTag("!@#$%^&*()")
166+
if err == nil {
167+
t.Fatal("Expected error when untagging non-existing tag, got nil")
168+
}
169+
})
159170
})
160171
}

pkg/distribution/internal/store/store.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path/filepath"
88

99
"github.com/docker/model-distribution/internal/progress"
10-
"github.com/google/go-containerregistry/pkg/name"
1110
v1 "github.com/google/go-containerregistry/pkg/v1"
1211
)
1312

@@ -165,11 +164,18 @@ func (s *LocalStore) RemoveTags(tags []string) ([]string, error) {
165164
if err != nil {
166165
return nil, fmt.Errorf("reading modelss index: %w", err)
167166
}
168-
var tagRef name.Tag
169167
var tagRefs []string
170168
for _, tag := range tags {
171-
tagRef, index = index.UnTag(tag)
169+
tagRef, newIndex, err := index.UnTag(tag)
170+
if err != nil {
171+
// Try to save progress before returning error.
172+
if writeIndexErr := s.writeIndex(newIndex); writeIndexErr != nil {
173+
return tagRefs, fmt.Errorf("untagging model: %w, also failed to save: %w", err, writeIndexErr)
174+
}
175+
return tagRefs, fmt.Errorf("untagging model: %w", err)
176+
}
172177
tagRefs = append(tagRefs, tagRef.Name())
178+
index = newIndex
173179
}
174180
return tagRefs, s.writeIndex(index)
175181
}

0 commit comments

Comments
 (0)