skills/skills/update-opencode/scripts/fetch-sha256.sh
dan 99187460b1 feat(update-opencode): add skill for automating OpenCode version updates in Nix
Implement complete workflow for checking and applying OpenCode updates:
- check-version.sh: Compare current vs latest GitHub release
- fetch-sha256.sh: Fetch SHA256 hash using nix-prefetch-url
- update-nix-file.sh: Update Nix package definition with dry-run support
- verify-update.sh: Verify installed version matches expectation

Includes comprehensive documentation (SKILL.md for agents, README.md for users)
and full spec in specs/002-update-opencode/ with user stories and acceptance criteria.

Eliminates manual Nix file editing and hash lookups for OpenCode updates.
2025-11-15 13:35:58 -08:00

65 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# fetch-sha256.sh - Fetch and compute SRI hash for OpenCode release
#
# Usage: fetch-sha256.sh <version>
#
# Example: fetch-sha256.sh 1.0.58
# Output: sha256-ABC123... (SRI format)
# Check arguments
if [[ $# -ne 1 ]]; then
echo "Error: Missing required argument" >&2
echo "Usage: $0 <version>" >&2
echo "Example: $0 1.0.58" >&2
exit 1
fi
VERSION="$1"
# Validate version format (X.Y.Z)
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "Error: Invalid version format '$VERSION' (expected X.Y.Z)" >&2
exit 1
fi
# Check dependencies
for cmd in nix-prefetch-url nix; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Required command '$cmd' not found" >&2
exit 1
fi
done
# Construct download URL
URL="https://github.com/sst/opencode/releases/download/v${VERSION}/opencode-linux-x64.zip"
# Fetch file and compute Nix base-32 hash
# Note: nix-prefetch-url outputs hash to stdout, potential warnings/info to stderr
# Allow nix-prefetch-url to fail gracefully so we can provide a custom error message
NIX_HASH=$(nix-prefetch-url --type sha256 "$URL" 2>/dev/null | head -1 || true)
if [[ -z "$NIX_HASH" ]]; then
echo "Error: Failed to fetch or hash $URL" >&2
echo " Verify version $VERSION exists on GitHub releases" >&2
exit 1
fi
# Convert Nix base-32 hash to SRI format (sha256-...)
SRI_HASH=$(nix hash to-sri --type sha256 "$NIX_HASH" 2>/dev/null)
if [[ -z "$SRI_HASH" ]]; then
echo "Error: Failed to convert hash to SRI format" >&2
exit 1
fi
# Validate SRI hash format
if ! echo "$SRI_HASH" | grep -qE '^sha256-[A-Za-z0-9+/]+=*$'; then
echo "Error: Generated hash '$SRI_HASH' does not match SRI format" >&2
exit 1
fi
# Output SRI hash
echo "$SRI_HASH"