forgeFRENBrowse the repositories

roneoinstall.sh

98ff76d5794bbd1480ad719803187d2c40e42403

At commit 98ff76d

162 lines · 4.6 KiBShellRaw
  1. #!/bin/sh
  2. # Install Ronéo from https://code.cleyrop.tech/roneo releases with SHA-256 verification.
  3. #
  4. # Usage:
  5. # curl -fsSL https://code.cleyrop.tech/roneo/raw/main/install.sh | sh
  6. # RONEO_VERSION=v0.1.0 sh install.sh
  7. # RONEO_INSTALL_DIR=/usr/local/bin sh install.sh
  8. set -eu
  9. FORGE_URL="${RONEO_FORGE_URL:-https://code.cleyrop.tech}"
  10. REPO="${RONEO_REPO:-roneo}"
  11. VERSION="${RONEO_VERSION:-latest}"
  12. INSTALL_DIR="${RONEO_INSTALL_DIR:-${HOME}/.local/bin}"
  13. need_cmd() {
  14. if ! command -v "$1" >/dev/null 2>&1; then
  15. printf 'error: required command not found: %s\n' "$1" >&2
  16. exit 1
  17. fi
  18. }
  19. detect_os() {
  20. case "$(uname -s)" in
  21. Linux*) printf 'linux' ;;
  22. Darwin*) printf 'darwin' ;;
  23. *)
  24. printf 'error: unsupported OS: %s\n' "$(uname -s)" >&2
  25. exit 1
  26. ;;
  27. esac
  28. }
  29. detect_arch() {
  30. case "$(uname -m)" in
  31. x86_64 | amd64) printf 'amd64' ;;
  32. aarch64 | arm64) printf 'arm64' ;;
  33. *)
  34. printf 'error: unsupported architecture: %s\n' "$(uname -m)" >&2
  35. exit 1
  36. ;;
  37. esac
  38. }
  39. sha256_file() {
  40. if command -v sha256sum >/dev/null 2>&1; then
  41. sha256sum "$1" | awk '{print $1}'
  42. elif command -v shasum >/dev/null 2>&1; then
  43. shasum -a 256 "$1" | awk '{print $1}'
  44. else
  45. printf 'error: neither sha256sum nor shasum found\n' >&2
  46. exit 1
  47. fi
  48. }
  49. main() {
  50. need_cmd curl
  51. need_cmd uname
  52. os="$(detect_os)"
  53. arch="$(detect_arch)"
  54. api_url="${FORGE_URL}/api/v1/repos/${REPO}/releases/${VERSION}"
  55. tmpdir="$(mktemp -d)"
  56. trap 'rm -rf "$tmpdir"' EXIT INT TERM
  57. meta_file="${tmpdir}/release.json"
  58. printf 'Fetching %s release metadata (%s/%s)...\n' "$REPO" "$os" "$arch"
  59. curl -fsSL -H 'Accept: application/json' "$api_url" -o "$meta_file"
  60. # Resolve asset url, sha256, and tag from the forge-public JSON schema (lot 7).
  61. # Uses python3 if available, otherwise jq (no `eval`).
  62. parsed_file="${tmpdir}/parsed.txt"
  63. if command -v python3 >/dev/null 2>&1; then
  64. python3 -c '
  65. import json, sys
  66. data = json.load(open(sys.argv[1]))
  67. os_name, arch_name = sys.argv[2], sys.argv[3]
  68. tag = data.get("tag") or data.get("version") or ""
  69. match = None
  70. for a in data.get("assets", []):
  71. if a.get("bin", "roneo") == "roneo" and a.get("os") == os_name and a.get("arch") == arch_name:
  72. match = a
  73. break
  74. if not match:
  75. sys.stderr.write(f"error: no roneo release asset found for {os_name}/{arch_name} in {tag}\n")
  76. sys.exit(1)
  77. print(tag)
  78. print(match["url"])
  79. print(match.get("sha256", ""))
  80. ' "$meta_file" "$os" "$arch" > "$parsed_file"
  81. elif command -v jq >/dev/null 2>&1; then
  82. jq -r --arg os "$os" --arg arch "$arch" '
  83. (.tag // .version // ""),
  84. ([.assets[] | select((.bin // "roneo") == "roneo" and .os == $os and .arch == $arch)][0] | (.url // ""), (.sha256 // ""))
  85. ' "$meta_file" > "$parsed_file"
  86. else
  87. printf 'error: python3 or jq is required to parse release metadata\n' >&2
  88. exit 1
  89. fi
  90. {
  91. IFS= read -r TAG || true
  92. IFS= read -r ASSET_URL || true
  93. IFS= read -r ASSET_SHA256 || true
  94. } < "$parsed_file"
  95. if [ -z "$ASSET_URL" ]; then
  96. printf 'error: no roneo release asset found for %s/%s\n' "$os" "$arch" >&2
  97. exit 1
  98. fi
  99. case "$ASSET_URL" in
  100. http://*|https://*) download_url="$ASSET_URL" ;;
  101. /*) download_url="${FORGE_URL}${ASSET_URL}" ;;
  102. *) download_url="${FORGE_URL}/${ASSET_URL}" ;;
  103. esac
  104. dl_path="${tmpdir}/asset"
  105. printf 'Downloading roneo %s (%s/%s)...\n' "$TAG" "$os" "$arch"
  106. curl -fsSL "$download_url" -o "$dl_path"
  107. if [ -n "$ASSET_SHA256" ]; then
  108. actual="$(sha256_file "$dl_path")"
  109. if [ "$actual" != "$ASSET_SHA256" ]; then
  110. printf 'error: SHA-256 mismatch!\n expected: %s\n actual: %s\n' "$ASSET_SHA256" "$actual" >&2
  111. exit 1
  112. fi
  113. printf 'Verified SHA-256: %s\n' "$actual"
  114. fi
  115. bin_src="$dl_path"
  116. case "$download_url" in
  117. *.tar.gz|*.tgz)
  118. need_cmd tar
  119. mkdir -p "${tmpdir}/extract"
  120. tar -xzf "$dl_path" -C "${tmpdir}/extract"
  121. bin_src="$(find "${tmpdir}/extract" -type f -name roneo | head -n 1)"
  122. ;;
  123. *.zip)
  124. need_cmd unzip
  125. mkdir -p "${tmpdir}/extract"
  126. unzip -q "$dl_path" -d "${tmpdir}/extract"
  127. bin_src="$(find "${tmpdir}/extract" -type f -name roneo | head -n 1)"
  128. ;;
  129. esac
  130. if [ -z "$bin_src" ] || [ ! -f "$bin_src" ]; then
  131. printf 'error: downloaded asset did not contain a roneo binary\n' >&2
  132. exit 1
  133. fi
  134. mkdir -p "$INSTALL_DIR"
  135. chmod 0755 "$bin_src"
  136. mv -f "$bin_src" "${INSTALL_DIR}/roneo"
  137. printf 'Installed roneo %s -> %s/roneo\n' "$TAG" "$INSTALL_DIR"
  138. case ":${PATH}:" in
  139. *":${INSTALL_DIR}:"*) ;;
  140. *) printf 'Note: add %s to your PATH to run roneo directly.\n' "$INSTALL_DIR" ;;
  141. esac
  142. }
  143. main "$@"