-
Notifications
You must be signed in to change notification settings - Fork 0
/
oci-image-reference.go
118 lines (95 loc) · 3.24 KB
/
oci-image-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
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
package sdk
import (
"errors"
"strconv"
"strings"
)
//
// OCIImageReference
//
type OCIImageReference struct {
Artifact string `ard:"artifact"`
Reference string `ard:"reference"`
Host string `ard:"host"`
Image string `ard:"image"`
Tag string `ard:"tag"`
Port int64 `ard:"port"`
Repository string `ard:"repository"`
DigestAlgorithm string `ard:"digest-algorithm"`
DigestHex string `ard:"digest-hex"`
}
func (self OCIImageReference) Validate() error {
if self.Artifact != "" {
if (self.Reference != "") || (self.Host != "") || (self.Image != "") || (self.Tag != "") || (self.Port != 0) || (self.Repository != "") || (self.DigestAlgorithm != "") || (self.DigestHex != "") {
return errors.New("invalid OCI image reference: `artifact` incompatible with other properties")
}
}
if self.Reference != "" {
if (self.Artifact != "") || (self.Host != "") || (self.Image != "") || (self.Tag != "") || (self.Port != 0) || (self.Repository != "") || (self.DigestAlgorithm != "") || (self.DigestHex != "") {
return errors.New("invalid OCI image reference: `reference` incompatible with other properties")
}
}
if self.Image != "" {
if (self.Host == "") || (self.Tag == "") {
return errors.New("invalid OCI image reference: `image`, `host`, and `tag` must be set together")
}
if (self.Artifact != "") || (self.Reference != "") {
return errors.New("invalid OCI image reference: `image` incompatible with `artifact` and `reference`")
}
}
if self.Host != "" {
if (self.Image == "") || (self.Tag == "") {
return errors.New("invalid OCI image reference: `image`, `host`, and `tag` must be set together")
}
if (self.Artifact != "") || (self.Reference != "") {
return errors.New("invalid OCI image reference: `image` incompatible with `artifact` and `reference`")
}
}
if self.Tag != "" {
if (self.Host == "") || (self.Image == "") {
return errors.New("invalid OCI image reference: `image`, `host`, and `tag` must be set together")
}
if (self.Artifact != "") || (self.Reference != "") {
return errors.New("invalid OCI image reference: `image` incompatible with `artifact` and `reference`")
}
}
if (self.DigestAlgorithm != "") && (self.DigestHex == "") {
return errors.New("invalid OCI image reference: `digest-algorithm` and `digest-hex` must be set together")
}
if (self.DigestHex != "") && (self.DigestAlgorithm == "") {
return errors.New("invalid OCI image reference: `digest-algorithm` and `digest-hex` must be set together")
}
return nil
}
// fmt.Stringer interface
func (self OCIImageReference) String() string {
// [host[:port]/][repository/]image[:tag][@digest-algorithm:digest-hex]
if self.Reference != "" {
return self.Reference
}
var s strings.Builder
if self.Host != "" {
s.WriteString(self.Host)
if self.Port != 0 {
s.WriteRune(':')
s.WriteString(strconv.Itoa(int(self.Port)))
}
s.WriteRune('/')
}
if self.Repository != "" {
s.WriteString(self.Repository)
s.WriteRune('/')
}
s.WriteString(self.Image)
if self.Tag != "" {
s.WriteRune(':')
s.WriteString(self.Tag)
}
if (self.DigestAlgorithm != "") && (self.DigestHex != "") {
s.WriteRune('@')
s.WriteString(self.DigestAlgorithm)
s.WriteRune(':')
s.WriteString(self.DigestHex)
}
return s.String()
}