Skip to content

Commit b528bde

Browse files
author
Dmitriy Matrenichev
committed
chore: add rehash command prototype
Something to think about in future or even merge with `update` command. Signed-off-by: Dmitriy Matrenichev <[email protected]>
1 parent 9a526ec commit b528bde

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

cmd/bldr/cmd/rehash.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package cmd
6+
7+
import (
8+
"crypto/sha256"
9+
"crypto/sha512"
10+
"fmt"
11+
"io"
12+
"net/http"
13+
"strings"
14+
"text/template"
15+
16+
"github.com/Masterminds/sprig/v3"
17+
"github.com/spf13/cobra"
18+
)
19+
20+
func init() {
21+
rootCmd.AddCommand(rehashCmd)
22+
}
23+
24+
var rehashCmd = &cobra.Command{
25+
Use: "update",
26+
Short: "Update pkgs",
27+
Args: cobra.NoArgs,
28+
RunE: func(_ *cobra.Command, _ []string) error {
29+
for _, d := range datas {
30+
if err := doTemplate(
31+
template.Must(template.New(d["url"]).Funcs(sprig.HermeticTxtFuncMap()).Parse(d["url"])),
32+
d,
33+
); err != nil {
34+
return err
35+
}
36+
}
37+
38+
return nil
39+
},
40+
}
41+
42+
// datas is a list of deps to update. Provided as an example.
43+
var datas = []map[string]string{
44+
{"VERSION": "v1.30.3", "url": "https://github.com/kubernetes/cloud-provider-aws/archive/refs/tags/{{ .VERSION }}.tar.gz"},
45+
{"GLIB_VERSION": "2.81.1", "url": "https://download.gnome.org/sources/glib/{{ regexReplaceAll \".\\\\d+$\" .GLIB_VERSION \"${1}\" }}/glib-{{ .GLIB_VERSION }}.tar.xz"},
46+
{"GVISOR_VERSION": "20240729.0", "url": "https://github.com/google/gvisor/archive/3f38cb19ba373b027f5220450591daa3ab767145.tar.gz"},
47+
{"QEMU_VERSION": "9.0.2", "url": "https://download.qemu.org/qemu-{{ .QEMU_VERSION }}.tar.xz"},
48+
{"SPIN_VERSION": "v0.15.1", "url": "https://github.com/spinkube/containerd-shim-spin/releases/download/{{ .SPIN_VERSION }}/containerd-shim-spin-v2-linux-aarch64.tar.gz"},
49+
{"SPIN_VERSION": "v0.15.1", "url": "https://github.com/spinkube/containerd-shim-spin/releases/download/{{ .SPIN_VERSION }}/containerd-shim-spin-v2-linux-x86_64.tar.gz"},
50+
{"TAILSCALE_VERSION": "1.70.0", "url": "https://github.com/tailscale/tailscale/archive/refs/tags/v{{ .TAILSCALE_VERSION }}.tar.gz"},
51+
{"UTIL_LINUX_VERSION": "2.40.2", "url": "https://www.kernel.org/pub/linux/utils/util-linux/v{{ regexReplaceAll \".\\\\d+$\" .UTIL_LINUX_VERSION \"${1}\" }}/util-linux-{{ regexReplaceAll \"\\\\.0$\" .UTIL_LINUX_VERSION \"${1}\" }}.tar.xz"},
52+
}
53+
54+
func doTemplate(tmpl *template.Template, data map[string]string) error {
55+
var bldr strings.Builder
56+
57+
err := tmpl.Execute(&bldr, data)
58+
if err != nil {
59+
return fmt.Errorf("failed to execute the template '%s': %w", tmpl.Name(), err)
60+
}
61+
62+
s256, s512 := sha256.New(), sha512.New()
63+
64+
result, err := http.Get(bldr.String())
65+
if err != nil {
66+
return fmt.Errorf("failed to fetch the URL '%s': %w", bldr.String(), err)
67+
}
68+
69+
defer result.Body.Close()
70+
71+
if result.StatusCode != http.StatusOK {
72+
return fmt.Errorf("code %d != 200 for URL: %s", result.StatusCode, bldr.String())
73+
}
74+
75+
_, err = io.Copy(io.MultiWriter(s256, s512), result.Body)
76+
if err != nil {
77+
return fmt.Errorf("failed to process the content of the URL '%s': %w", bldr.String(), err)
78+
}
79+
80+
fmt.Println("URL:", bldr.String())
81+
fmt.Println("sha256:", fmt.Sprintf("%x", s256.Sum(nil)))
82+
fmt.Println("sha512:", fmt.Sprintf("%x", s512.Sum(nil)))
83+
84+
return nil
85+
}

0 commit comments

Comments
 (0)