|
| 1 | +#!/usr/bin/env sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +REPO_OWNER="RewriteToday" |
| 5 | +REPO_NAME="cli" |
| 6 | +BINARY_NAME="rewrite" |
| 7 | +INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" |
| 8 | +VERSION="${VERSION:-latest}" |
| 9 | + |
| 10 | +need_cmd() { |
| 11 | + command -v "$1" >/dev/null 2>&1 || { |
| 12 | + echo "missing required command: $1" >&2 |
| 13 | + exit 1 |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +fetch_url() { |
| 18 | + url="$1" |
| 19 | + out="$2" |
| 20 | + |
| 21 | + if command -v curl >/dev/null 2>&1; then |
| 22 | + curl -fsSL "$url" -o "$out" |
| 23 | + return |
| 24 | + fi |
| 25 | + |
| 26 | + if command -v wget >/dev/null 2>&1; then |
| 27 | + wget -qO "$out" "$url" |
| 28 | + return |
| 29 | + fi |
| 30 | + |
| 31 | + echo "curl or wget is required" >&2 |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +resolve_latest_version() { |
| 36 | + latest_url="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/latest" |
| 37 | + |
| 38 | + if command -v curl >/dev/null 2>&1; then |
| 39 | + final_url=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$latest_url") |
| 40 | + elif command -v wget >/dev/null 2>&1; then |
| 41 | + final_url=$(wget -qO- --max-redirect=0 "$latest_url" 2>&1 | awk '/^ Location: /{print $2}' | tr -d '\r') |
| 42 | + else |
| 43 | + echo "curl or wget is required" >&2 |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + |
| 47 | + tag=$(basename "$final_url") |
| 48 | + if [ -z "$tag" ]; then |
| 49 | + echo "failed to resolve latest release tag" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + |
| 53 | + echo "$tag" |
| 54 | +} |
| 55 | + |
| 56 | +normalize_os() { |
| 57 | + os=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 58 | + case "$os" in |
| 59 | + linux) echo "linux" ;; |
| 60 | + darwin) echo "darwin" ;; |
| 61 | + mingw*|msys*|cygwin*) echo "windows" ;; |
| 62 | + *) |
| 63 | + echo "unsupported OS: $os" >&2 |
| 64 | + exit 1 |
| 65 | + ;; |
| 66 | + esac |
| 67 | +} |
| 68 | + |
| 69 | +normalize_arch() { |
| 70 | + arch=$(uname -m) |
| 71 | + case "$arch" in |
| 72 | + x86_64|amd64) echo "amd64" ;; |
| 73 | + aarch64|arm64) echo "arm64" ;; |
| 74 | + *) |
| 75 | + echo "unsupported architecture: $arch" >&2 |
| 76 | + exit 1 |
| 77 | + ;; |
| 78 | + esac |
| 79 | +} |
| 80 | + |
| 81 | +install_binary() { |
| 82 | + bin_path="$1" |
| 83 | + chmod +x "$bin_path" |
| 84 | + |
| 85 | + if [ -w "$INSTALL_DIR" ]; then |
| 86 | + mv "$bin_path" "$INSTALL_DIR/$BINARY_NAME" |
| 87 | + else |
| 88 | + need_cmd sudo |
| 89 | + sudo mv "$bin_path" "$INSTALL_DIR/$BINARY_NAME" |
| 90 | + fi |
| 91 | +} |
| 92 | + |
| 93 | +main() { |
| 94 | + need_cmd uname |
| 95 | + need_cmd tar |
| 96 | + |
| 97 | + if [ "$VERSION" = "latest" ]; then |
| 98 | + VERSION=$(resolve_latest_version) |
| 99 | + fi |
| 100 | + |
| 101 | + os=$(normalize_os) |
| 102 | + arch=$(normalize_arch) |
| 103 | + |
| 104 | + asset_ext="tar.gz" |
| 105 | + if [ "$os" = "windows" ]; then |
| 106 | + need_cmd unzip |
| 107 | + asset_ext="zip" |
| 108 | + fi |
| 109 | + |
| 110 | + asset_name="${BINARY_NAME}_${VERSION}_${os}_${arch}.${asset_ext}" |
| 111 | + download_url="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${VERSION}/${asset_name}" |
| 112 | + |
| 113 | + tmp_dir=$(mktemp -d) |
| 114 | + trap 'rm -rf "$tmp_dir"' EXIT INT TERM |
| 115 | + |
| 116 | + archive_path="$tmp_dir/$asset_name" |
| 117 | + fetch_url "$download_url" "$archive_path" |
| 118 | + |
| 119 | + if [ "$asset_ext" = "zip" ]; then |
| 120 | + unzip -q "$archive_path" -d "$tmp_dir" |
| 121 | + else |
| 122 | + tar -xzf "$archive_path" -C "$tmp_dir" |
| 123 | + fi |
| 124 | + |
| 125 | + bin_path="$tmp_dir/$BINARY_NAME" |
| 126 | + if [ ! -f "$bin_path" ]; then |
| 127 | + echo "binary not found in downloaded archive" >&2 |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + |
| 131 | + install_binary "$bin_path" |
| 132 | + |
| 133 | + echo "installed ${BINARY_NAME} ${VERSION} to ${INSTALL_DIR}/${BINARY_NAME}" |
| 134 | +} |
| 135 | + |
| 136 | +main "$@" |
0 commit comments