|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Point the onkernel/hypeman-go dependency to stainless-sdks/hypeman-go at a specific branch. |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# ./scripts/use-sdk-preview <branch-name> |
| 9 | +# ./scripts/use-sdk-preview <commit-hash> |
| 10 | +# |
| 11 | +# This modifies go.mod with a replace directive. Do not commit this change. |
| 12 | + |
| 13 | +cd "$(dirname "$0")/.." |
| 14 | + |
| 15 | +STAINLESS_SDK_REPO="github.com/stainless-sdks/hypeman-go" |
| 16 | +PUBLIC_SDK_REPO="github.com/onkernel/hypeman-go" |
| 17 | + |
| 18 | +# Ensure the Go toolchain can access the private SDK repository. |
| 19 | +if [[ -z "${GOPRIVATE:-}" ]]; then |
| 20 | + export GOPRIVATE="$STAINLESS_SDK_REPO" |
| 21 | +elif [[ "$GOPRIVATE" != *"$STAINLESS_SDK_REPO"* ]]; then |
| 22 | + export GOPRIVATE="${GOPRIVATE},${STAINLESS_SDK_REPO}" |
| 23 | +fi |
| 24 | + |
| 25 | +# Ensure git rewrites GitHub HTTPS URLs to SSH for private repo access. |
| 26 | +if ! git config --global --get-all url. "[email protected]:".insteadOf | grep -q "https://github.com/"; then |
| 27 | + echo "Your git configuration is missing the rewrite from HTTPS to SSH for GitHub repositories." >&2 |
| 28 | + echo "Run the following command and try again:" >&2 |
| 29 | + echo " git config --global url.\"[email protected]:\".insteadOf \"https://github.com/\"" >&2 |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +usage() { |
| 34 | + echo "Usage: $(basename "$0") <commit-hash|branch-name>" >&2 |
| 35 | + echo "" >&2 |
| 36 | + echo "Point the hypeman-go SDK dependency to stainless-sdks/hypeman-go at a specific ref." >&2 |
| 37 | + echo "" >&2 |
| 38 | + echo "Examples:" >&2 |
| 39 | + echo " $(basename "$0") preview/devices # Use preview/devices branch" >&2 |
| 40 | + echo " $(basename "$0") main # Use main branch" >&2 |
| 41 | + echo " $(basename "$0") abc1234 # Use specific commit" >&2 |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +if [ "$#" -ne 1 ]; then |
| 46 | + usage |
| 47 | +fi |
| 48 | + |
| 49 | +ref="$1" |
| 50 | +commit="" |
| 51 | +tmp_dir="/tmp/hypeman-go" |
| 52 | + |
| 53 | +# Clone the stainless-sdks/hypeman-go repo (shallow clone for speed) |
| 54 | +rm -rf "$tmp_dir" |
| 55 | +echo "==> Cloning stainless-sdks/hypeman-go..." |
| 56 | +git clone --filter=blob:none --quiet [email protected]:stainless-sdks/hypeman-go "$tmp_dir" |
| 57 | + |
| 58 | +# Determine the commit hash corresponding to the provided ref |
| 59 | +pushd "$tmp_dir" >/dev/null |
| 60 | + |
| 61 | +# If the ref looks like a commit SHA (7-40 hex chars), use it directly; otherwise treat it as a branch |
| 62 | +if [[ "$ref" =~ ^[0-9a-f]{7,40}$ ]]; then |
| 63 | + commit="$ref" |
| 64 | +else |
| 65 | + # Fetch the branch and resolve its HEAD commit hash |
| 66 | + git fetch --depth=1 origin "$ref" >/dev/null 2>&1 || { |
| 67 | + echo "Error: failed to fetch branch '$ref' from stainless-sdks/hypeman-go." >&2 |
| 68 | + popd >/dev/null |
| 69 | + exit 1 |
| 70 | + } |
| 71 | + commit=$(git rev-parse FETCH_HEAD) |
| 72 | +fi |
| 73 | + |
| 74 | +# Compute the Go pseudo-version for the resolved commit |
| 75 | +gomod_version=$(git show -s --abbrev=12 \ |
| 76 | + --date=format:%Y%m%d%H%M%S \ |
| 77 | + --format='v0.0.0-%cd-%h' "$commit") |
| 78 | + |
| 79 | +popd >/dev/null |
| 80 | + |
| 81 | +# Verify we're in the CLI module directory |
| 82 | +if [ ! -f go.mod ]; then |
| 83 | + echo "go.mod not found. Please run this script from the hypeman-cli repository root." >&2 |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +echo "==> Updating go.mod to use stainless-sdks/hypeman-go @ $gomod_version..." |
| 88 | + |
| 89 | +# Remove any existing replace directive for the SDK, then add the new one |
| 90 | +go mod edit -dropreplace="$PUBLIC_SDK_REPO" 2>/dev/null || true |
| 91 | +go mod edit -replace="$PUBLIC_SDK_REPO"="$STAINLESS_SDK_REPO"@"$gomod_version" |
| 92 | +go mod tidy |
| 93 | + |
| 94 | +echo "" |
| 95 | +echo "go.mod updated to use $STAINLESS_SDK_REPO @ $gomod_version" |
| 96 | +echo "" |
| 97 | +echo "WARNING: Do not commit this change to go.mod/go.sum!" |
| 98 | + |
0 commit comments