-
Notifications
You must be signed in to change notification settings - Fork 280
/
parse.go
131 lines (102 loc) · 2.68 KB
/
parse.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package image
import (
"strings"
)
// Reference is an opaque object that include identifier such as a name, tag, repository, registry, etc...
type Reference struct {
named Named `json:"named"`
tag string `json:"tag"`
scheme string `json:"scheme"` // registry scheme, i.e. http, https
}
func (r Reference) String() string {
return r.Name()
}
// Name returns the image's name. (ie: debian[:8.2])
func (r Reference) Name() string {
return r.named.RemoteName() + r.tag
}
// ShortName returns the image's name (ie: debian)
func (r Reference) ShortName() string {
return r.named.RemoteName()
}
// Tag returns the image's tag (or digest).
func (r Reference) Tag() string {
if len(r.tag) > 1 {
return r.tag[1:]
}
return ""
}
// Registry returns the image's registry. (ie: host[:port])
func (r Reference) Registry() string {
return r.named.Hostname()
}
// Scheme returns registry's scheme. (ie: https)
func (r Reference) Scheme() string {
return r.scheme
}
// Repository returns the image's repository. (ie: registry/name)
func (r Reference) Repository() string {
return r.named.FullName()
}
// Remote returns the image's remote identifier. (ie: registry/name[:tag])
func (r Reference) Remote() string {
return r.named.FullName() + r.tag
}
func clean(url string) (cleaned string, scheme string) {
s := url
if strings.HasPrefix(url, "http://") {
scheme = "http"
s = strings.Replace(url, "http://", "", 1)
} else if strings.HasPrefix(url, "https://") {
scheme = "https"
s = strings.Replace(url, "https://", "", 1)
}
if scheme == "" {
scheme = DefaultScheme
}
return s, scheme
}
// Parse returns a Reference from analyzing the given remote identifier.
func Parse(remote string) (*Reference, error) {
cleaned, scheme := clean(remote)
n, err := ParseNamed(cleaned)
if err != nil {
return nil, err
}
n = WithDefaultTag(n)
var t string
switch x := n.(type) {
case Canonical:
t = "@" + x.Digest().String()
case NamedTagged:
t = ":" + x.Tag()
}
return &Reference{named: n, tag: t, scheme: scheme}, nil
}
// ParseRepo - parses remote
// pretty much the same as Parse but better for testing
func ParseRepo(remote string) (*Repository, error) {
cleaned, scheme := clean(remote)
n, err := ParseNamed(cleaned)
if err != nil {
return nil, err
}
n = WithDefaultTag(n)
var t string
switch x := n.(type) {
case Canonical:
t = "@" + x.Digest().String()
case NamedTagged:
t = ":" + x.Tag()
}
ref := &Reference{named: n, tag: t, scheme: scheme}
return &Repository{
Name: ref.Name(),
Repository: ref.Repository(),
Registry: ref.Registry(),
Remote: ref.Remote(),
ShortName: ref.ShortName(),
Tag: ref.Tag(),
Scheme: ref.scheme,
}, nil
}