-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathknowntags.go
98 lines (82 loc) · 2.16 KB
/
knowntags.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
package main
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
)
// a hash->name pair to read in from the known layers json
type LayerNameMapEntry struct {
Hash string `json:"hash"`
Name string `json:"name"`
}
// global map of layer hashes to known names
var LayerNameMap = map[string][]string{}
// todo: be nice if this did a smart shortening, like replacing names with common prefixes and tags like
// foo.com/{imageone,imagetwo}:commontag
func getShortStringForNames(names []string) []string {
// map from tag-> names
namesWithCommonTags := map[string][]string{}
truncatedLayerNames := []string{}
if len(names) == 1 {
return names
}
for _, name := range names {
baseName := filepath.Base(name)
ss := strings.Split(baseName, ":")
if len(ss) == 2 {
namesWithCommonTags[ss[1]] = append(namesWithCommonTags[ss[1]], ss[0])
} else {
namesWithCommonTags[""] = append(namesWithCommonTags[""], baseName)
}
}
for tag, names := range namesWithCommonTags {
tagstr := fmt.Sprintf("{%s}%s", strings.Join(names, ","), tag)
truncatedLayerNames = append(truncatedLayerNames, tagstr)
}
return truncatedLayerNames
}
func getNamesForHash(digest string) []string {
layerNames, ok := LayerNameMap[digest]
if !ok {
layerNames = []string{"?"}
}
return layerNames
}
func getUserOrSudoUserHomedir() string {
username := os.Getenv("SUDO_USER")
if username == "" {
username = os.Getenv("USER")
}
u, err := user.Lookup(username)
if err != nil {
panic(err)
}
return u.HomeDir
}
func getKnowLayersFilename() string {
userHome := getUserOrSudoUserHomedir()
return filepath.Join(userHome, ".cache/ociv/known-layers.json")
}
func makeCacheDir() {
userHome := getUserOrSudoUserHomedir()
cacheDir := filepath.Join(userHome, ".cache/ociv")
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0755); err != nil {
panic(err)
}
}
}
func setupWellKnownLayerNames() {
fname := getKnowLayersFilename()
entries, err := Load(fname)
if err != nil {
log.Printf("WARN: can't read %s: %s", fname, err.Error())
return
}
for _, e := range entries {
LayerNameMap[e.Hash] = append(LayerNameMap[e.Hash], e.Name)
}
}