-
Notifications
You must be signed in to change notification settings - Fork 47
/
common.go
158 lines (131 loc) · 3.16 KB
/
common.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright © 2022 - 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"fmt"
"net/url"
"path/filepath"
"gopkg.in/yaml.v3"
"github.com/distribution/distribution/reference"
)
const (
docker = "docker"
oci = "oci"
file = "file"
dir = "dir"
)
// ImageSource represents the source from where an image is created for easy identification
type ImageSource struct {
source string
srcType string
digest string
}
func (i *ImageSource) SetDigest(digest string) {
i.digest = digest
}
func (i ImageSource) GetDigest() string {
return i.digest
}
func (i ImageSource) Value() string {
return i.source
}
func (i ImageSource) IsImage() bool {
return i.srcType == oci
}
func (i ImageSource) IsDir() bool {
return i.srcType == dir
}
func (i ImageSource) IsFile() bool {
return i.srcType == file
}
func (i ImageSource) IsEmpty() bool {
if i.srcType == "" {
return true
}
if i.source == "" {
return true
}
return false
}
func (i ImageSource) String() string {
if i.IsEmpty() {
return ""
}
return fmt.Sprintf("%s://%s", i.srcType, i.source)
}
func (i ImageSource) MarshalYAML() (interface{}, error) {
return i.String(), nil
}
func (i *ImageSource) UnmarshalYAML(value *yaml.Node) error {
return i.updateFromURI(value.Value)
}
func (i *ImageSource) CustomUnmarshal(data interface{}) (bool, error) {
src, ok := data.(string)
if !ok {
return false, fmt.Errorf("can't unmarshal %+v to an ImageSource type", data)
}
err := i.updateFromURI(src)
return false, err
}
func (i *ImageSource) updateFromURI(uri string) error {
u, err := url.Parse(uri)
if err != nil {
return err
}
scheme := u.Scheme
value := u.Opaque
if value == "" {
value = filepath.Join(u.Host, u.Path)
}
switch scheme {
case oci, docker:
return i.parseImageReference(value)
case dir:
i.srcType = dir
i.source = value
case file:
i.srcType = file
i.source = value
default:
return i.parseImageReference(uri)
}
return nil
}
func (i *ImageSource) parseImageReference(ref string) error {
n, err := reference.ParseNormalizedNamed(ref)
if err != nil {
return fmt.Errorf("invalid image reference %s", ref)
} else if reference.IsNameOnly(n) {
ref += ":latest"
}
i.srcType = oci
i.source = ref
return nil
}
func NewSrcFromURI(uri string) (*ImageSource, error) {
src := ImageSource{}
err := src.updateFromURI(uri)
return &src, err
}
func NewEmptySrc() *ImageSource {
return &ImageSource{}
}
func NewDockerSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: oci}
}
func NewFileSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: file}
}
func NewDirSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: dir}
}