/
input_image_reference.go
101 lines (83 loc) · 1.92 KB
/
input_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
package client
import (
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
)
type InputImageReference interface {
Name() string
Layout() bool
FullName() (string, error)
}
type defaultInputImageReference struct {
name string
}
type layoutInputImageReference struct {
name string
}
func ParseInputImageReference(input string) InputImageReference {
if strings.HasPrefix(input, "oci:") {
imageNameParsed := strings.SplitN(input, ":", 2)
return &layoutInputImageReference{
name: imageNameParsed[1],
}
}
return &defaultInputImageReference{
name: input,
}
}
func (d *defaultInputImageReference) Name() string {
return d.name
}
func (d *defaultInputImageReference) Layout() bool {
return false
}
func (d *defaultInputImageReference) FullName() (string, error) {
return d.name, nil
}
func (l *layoutInputImageReference) Name() string {
return filepath.Base(l.name)
}
func (l *layoutInputImageReference) Layout() bool {
return true
}
func (l *layoutInputImageReference) FullName() (string, error) {
var (
fullImagePath string
err error
)
path := parsePath(l.name)
if fullImagePath, err = filepath.EvalSymlinks(path); err != nil {
if !os.IsNotExist(err) {
return "", errors.Wrap(err, "evaluate symlink")
} else {
fullImagePath = path
}
}
if fullImagePath, err = filepath.Abs(fullImagePath); err != nil {
return "", errors.Wrap(err, "resolve absolute path")
}
return fullImagePath, nil
}
func parsePath(path string) string {
var result string
if filepath.IsAbs(path) && runtime.GOOS == "windows" {
dir, fileWithTag := filepath.Split(path)
file := removeTag(fileWithTag)
result = filepath.Join(dir, file)
} else {
result = removeTag(path)
}
return result
}
func removeTag(path string) string {
result := path
if strings.Contains(path, ":") {
split := strings.SplitN(path, ":", 2)
// do not include the tag in the path
result = split[0]
}
return result
}