From 4be6e521f639617fefda4c3f32dda41aa36199f6 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 3 Dec 2024 04:23:53 +0000 Subject: feat(yae): track sri hash field for sources --- internal/yae/source.go | 10 +++++++++- internal/yae/utilities.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/yae/source.go b/internal/yae/source.go index 735e4d4..20d16dc 100644 --- a/internal/yae/source.go +++ b/internal/yae/source.go @@ -11,6 +11,7 @@ import ( type Source struct { URL string `json:"url"` SHA256 string `json:"sha256"` + Hash string `json:"hash"` Unpack bool `json:"unpack"` Type string `json:"type"` Version string `json:"version,omitempty"` @@ -78,10 +79,17 @@ func (source *Source) Update(sources *Environment, name string, force bool, forc return updated, err } - if sha256 != source.SHA256 { + sriHash, err := FetchSRIHash(sha256) + + if err != nil { + return updated, err + } + + if sha256 != source.SHA256 || sriHash != source.Hash || force || source.Force { log.Infof("rehashed %s: %s -> %s", name, source.SHA256, sha256) source.SHA256 = sha256 + source.Hash = sriHash updated = true } diff --git a/internal/yae/utilities.go b/internal/yae/utilities.go index ef9d334..73042fd 100644 --- a/internal/yae/utilities.go +++ b/internal/yae/utilities.go @@ -25,6 +25,16 @@ func FetchSHA256(url string, unpack bool) (string, error) { return strings.Trim(lines[len(lines)-2], "\n"), nil } +func FetchSRIHash(sha256 string) (string, error) { + output, err := command("nix", false, "hash", "convert", "--hash-algo", "sha256", "--from", "nix32", sha256) + + if err != nil { + return "", err + } + + return strings.Trim(output, "\n"), nil +} + func command(name string, show bool, args ...string) (string, error) { executable, err := exec.LookPath(name) out := []byte{} -- cgit v1.2.3