|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +force=false |
| 5 | +if [[ "${1:-}" == "--force" ]]; then |
| 6 | + force=true |
| 7 | +fi |
| 8 | + |
| 9 | +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | +repo_root="$(cd "${script_dir}/../../.." && pwd)" |
| 11 | +bin_dir="${repo_root}/bin" |
| 12 | +target_bin="${bin_dir}/ezorm" |
| 13 | + |
| 14 | +if [[ -x "${target_bin}" && "${force}" == "false" ]]; then |
| 15 | + printf 'ezorm already present: %s\n' "${target_bin}" |
| 16 | + exit 0 |
| 17 | +fi |
| 18 | + |
| 19 | +mkdir -p "${bin_dir}" |
| 20 | + |
| 21 | +try_go_install() { |
| 22 | + if ! command -v go >/dev/null 2>&1; then |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + |
| 26 | + printf 'installing ezorm via go install...\n' |
| 27 | + if ! go install github.com/ezbuy/ezorm/v2@latest; then |
| 28 | + return 1 |
| 29 | + fi |
| 30 | + |
| 31 | + local gopath |
| 32 | + gopath="$(go env GOPATH)" |
| 33 | + if [[ -z "${gopath}" ]]; then |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + |
| 37 | + local installed_bin="${gopath}/bin/ezorm" |
| 38 | + if [[ ! -x "${installed_bin}" ]]; then |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + cp "${installed_bin}" "${target_bin}" |
| 43 | + chmod +x "${target_bin}" |
| 44 | + printf 'installed ezorm to %s\n' "${target_bin}" |
| 45 | + return 0 |
| 46 | +} |
| 47 | + |
| 48 | +try_release_download() { |
| 49 | + if ! command -v curl >/dev/null 2>&1; then |
| 50 | + printf 'curl is required to download ezorm release\n' |
| 51 | + return 1 |
| 52 | + fi |
| 53 | + |
| 54 | + local os arch |
| 55 | + case "$(uname -s)" in |
| 56 | + Linux) os="linux" ;; |
| 57 | + Darwin) os="darwin" ;; |
| 58 | + *) |
| 59 | + printf 'unsupported OS: %s\n' "$(uname -s)" |
| 60 | + return 1 |
| 61 | + ;; |
| 62 | + esac |
| 63 | + |
| 64 | + case "$(uname -m)" in |
| 65 | + x86_64) arch="amd64" ;; |
| 66 | + arm64|aarch64) arch="arm64" ;; |
| 67 | + *) |
| 68 | + printf 'unsupported arch: %s\n' "$(uname -m)" |
| 69 | + return 1 |
| 70 | + ;; |
| 71 | + esac |
| 72 | + |
| 73 | + local py |
| 74 | + if command -v python3 >/dev/null 2>&1; then |
| 75 | + py="python3" |
| 76 | + elif command -v python >/dev/null 2>&1; then |
| 77 | + py="python" |
| 78 | + else |
| 79 | + printf 'python is required to parse release metadata\n' |
| 80 | + return 1 |
| 81 | + fi |
| 82 | + |
| 83 | + printf 'downloading ezorm release for %s/%s...\n' "${os}" "${arch}" |
| 84 | + |
| 85 | + local release_json url |
| 86 | + release_json="$(curl -fsSL https://api.github.com/repos/ezbuy/ezorm/releases/latest)" |
| 87 | + url="$(${py} - "${os}" "${arch}" <<'PY' |
| 88 | +import json |
| 89 | +import sys |
| 90 | +
|
| 91 | +target_os = sys.argv[1] |
| 92 | +target_arch = sys.argv[2] |
| 93 | +
|
| 94 | +data = json.load(sys.stdin) |
| 95 | +assets = data.get("assets", []) |
| 96 | +for asset in assets: |
| 97 | + name = asset.get("name", "") |
| 98 | + lower = name.lower() |
| 99 | + if "ezorm" not in lower: |
| 100 | + continue |
| 101 | + if target_os in lower and target_arch in lower: |
| 102 | + print(asset.get("browser_download_url", "")) |
| 103 | + raise SystemExit(0) |
| 104 | +print("") |
| 105 | +raise SystemExit(1) |
| 106 | +PY |
| 107 | +)" |
| 108 | + |
| 109 | + if [[ -z "${url}" ]]; then |
| 110 | + printf 'no release asset found for %s/%s\n' "${os}" "${arch}" |
| 111 | + return 1 |
| 112 | + fi |
| 113 | + |
| 114 | + local tmpdir tmpfile |
| 115 | + tmpdir="$(mktemp -d)" |
| 116 | + tmpfile="${tmpdir}/ezorm_download" |
| 117 | + curl -fsSL "${url}" -o "${tmpfile}" |
| 118 | + |
| 119 | + case "${url}" in |
| 120 | + *.tar.gz) |
| 121 | + tar -xzf "${tmpfile}" -C "${tmpdir}" |
| 122 | + ;; |
| 123 | + *.zip) |
| 124 | + unzip -q "${tmpfile}" -d "${tmpdir}" |
| 125 | + ;; |
| 126 | + *) |
| 127 | + cp "${tmpfile}" "${target_bin}" |
| 128 | + chmod +x "${target_bin}" |
| 129 | + printf 'installed ezorm to %s\n' "${target_bin}" |
| 130 | + rm -rf "${tmpdir}" |
| 131 | + return 0 |
| 132 | + ;; |
| 133 | + esac |
| 134 | + |
| 135 | + local found |
| 136 | + found="$(find "${tmpdir}" -type f -name ezorm -maxdepth 3 | head -n 1)" |
| 137 | + if [[ -z "${found}" ]]; then |
| 138 | + printf 'ezorm binary not found in release archive\n' |
| 139 | + rm -rf "${tmpdir}" |
| 140 | + return 1 |
| 141 | + fi |
| 142 | + |
| 143 | + cp "${found}" "${target_bin}" |
| 144 | + chmod +x "${target_bin}" |
| 145 | + rm -rf "${tmpdir}" |
| 146 | + printf 'installed ezorm to %s\n' "${target_bin}" |
| 147 | + return 0 |
| 148 | +} |
| 149 | + |
| 150 | +if try_go_install; then |
| 151 | + exit 0 |
| 152 | +fi |
| 153 | + |
| 154 | +if try_release_download; then |
| 155 | + exit 0 |
| 156 | +fi |
| 157 | + |
| 158 | +printf 'failed to install ezorm; try go install github.com/ezbuy/ezorm/v2@latest\n' |
| 159 | +exit 1 |
0 commit comments