Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 2f1797a

Browse files
committed
only append latest tag to image once, and use regex to check if image has tag
1 parent 07b456e commit 2f1797a

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

cmd/root.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,21 @@ func getPrepperForImage(image string) (pkgutil.Prepper, error) {
135135
}
136136

137137
// see if the image name has tag provided, if not add latest as tag
138-
if !strings.Contains(image, ":") {
138+
if !pkgutil.HasTag(image) {
139139
image = image + ":latest"
140140
}
141141

142142
if strings.HasPrefix(image, DaemonPrefix) {
143143
// remove the DaemonPrefix
144-
image := strings.Replace(image, DaemonPrefix, "", -1)
145-
if !strings.Contains(image, ":") {
146-
image = image + ":latest"
147-
}
144+
image = strings.Replace(image, DaemonPrefix, "", -1)
148145

149146
return pkgutil.DaemonPrepper{
150147
Source: image,
151148
Client: cli,
152149
}, nil
153150
}
154-
// either has remote prefix or has no prefix, in which case we force remote
155151

156-
// see if the image name has tag provided, if not add latest as tag
157-
if !strings.Contains(image, ":") {
158-
image = image + ":latest"
159-
}
152+
// either has remote prefix or has no prefix, in which case we force remote
160153
ref, err := docker.ParseReference("//" + image)
161154
if err != nil {
162155
return nil, err

pkg/util/image_prep_utils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ type ConfigSchema struct {
9999
}
100100

101101
func getImage(p Prepper) (Image, error) {
102-
output.PrintToStdErr("Retrieving image %s from source %s\n", p.GetSource(), p.Name())
102+
var source string
103+
// see if the image name has tag provided, if not add latest as tag
104+
if !HasTag(p.GetSource()) {
105+
source = p.GetSource() + ":latest"
106+
} else {
107+
source = p.GetSource()
108+
}
109+
output.PrintToStdErr("Retrieving image %s from source %s\n", source, p.Name())
103110
imgPath, err := p.GetFileSystem()
104111
if err != nil {
105112
return Image{}, err
@@ -110,9 +117,9 @@ func getImage(p Prepper) (Image, error) {
110117
logrus.Error("Error retrieving History: ", err)
111118
}
112119

113-
logrus.Infof("Finished prepping image %s", p.GetSource())
120+
logrus.Infof("Finished prepping image %s", source)
114121
return Image{
115-
Source: p.GetSource(),
122+
Source: source,
116123
FSPath: imgPath,
117124
Config: config,
118125
}, nil

pkg/util/image_utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"regexp"
2425

2526
"github.com/docker/docker/pkg/system"
2627
"github.com/sirupsen/logrus"
@@ -67,3 +68,9 @@ func copyToFile(outfile string, r io.Reader) error {
6768

6869
return nil
6970
}
71+
72+
// checks to see if an image string contains a tag.
73+
func HasTag(image string) bool {
74+
tagRegex := regexp.MustCompile(".*:[^/]*$")
75+
return tagRegex.MatchString(image)
76+
}

0 commit comments

Comments
 (0)