-
Notifications
You must be signed in to change notification settings - Fork 27
/
artifact.go
68 lines (61 loc) · 1.43 KB
/
artifact.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
package registry
import (
"fmt"
"strings"
)
// Artifact to head release dependency.
type Artifact struct {
Registry string
Repository string
Tag string
Digest string
}
// NewArtifact creates a new artifact object.
func NewArtifact(registry, repository, tag, digest string) Artifact {
return Artifact{
Registry: registry,
Repository: repository,
Tag: tag,
Digest: digest,
}
}
// ParseArtifactFromURI parses the URI into a new Artifact object.
func ParseArtifactFromURI(uri string) (*Artifact, error) {
elements := strings.SplitN(uri, "/", 2)
if len(elements) != 2 {
return nil, fmt.Errorf("registry not found")
}
registry := elements[0]
rol := elements[1]
var tag string
var digest string
elements = strings.SplitN(rol, "@", 2)
if len(elements) != 2 {
elements = strings.SplitN(rol, ":", 2)
if len(elements) != 2 {
return nil, fmt.Errorf("tag or digest not found")
}
tag = elements[1]
} else {
digest = elements[1]
}
repository := elements[0]
return &Artifact{
Registry: registry,
Repository: repository,
Tag: tag,
Digest: digest,
}, nil
}
// Version returns tag or digest.
func (art Artifact) Version() string {
if art.Digest != "" {
return "@" + art.Digest
}
return ":" + art.Tag
}
// VersionedImage returns full URI for image.
func (art Artifact) VersionedImage() string {
version := art.Version()
return art.Registry + "/" + art.Repository + version
}