forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
51 lines (41 loc) · 1.2 KB
/
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
43
44
45
46
47
48
49
50
51
package reference
import (
"strings"
"github.com/docker/distribution/reference"
)
// NamedDockerImageReference points to a Docker image.
type NamedDockerImageReference struct {
Registry string
Namespace string
Name string
Tag string
ID string
}
// ParseNamedDockerImageReference parses a Docker pull spec string into a
// NamedDockerImageReference.
func ParseNamedDockerImageReference(spec string) (NamedDockerImageReference, error) {
var ref NamedDockerImageReference
namedRef, err := reference.ParseNamed(spec)
if err != nil {
return ref, err
}
name := namedRef.Name()
i := strings.IndexRune(name, '/')
if i == -1 || (!strings.ContainsAny(name[:i], ":.") && name[:i] != "localhost") {
ref.Name = name
} else {
ref.Registry, ref.Name = name[:i], name[i+1:]
}
if named, ok := namedRef.(reference.NamedTagged); ok {
ref.Tag = named.Tag()
}
if named, ok := namedRef.(reference.Canonical); ok {
ref.ID = named.Digest().String()
}
// It's not enough just to use the reference.ParseNamed(). We have to fill
// ref.Namespace from ref.Name
if i := strings.IndexRune(ref.Name, '/'); i != -1 {
ref.Namespace, ref.Name = ref.Name[:i], ref.Name[i+1:]
}
return ref, nil
}