forked from opcr-io/policy
/
inspect.go
66 lines (51 loc) · 1.41 KB
/
inspect.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
package app
import (
"github.com/bleggett/policy/pkg/parser"
"github.com/pkg/errors"
"oras.land/oras-go/pkg/content"
)
func (c *PolicyApp) Inspect(userRef string) error {
defer c.Cancel()
ref, err := parser.CalculatePolicyRef(userRef, c.Configuration.DefaultDomain)
if err != nil {
return err
}
ociStore, err := content.NewOCI(c.Configuration.PoliciesRoot())
if err != nil {
return err
}
err = ociStore.LoadIndex()
if err != nil {
return err
}
refs := ociStore.ListReferences()
refDescriptor, ok := refs[ref]
if !ok {
return errors.Errorf("policy [%s] not found in the local store", ref)
}
contentInfo, err := ociStore.Info(c.Context, refDescriptor.Digest)
if err != nil {
return errors.Wrapf(err, "failed to read content info for policy [%s]", ref)
}
c.UI.Normal().
WithStringValue("digest", contentInfo.Digest.String()).
WithIntValue("size", contentInfo.Size).
WithStringValue("created_at", contentInfo.CreatedAt.String()).
WithStringValue("updated_at", contentInfo.UpdatedAt.String()).
Do()
if len(refDescriptor.Annotations) > 0 {
msg := c.UI.Normal().WithTable("Annotation", "Value")
for k, v := range refDescriptor.Annotations {
msg.WithTableRow(k, v)
}
msg.Msg("Annotations")
}
if len(contentInfo.Labels) > 0 {
msg := c.UI.Normal().WithTable("Label", "Value")
for k, v := range contentInfo.Labels {
msg.WithTableRow(k, v)
}
msg.Msg("Labels")
}
return nil
}