Skip to content

Commit

Permalink
Use nix flake prefetch to calculate first hash
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-izval committed Feb 14, 2025
1 parent c14320c commit ba2a5c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
46 changes: 24 additions & 22 deletions cli/cmd/updateVersions.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ var updateVersionsCmd = &cobra.Command{

func updateVersions(token string, versionsPath string, vendorHashPath string, minVersion *semver.Version, maxVersion *semver.Version) error {
nixPrefetchPath, err := exec.LookPath("nix-prefetch")
nurlPath, err := exec.LookPath("nurl")
if err != nil {
return fmt.Errorf("nix-prefetch not found: %w", err)
}
Expand All @@ -103,7 +102,7 @@ func updateVersions(token string, versionsPath string, vendorHashPath string, mi
log.Printf("Version %s found in file\n", version)
} else {
log.Printf("Computing hashes for %s\n", version)
hash, err := computeHash(nurlPath, tagName)
hash, err := computeHash(tagName)
if err != nil {
return fmt.Errorf("Unable to compute hash: %w", err)
}
Expand Down Expand Up @@ -178,16 +177,31 @@ func withReleases(token string, f func(release *github.RepositoryRelease) error)
return nil
}

func computeHash(nurlPath string, tagName string) (string, error) {
hash, err := runNurl(
nurlPath,
fmt.Sprintf("https://github.com/%s/%s", owner, repo),
tagName,
"--hash")
func computeHash(tagName string) (string, error) {
cmd := exec.Command(
"nix", "flake", "prefetch",
"--extra-experimental-features", "nix-command flakes", "--json",
fmt.Sprintf("github:%s/%s/%s", owner, repo, tagName),
)

// Redirect stderr to the standard logger
cmd.Stderr = log.Writer()

// Get the output
output, err := cmd.Output()
if err != nil {
return "", err
return "", fmt.Errorf("command execution failed: %w", err)
}

// Parse JSON output to get hash
var result struct {
Hash string `json:"hash"`
}
return hash, nil
if err := json.Unmarshal(output, &result); err != nil {
return "", fmt.Errorf("failed to parse JSON output: %w", err)
}

return result.Hash, nil
}

func computeVendorHash(nixPrefetchPath string, vendorHashFile string, version *semver.Version, hash string) (string, error) {
Expand Down Expand Up @@ -218,18 +232,6 @@ func runNixPrefetch(nixPrefetchPath string, extraArgs ...string) (string, error)
return strings.TrimRight(string(output), "\n"), nil
}

func runNurl(nurlPath string, args ...string) (string, error) {
cmd := exec.Command(nurlPath, args...)
cmd.Stderr = log.Writer()

output, err := cmd.Output()
if err != nil {
return "", err
}

return strings.TrimSpace(string(output)), nil
}

func init() {
rootCmd.AddCommand(updateVersionsCmd)

Expand Down
1 change: 0 additions & 1 deletion cli/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
packages = [
pkgs.cobra-cli
pkgs.nix-prefetch
pkgs.nurl
];

languages.go.enable = true;
Expand Down
6 changes: 5 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
"hash": "sha256-wJg/BfKWgDzv9HKOsNaq+l2jG37VbOtmBF+QEhNLQ1k=",
"vendorHash": "sha256-YFsPxDlD7SqHo0x2UQnsJ5jDTp2JXdhEgDvtIpWVQ9o="
},
"1.10.5": {
"hash": "sha256-6Y9r3VxL3DRvUaU6hLE+SdqjfIF+PAlXEYBPBc571QE=",
"vendorHash": "sha256-xyFguSjqUweZyoO97nkjLfJWS+eifNV7hpJUjh/6Z54="
},
"1.2.0": {
"hash": "sha256-5um+zS7MVL59SlxchjXdlhBGNdacbQgvg7BRAWnW5XU=",
"vendorHash": "sha256-6x1cv+DKXH2yyMjIA6JY5EkTmWbwH4LBammXKtw2EZo="
Expand Down Expand Up @@ -372,7 +376,7 @@
"latest": {
"1.0": "1.0.11",
"1.1": "1.1.9",
"1.10": "1.10.4",
"1.10": "1.10.5",
"1.2": "1.2.9",
"1.3": "1.3.10",
"1.4": "1.4.7",
Expand Down

0 comments on commit ba2a5c3

Please sign in to comment.