Skip to content

Commit

Permalink
WIP: Add Image.RemoveTag
Browse files Browse the repository at this point in the history
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Feb 7, 2020
1 parent 1f77809 commit 58e3fd1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
31 changes: 31 additions & 0 deletions internal/localimage/image.go
Expand Up @@ -67,3 +67,34 @@ func (i *Image) AddTag(tag reference.Named) (bool, error) {
}
return true, nil
}

// RemoveTag removes tag from image.
// If the tag is not present, the method silently succeeds.
// The image is not removed even if the last tag is removed.
// Returns true if the tag was found and removed, false if the tags were not changed. // FIXME: can/should this be dropped?
// FIXME: This allows untagging using name@digest, and name:tag@digest. Should that be allowed??!!
func (i *Image) RemoveTag(tag reference.Named) (bool, error) {
if reference.IsNameOnly(tag) {
return false, fmt.Errorf("Refusing to remove tag %q which has neither a tag nor a digest", tag.String())
}

si, err := i.storageImage()
if err != nil {
return false, err
}
tagString := tag.String()
if !util.StringInSlice(tagString, si.Names) {
return false, nil
}

newNames := []string{}
for _, t := range si.Names {
if t != tagString {
newNames = append(newNames, t)
}
}
if err := i.storage.store.SetNames(si.ID, newNames); err != nil {
return false, err
}
return true, nil
}
26 changes: 12 additions & 14 deletions libpod/image/image.go
Expand Up @@ -573,26 +573,24 @@ func (i *Image) TagImage(tag string) error {

// UntagImage removes a tag from the given image
func (i *Image) UntagImage(tag string) error {
if err := i.reloadImage(); err != nil {
// Previously, this code only worked with an _exact_ match of the tag against i.Names(), which contains
// reference.Named.String()-formatted data. So, for any input that worked previously, we can use reference.ParseNormalizedNamed()
// here safely without worrying about short names and the like.
// FIXME: Should this use normalizeTag anyway?
ref, err := reference.ParseNormalizedNamed(tag)
if err != nil {
return err
}
var newTags []string
tags := i.Names()
if !util.StringInSlice(tag, tags) {
updated, err := i.localImage.RemoveTag(ref)
if err != nil {
return nil
}
for _, t := range tags {
if tag != t {
newTags = append(newTags, t)
if updated {
if err := i.reloadImage(); err != nil {
return err
}
i.newImageEvent(events.Untag)
}
if err := i.imageruntime.store.SetNames(i.ID(), newTags); err != nil {
return err
}
if err := i.reloadImage(); err != nil {
return err
}
i.newImageEvent(events.Untag)
return nil
}

Expand Down

0 comments on commit 58e3fd1

Please sign in to comment.