-
Notifications
You must be signed in to change notification settings - Fork 1k
/
reference.go
42 lines (36 loc) · 911 Bytes
/
reference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package util
import (
"strings"
)
type refOpts struct {
withTag bool
}
type ReferenceOption func(r *refOpts)
// ReferenceWithTag returns a ReferenceOption that ensures a tag is filled. If the tag is not provided,
// the default is added
func ReferenceWithTag() ReferenceOption {
return func(r *refOpts) {
r.withTag = true
}
}
// ReferenceExpand expands "redis" to "docker.io/library/redis" so all images have a full domain,
// and similarly foo/bar to docker.io/foo/bar.
// If the image does not have a tag, ":latest" is added.
func ReferenceExpand(ref string, options ...ReferenceOption) string {
var opts refOpts
for _, opt := range options {
opt(&opts)
}
ret := ref
parts := strings.Split(ref, "/")
switch len(parts) {
case 1:
ret = "docker.io/library/" + ref
case 2:
ret = "docker.io/" + ref
}
if opts.withTag && !strings.Contains(ret, ":") {
ret += ":latest"
}
return ret
}