-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
94 lines (86 loc) · 2.17 KB
/
app.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
package compose
import (
"context"
"fmt"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/reference"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"strings"
)
type (
AppRef struct {
Spec reference.Spec
Repo string
Name string
Tag string
Digest digest.Digest
}
AppTree TreeNode
App interface {
Name() string
Ref() *AppRef
HasLayersMeta(arch string) bool
GetBlobRuntimeSize(desc *ocispec.Descriptor, arch string, blockSize int64) int64
GetComposeRoot() *TreeNode
}
AppLoader interface {
LoadAppTree(context.Context, BlobProvider, platforms.MatchComparer, string) (App, *AppTree, error)
}
)
func ParseAppRef(ref string) (*AppRef, error) {
s, err := reference.Parse(ref)
if err != nil {
return nil, err
}
hostNameLen := len(s.Hostname())
if hostNameLen == len(ref) {
return nil, fmt.Errorf("invalid app reference: digest must be specified (host/repo/name@sha256:<hash>)")
}
appName := s.Locator[hostNameLen+1:]
i := strings.Index(appName, "/")
repo := ""
if i > 0 {
repo = appName[:i]
appName = appName[i+1:]
}
t, d := reference.SplitObject(s.Object)
return &AppRef{
Spec: s,
Repo: repo,
Name: appName,
Tag: t,
Digest: d,
}, nil
}
func (r *AppRef) String() string {
return r.Spec.String()
}
func (r *AppRef) GetBlobRef(digest digest.Digest) string {
return r.Spec.Locator + "@" + digest.String()
}
func (t *AppTree) Walk(fn NodeProcessor) error {
return TreeNode(*t).Walk(fn)
}
func (t *AppTree) Print() {
err := t.Walk(func(node *TreeNode, depth int) error {
if depth == 0 {
id := node.Descriptor.Digest.String()
if len(node.Descriptor.URLs) > 0 {
id = node.Descriptor.URLs[0]
}
fmt.Printf("%s: %s, %d\n", node.Type, id, node.Descriptor.Size)
} else if depth == 1 {
fmt.Printf("%*s\n", 9, "|")
fmt.Printf("%*s %s: %s, %d\n", 11, "|—>", node.Type, node.Descriptor.Digest.String(), node.Descriptor.Size)
} else if depth == 2 {
fmt.Printf("%*s\n", 9*depth, "|")
ImageTree(*node).Print(depth)
fmt.Println()
}
return nil
})
if err != nil {
fmt.Printf("Failed to print image tree: %s\n", err.Error())
}
}