Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add rehash command prototype #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions cmd/bldr/cmd/rehash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package cmd

import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
"net/http"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(rehashCmd)
}

var rehashCmd = &cobra.Command{
Use: "update",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update or rehash?

Short: "Update pkgs",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
for _, d := range datas {
if err := doTemplate(
template.Must(template.New(d["url"]).Funcs(sprig.HermeticTxtFuncMap()).Parse(d["url"])),
d,
); err != nil {
return err
}
}

return nil
},
}

// datas is a list of deps to update. Provided as an example.
//
//nolint:lll
var datas = []map[string]string{
{"VERSION": "v1.30.3", "url": "https://github.com/kubernetes/cloud-provider-aws/archive/refs/tags/{{ .VERSION }}.tar.gz"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we extract that automatically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to bldr update itself, i was thinking of doing something similar to renovate, have additional fields, something like:

steps:
  - sources:
      - url: https://download.qemu.org/qemu-{{ .QEMU_VERSION }}.tar.xz
        destination: qemu.tar.xz
        sha256: a8c3f596aece96da3b00cafb74baafa0d14515eafb8ed1ee3f7f5c2d0ebf02b6
        sha512: 58ed84f6fe6263d279356bc9193f96edf62cf3663fb151daa3f047d52329fe49cb91c2d45e09697e0469f4f5409be96403aec9572d4871ffa40848a786c21599
        update_options:
            uri: github://qemu/qemu-guest-agent
            filter: <some filtering option>
      - url: https://download.nvidia.org/nvidia-{{ .NVIDIA_VERSION }}.tar.xz
        destination: nvidia.tar.xz
        sha256: a8c3f596aece96da3b00cafb74baafa0d14515eafb8ed1ee3f7f5c2d0ebf02b6
        sha512: 58ed84f6fe6263d279356bc9193f96edf62cf3663fb151daa3f047d52329fe49cb91c2d45e09697e0469f4f5409be96403aec9572d4871ffa40848a786c21599
        update_options:
            uri: url:download.nvidia.org/releases.json
            filter: <some filtering option> // maybe use cue to filter from json or use `.foo.bar` syntax

this was we can mostly get rid of renovate

{"GLIB_VERSION": "2.81.1", "url": "https://download.gnome.org/sources/glib/{{ regexReplaceAll \".\\\\d+$\" .GLIB_VERSION \"${1}\" }}/glib-{{ .GLIB_VERSION }}.tar.xz"},
{"GVISOR_VERSION": "20240729.0", "url": "https://github.com/google/gvisor/archive/3f38cb19ba373b027f5220450591daa3ab767145.tar.gz"},
{"QEMU_VERSION": "9.0.2", "url": "https://download.qemu.org/qemu-{{ .QEMU_VERSION }}.tar.xz"},
{"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"},
{"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"},
{"TAILSCALE_VERSION": "1.70.0", "url": "https://github.com/tailscale/tailscale/archive/refs/tags/v{{ .TAILSCALE_VERSION }}.tar.gz"},
{"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"},
}

func doTemplate(tmpl *template.Template, data map[string]string) error {
var bldr strings.Builder

err := tmpl.Execute(&bldr, data)
if err != nil {
return fmt.Errorf("failed to execute the template '%s': %w", tmpl.Name(), err)
}

s256, s512 := sha256.New(), sha512.New()

result, err := http.Get(bldr.String()) //nolint:noctx
if err != nil {
return fmt.Errorf("failed to fetch the URL '%s': %w", bldr.String(), err)
}

defer result.Body.Close() //nolint:errcheck

if result.StatusCode != http.StatusOK {
return fmt.Errorf("code %d != 200 for URL: %s", result.StatusCode, bldr.String())
}

_, err = io.Copy(io.MultiWriter(s256, s512), result.Body)
if err != nil {
return fmt.Errorf("failed to process the content of the URL '%s': %w", bldr.String(), err)
}

fmt.Println("URL:", bldr.String())
fmt.Println("sha256:", fmt.Sprintf("%x", s256.Sum(nil)))
fmt.Println("sha512:", fmt.Sprintf("%x", s512.Sum(nil)))

return nil
}