-
Notifications
You must be signed in to change notification settings - Fork 35
/
tag.go
59 lines (53 loc) · 1.14 KB
/
tag.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package docker
import (
"fmt"
"strings"
)
// Tag represent a docker tag
type Tag struct {
Username string
Registry string
Image string
Version string
}
func (t *Tag) Repository() string {
if t.Registry == "" {
t.Registry = "index.docker.io"
}
return fmt.Sprintf("%v/%v", t.Registry, t.Username)
}
// String stringify docker tag
func (t *Tag) String() string {
result := t.Registry
if result == "" {
result = t.Username
} else if t.Username != "" {
result += "/" + t.Username
}
if result != "" {
result += "/"
}
result += t.Image
if t.Version != "" {
result += ":" + t.Version
}
return result
}
// NewTag returns new tag
func NewTag(imageTag string) *Tag {
tag := &Tag{}
parts := strings.SplitN(imageTag, ":", 2)
if len(parts) == 2 {
tag.Version = parts[1]
imageTag = parts[0]
}
tag.Image = imageTag
if imageIndex := strings.LastIndex(imageTag, "/"); imageIndex != -1 {
tag.Registry = string(imageTag[:imageIndex])
tag.Image = string(imageTag[imageIndex+1:])
if registryIndex := strings.LastIndex(tag.Username, "/"); registryIndex != -1 {
tag.Username = string(tag.Username[registryIndex+1:])
}
}
return tag
}