Skip to content

Instantly share code, notes, and snippets.

@natebrennand
Created November 6, 2020 17:51
Show Gist options
  • Save natebrennand/a8294de74cd2cf896cf349d57facc895 to your computer and use it in GitHub Desktop.
Save natebrennand/a8294de74cd2cf896cf349d57facc895 to your computer and use it in GitHub Desktop.
Generate SHA's for all rules_go platforms.

rules_go SDK SHA Generator

rules_go maintains support for many platforms. This scripts makes generating the sha256 for all the platforms and buliding the entry to sdk_list.bzl simple. And the calculation is performed directly from the io.Reader so you're not left with 10 archives on disk.

❯❯❯ go run generate.rules.go.shas.go --version=1.15.4
2020/11/06 09:42:46 darwin_amd64 -> aaf8c5323e0557211680960a8f51bedf98ab9a368775a687d6cf1f0079232b1d
2020/11/06 09:42:49 freebsd_386 -> 37874f0df879d72972c684006ff3826a9fa89400f01afcc89576e767d27781e2
2020/11/06 09:42:54 freebsd_amd64 -> c1a9d20dc190ed40ec78d5e77337b1db187a9634e7c485505a8b9afbcc1e4738
2020/11/06 09:42:57 linux_386 -> 6b2f6d8afddfb198bf0e36044084dc4db4cb0be1107375240b34d215aa5ff6ad
2020/11/06 09:43:01 linux_amd64 -> eb61005f0b932c93b424a3a4eaa67d72196c79129d9a3ea8578047683e2c80d5
2020/11/06 09:43:03 linux_arm64 -> 6f083b453484fc5f95afb345547a58ccc957cde91348b7a7c68f5b060e488c85
2020/11/06 09:43:06 linux_arm -> fe449ad3e121472e5db2f70becc0fef9d1a7188616c0605ada63f1e3bbad280e
2020/11/06 09:43:09 linux_ppc64le -> d041ca820d47ccec9cd89abf34266aaedb0c74dc11f51accdc15a5a67679a990
2020/11/06 09:43:11 linux_s390x -> c580c201ae9632f00770fad3d648f479effe566f4ce69beb533c7fd2f98673c7
2020/11/06 09:43:15 windows_386 -> 3be3cfc08ccc7e7056fdee17b6f5d18e9d7f3d1351dcfec8de34b1c95cb05b50
2020/11/06 09:43:18 windows_amd64 -> 3593204e3851be577e4209900ece031b36f1e9ce1671f3f3221c9af7a090a941
    "1.15.4": {
        "darwin_amd64": ("go1.15.4.darwin-amd64.tar.gz", "aaf8c5323e0557211680960a8f51bedf98ab9a368775a687d6cf1f0079232b1d"),
        "freebsd_386": ("go1.15.4.freebsd-386.tar.gz", "37874f0df879d72972c684006ff3826a9fa89400f01afcc89576e767d27781e2"),
        "freebsd_amd64": ("go1.15.4.freebsd-amd64.tar.gz", "c1a9d20dc190ed40ec78d5e77337b1db187a9634e7c485505a8b9afbcc1e4738"),
        "linux_386": ("go1.15.4.linux-386.tar.gz", "6b2f6d8afddfb198bf0e36044084dc4db4cb0be1107375240b34d215aa5ff6ad"),
        "linux_amd64": ("go1.15.4.linux-amd64.tar.gz", "eb61005f0b932c93b424a3a4eaa67d72196c79129d9a3ea8578047683e2c80d5"),
        "linux_arm64": ("go1.15.4.linux-arm64.tar.gz", "6f083b453484fc5f95afb345547a58ccc957cde91348b7a7c68f5b060e488c85"),
        "linux_arm": ("go1.15.4.linux-armv6l.tar.gz", "fe449ad3e121472e5db2f70becc0fef9d1a7188616c0605ada63f1e3bbad280e"),
        "linux_ppc64le": ("go1.15.4.linux-ppc64le.tar.gz", "d041ca820d47ccec9cd89abf34266aaedb0c74dc11f51accdc15a5a67679a990"),
        "linux_s390x": ("go1.15.4.linux-s390x.tar.gz", "c580c201ae9632f00770fad3d648f479effe566f4ce69beb533c7fd2f98673c7"),
        "windows_386": ("go1.15.4.windows-386.zip", "3be3cfc08ccc7e7056fdee17b6f5d18e9d7f3d1351dcfec8de34b1c95cb05b50"),
        "windows_amd64": ("go1.15.4.windows-amd64.zip", "3593204e3851be577e4209900ece031b36f1e9ce1671f3f3221c9af7a090a941"),
    },
package main
import (
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"net/http"
"strings"
)
type platformSpec struct {
version string
platform string
pkgSuffix string
sha string
}
func (p platformSpec) downloadURL() string {
return fmt.Sprintf("https://dl.google.com/go/go%s.%s", p.version, p.pkgSuffix)
}
// format as per https://github.com/bazelbuild/rules_go/blob/master/go/private/sdk_list.bzl
func (p platformSpec) buildPkgName() string {
return fmt.Sprintf("go%s.%s", p.version, p.pkgSuffix)
}
func (p platformSpec) sdkListEntry(prefix string) string {
return fmt.Sprintf(`%s"%s": ("go%s.%s", "%s"),`,
prefix,
p.platform,
p.version,
p.pkgSuffix,
p.sha,
)
}
func getBuilds(version string) []platformSpec {
return []platformSpec{
{version: version, platform: "darwin_amd64", pkgSuffix: "darwin-amd64.tar.gz"},
{version: version, platform: "freebsd_386", pkgSuffix: "freebsd-386.tar.gz"},
{version: version, platform: "freebsd_amd64", pkgSuffix: "freebsd-amd64.tar.gz"},
{version: version, platform: "linux_386", pkgSuffix: "linux-386.tar.gz"},
{version: version, platform: "linux_amd64", pkgSuffix: "linux-amd64.tar.gz"},
{version: version, platform: "linux_arm64", pkgSuffix: "linux-arm64.tar.gz"},
{version: version, platform: "linux_arm", pkgSuffix: "linux-armv6l.tar.gz"},
{version: version, platform: "linux_ppc64le", pkgSuffix: "linux-ppc64le.tar.gz"},
{version: version, platform: "linux_s390x", pkgSuffix: "linux-s390x.tar.gz"},
{version: version, platform: "windows_386", pkgSuffix: "windows-386.zip"},
{version: version, platform: "windows_amd64", pkgSuffix: "windows-amd64.zip"},
}
}
func main() {
goVersion := flag.String("version", "", "Go version to create SHA's for")
flag.Parse()
if goVersion == nil || *goVersion == "" {
log.Fatalf("version flag must be populated")
}
specs := getBuilds(*goVersion)
for i, platform := range specs {
dlURL := platform.downloadURL()
resp, err := http.Get(dlURL)
if err != nil {
log.Fatalf("failed to make request to %s", dlURL)
}
defer resp.Body.Close()
h := sha256.New()
n, err := io.Copy(h, resp.Body)
if err != nil {
log.Fatalf("failed to download %s: %s", dlURL, err)
} else if n == 0 {
log.Fatalf("no data returned from %s", dlURL)
}
specs[i].sha = hex.EncodeToString(h.Sum(nil))
log.Printf("%s -> %s", platform.platform, specs[i].sha)
}
entries := make([]string, len(specs))
for i := range entries {
entries[i] = specs[i].sdkListEntry(" ")
}
fmt.Println(fmt.Sprintf(` "%s": {
%s
},`, *goVersion, strings.Join(entries, "\n")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment