-
Notifications
You must be signed in to change notification settings - Fork 49
/
manifest_add_digests_cmd.go
108 lines (97 loc) 路 3.08 KB
/
manifest_add_digests_cmd.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
102
103
104
105
106
107
108
package app
import (
"os"
"path/filepath"
"strings"
"github.com/alecthomas/hcl"
"github.com/cashapp/hermit/errors"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/platform"
"github.com/cashapp/hermit/state"
"github.com/cashapp/hermit/ui"
)
type addDigestsCmd struct {
ManifestFiles []string `arg:"" help:"List of files that need to be updated with digests"`
}
func (e *addDigestsCmd) Help() string {
return `
This command will go through each manifest file in input and add missing digest values.
It will use the sha256sums attribute in the manifest files to add digests.
Note: It might download packages that are not in the local cache. So it might take some time.
`
}
func (e *addDigestsCmd) Run(l *ui.UI, state *state.State) error {
for _, f := range e.ManifestFiles {
absolutePath, err := filepath.Abs(f)
if err != nil {
return errors.WithStack(err)
}
dir := filepath.Dir(absolutePath)
packageName := strings.Replace(f, ".hcl", "", 1)
base := filepath.Base(f)
task := l.Task(packageName)
var localmanifest *manifest.AnnotatedManifest
localmanifest, err = manifest.LoadManifestFile(os.DirFS(dir), packageName, base)
if err != nil {
return errors.WithStack(err)
}
if localmanifest.Manifest.SHA256Sums == nil {
localmanifest.Manifest.SHA256Sums = make(map[string]string)
}
err = populateDigests(l, state, localmanifest)
if err != nil {
return errors.WithStack(err)
}
var value []byte
value, err = hcl.Marshal(localmanifest.Manifest)
if err != nil {
return errors.WithStack(err)
}
err = os.WriteFile(f, value, os.ModePerm)
if err != nil {
l.Errorf("Could not write the manifest file %s", f)
return errors.Wrapf(err, "Could not write the manifest file %s", f)
}
task.Done()
if err != nil {
return errors.WithStack(err)
}
return nil
}
return nil
}
func populateDigests(l *ui.UI, state *state.State, localManifest *manifest.AnnotatedManifest) error {
l.Infof("Working on %s package", localManifest.Name)
for _, mc := range localManifest.Manifest.Versions {
ref := manifest.ParseReference(localManifest.Name + "-" + mc.Version[0])
for _, p := range platform.Core {
pkg, err := manifest.NewPackage(localManifest, p, ref)
if err != nil {
l.Tracef("Continuing with the next platform tuple. Current %s: %s", p.OS, p.Arch)
continue
}
// optimize for an already present value.
// Trust model here is that an existing value is correct which is the assumption anyway.
if _, ok := localManifest.Manifest.SHA256Sums[pkg.Source]; ok {
l.Tracef("Skipping shasum for %s as it's already present", pkg.Source)
continue
}
var digest string
digest, err = getDigest(l, state, pkg, ref)
if err != nil {
return errors.WithStack(err)
}
localManifest.Manifest.SHA256Sums[pkg.Source] = digest
}
}
return nil
}
func getDigest(l *ui.UI, state *state.State, pkg *manifest.Package, ref manifest.Reference) (string, error) {
task := l.Task(ref.String())
digest, err := state.CacheAndDigest(task, pkg)
if err != nil {
return "", errors.WithStack(err)
}
task.Done()
return digest, nil
}