-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtree.go
80 lines (62 loc) · 1.89 KB
/
tree.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
package main
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/xlab/treeprint"
)
var digestFull = false
func FormatDigest(digest string) string {
if digestFull {
return digest
}
return strings.TrimPrefix(digest, "sha256:")[:7]
}
func treeReferrers(w io.Writer, opts treeOptions) error {
if opts.Full {
digestFull = true
}
targetDigest, err := fetchTargetDigest(opts.Subject, opts.Insecure)
if err != nil {
return fmt.Errorf("error getting digest: %w", err)
}
writer := bufio.NewWriter(w)
defer writer.Flush()
writer.Write([]byte(fmt.Sprintf("Subject: %s\n\n", opts.Subject)))
root := treeprint.NewWithRoot(FormatDigest(targetDigest.DigestStr()))
if err := recurseReferrers(root, targetDigest); err != nil {
return fmt.Errorf("error writing referrers: %w", err)
}
writer.Write([]byte(root.String()))
return nil
}
func recurseReferrers(node treeprint.Tree, digest name.Digest) error {
imageIndex, err := remote.Referrers(digest, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return fmt.Errorf("error fetching referrers: %w", err)
}
index, err := imageIndex.IndexManifest()
if err != nil {
return fmt.Errorf("error getting index manifest: %w", err)
}
if len(index.Manifests) == 0 {
return nil
}
for _, manifest := range index.Manifests {
branch := node.AddBranch(fmt.Sprintf("%s: %s", FormatDigest(manifest.Digest.String()), manifest.ArtifactType))
d, err := name.NewDigest(
fmt.Sprintf("%s/%s@%s", digest.RegistryStr(), digest.RepositoryStr(), manifest.Digest.String()),
)
if err != nil {
return fmt.Errorf("error creating digest: %w", err)
}
if err := recurseReferrers(branch, d); err != nil {
return fmt.Errorf("error writing referrers: %w", err)
}
}
return nil
}