-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
images.go
101 lines (87 loc) · 1.82 KB
/
images.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 ecrm
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
)
// ImageURI represents an image URI.
type ImageURI string
func (u ImageURI) IsECRImage() bool {
return strings.Contains(string(u), ".dkr.ecr.")
}
func (u ImageURI) IsDigestURI() bool {
return strings.Contains(string(u), "@")
}
func (u ImageURI) Tag() string {
if u.IsDigestURI() {
return ""
}
s := strings.SplitN(string(u), ":", 2)
if len(s) == 2 {
return s[1]
}
return ""
}
func (u ImageURI) Base() string {
if u.IsDigestURI() {
s := strings.SplitN(string(u), "@", 2)
return s[0]
} else if strings.Contains(string(u), ":") {
s := strings.SplitN(string(u), ":", 2)
return s[0]
} else {
return string(u)
}
}
func (u ImageURI) String() string {
return string(u)
}
func (u ImageURI) Short() string {
return strings.SplitN(string(u), "/", 2)[1]
}
type Images map[ImageURI]set
func (i Images) Print(w io.Writer) error {
m := make([]string, 0, len(i))
for k := range i {
m = append(m, string(k))
}
sort.Strings(m)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(m); err != nil {
return fmt.Errorf("failed to encode image uris: %w", err)
}
return nil
}
func (i Images) LoadFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
in := []string{}
if err := json.NewDecoder(f).Decode(&in); err != nil {
return fmt.Errorf("failed to decode images: %w", err)
}
for _, u := range in {
i[ImageURI(u)] = newSet(filename)
}
return nil
}
func (i Images) Add(u ImageURI, usedBy string) bool {
if _, ok := i[u]; !ok {
i[u] = newSet()
}
return i[u].add(usedBy)
}
func (i Images) Contains(u ImageURI) bool {
return !i[u].isEmpty()
}
func (i Images) Merge(j Images) {
for k, v := range j {
i[k] = i[k].union(v)
}
}