-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
96 lines (79 loc) · 1.94 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
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
package clade
import (
"fmt"
"strings"
"github.com/distribution/distribution/v3/reference"
"github.com/opencontainers/go-digest"
"gopkg.in/yaml.v3"
)
type ImageReference struct {
reference.Named
Tag *Pipeline
Alias string
}
func (r *ImageReference) FromNameTag(name string, tag string) error {
named, err := reference.ParseNamed(name)
if err != nil {
return fmt.Errorf("parse reference name: %w", err)
} else {
r.Named = named
}
if strings.HasPrefix(tag, "(") && strings.HasSuffix(tag, ")") {
// Pipeline expression
} else if strings.ContainsRune(tag, ':') {
if _, err := reference.WithDigest(named, digest.Digest(tag)); err != nil {
return err
}
} else {
if _, err := reference.WithTag(named, tag); err != nil {
return err
}
}
if err := yaml.Unmarshal([]byte(tag), &r.Tag); err != nil {
return fmt.Errorf("unmarshal reference tag: %w", err)
}
return nil
}
func (r *ImageReference) unmarshalYamlScalar(node *yaml.Node) error {
ref := ""
if err := node.Decode(&ref); err != nil {
return err
}
pos := strings.LastIndex(ref, "/") + 1
pos += strings.IndexAny(ref[pos:], ":@")
if (ref[pos] == '@') && !strings.ContainsRune(ref[pos+1:], ':') {
return reference.ErrDigestInvalidFormat
}
if err := r.FromNameTag(ref[:pos], ref[pos+1:]); err != nil {
return err
}
return nil
}
func (r *ImageReference) unmarshalYamlMap(node *yaml.Node) error {
var ref struct {
Name string
Tag string
As string
}
if err := node.Decode(&ref); err != nil {
return err
}
if err := r.FromNameTag(ref.Name, ref.Tag); err != nil {
return err
}
r.Alias = ref.As
return nil
}
func (r *ImageReference) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case yaml.ScalarNode:
return r.unmarshalYamlScalar(node)
case yaml.MappingNode:
return r.unmarshalYamlMap(node)
}
return &yaml.TypeError{Errors: []string{"must be string or map"}}
}
type ResolvedImageReference struct {
reference.NamedTagged
Alias string
}