|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-FileCopyrightText: 2026 Mathieu Barbin <mathieu.barbin@gmail.com> |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# Install mdbook binary from GitHub releases. |
| 5 | +# |
| 6 | +# Environment variables: |
| 7 | +# MDBOOK_VERSION - required, e.g. "0.5.2" |
| 8 | +# MDBOOK_DIGEST - optional, e.g. "sha256:abc123..." |
| 9 | +# INSTALL_DIR - optional, defaults to "$HOME/.local/bin" |
| 10 | +# BINARY_CACHE_HIT - optional, set to "true" to skip download (restored from cache) |
| 11 | + |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +: "${ERRORPREFIX:="::error::Fatal error: "}" |
| 15 | + |
| 16 | +abort() { |
| 17 | + printf '%s%s\n' "$ERRORPREFIX" "$1" |
| 18 | + exit 2 |
| 19 | +} |
| 20 | + |
| 21 | +install_mdbook() { |
| 22 | + BIN_DIR="${INSTALL_DIR:-$HOME/.local/bin}" |
| 23 | + mkdir -p "$BIN_DIR" |
| 24 | + |
| 25 | + if [ "${BINARY_CACHE_HIT:-}" = "true" ]; then |
| 26 | + echo "mdbook binary restored from cache" |
| 27 | + (set -x; "${BIN_DIR}/mdbook" --version) |
| 28 | + return |
| 29 | + fi |
| 30 | + |
| 31 | + case "$(uname -ms)" in |
| 32 | + 'Linux x86_64') |
| 33 | + target=x86_64-unknown-linux-gnu |
| 34 | + ;; |
| 35 | + 'Linux aarch64') |
| 36 | + target=aarch64-unknown-linux-gnu |
| 37 | + ;; |
| 38 | + 'Darwin x86_64') |
| 39 | + target=x86_64-apple-darwin |
| 40 | + ;; |
| 41 | + 'Darwin arm64') |
| 42 | + target=aarch64-apple-darwin |
| 43 | + ;; |
| 44 | + *) |
| 45 | + abort "Unsupported platform: $(uname -ms)" |
| 46 | + ;; |
| 47 | + esac |
| 48 | + |
| 49 | + local url="https://github.com/rust-lang/mdBook/releases/download/v${MDBOOK_VERSION}/mdbook-v${MDBOOK_VERSION}-${target}.tar.gz" |
| 50 | + local tmp_dir |
| 51 | + tmp_dir="$(mktemp -d)" |
| 52 | + |
| 53 | + (set -x; |
| 54 | + curl -fsSL "$url" | tar -xzf - -C "$tmp_dir" |
| 55 | + mv "$tmp_dir/mdbook" "$BIN_DIR/" |
| 56 | + "${BIN_DIR}/mdbook" --version) |
| 57 | + rm -rf "$tmp_dir" |
| 58 | +} |
| 59 | + |
| 60 | +verify_digest() { |
| 61 | + local binary="$1" |
| 62 | + local digest="$2" |
| 63 | + local algorithm="${digest%%:*}" |
| 64 | + local expected_hash="${digest#*:}" |
| 65 | + |
| 66 | + case "${algorithm}" in |
| 67 | + sha256) |
| 68 | + local actual_hash |
| 69 | + if command -v sha256sum >/dev/null 2>&1; then |
| 70 | + actual_hash=$(sha256sum "${binary}" | cut -d ' ' -f 1) |
| 71 | + elif command -v shasum >/dev/null 2>&1; then |
| 72 | + actual_hash=$(shasum -a 256 "${binary}" | cut -d ' ' -f 1) |
| 73 | + else |
| 74 | + abort "sha256sum or shasum is required to verify the binary digest" |
| 75 | + fi |
| 76 | + ;; |
| 77 | + *) |
| 78 | + abort "Digest algorithm '${algorithm}' is not supported. Supported: sha256" |
| 79 | + ;; |
| 80 | + esac |
| 81 | + |
| 82 | + if [ "${actual_hash}" != "${expected_hash}" ]; then |
| 83 | + abort "${algorithm}: expected ${expected_hash} but got ${actual_hash} for ${binary}" |
| 84 | + fi |
| 85 | + echo "Digest verified: ${algorithm}:${actual_hash}" |
| 86 | +} |
| 87 | + |
| 88 | +main() { |
| 89 | + if [ -z "${MDBOOK_VERSION:-}" ]; then |
| 90 | + abort "MDBOOK_VERSION is required" |
| 91 | + fi |
| 92 | + |
| 93 | + install_mdbook |
| 94 | + |
| 95 | + if [ -n "${MDBOOK_DIGEST:-}" ]; then |
| 96 | + verify_digest "${BIN_DIR}/mdbook" "$MDBOOK_DIGEST" |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +main |
0 commit comments