|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +set -u |
| 4 | + |
| 5 | +type curl > /dev/null || { echo "curl: not found"; exit 1; } |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | + |
| 10 | +bin="sphere" |
| 11 | +repo="TBXark/sphere" |
| 12 | +dest_dir="/usr/local/bin" |
| 13 | + |
| 14 | +get_latest_release() { |
| 15 | + local repo="$1" |
| 16 | + curl -sSL "https://api.github.com/repos/${repo}/releases/latest" | \ |
| 17 | + awk 'BEGIN{FS=": |,|\""}; /tag_name/{print $5}' |
| 18 | +} |
| 19 | + |
| 20 | +version="$(get_latest_release "${repo}")" # v1.2.0 |
| 21 | + |
| 22 | +# if args has version override it and not eq "latest" |
| 23 | +if test $# -eq 1; then |
| 24 | + if test "$1" != "latest"; then |
| 25 | + version="$1" |
| 26 | + |
| 27 | + echo "Install ${version}" |
| 28 | + fi |
| 29 | +fi |
| 30 | + |
| 31 | +platform="$(uname | tr "[A-Z]" "[a-z]")" # Linux => linux |
| 32 | +arch="$(uname -m | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')" # x86_64 => amd64, aarch64 => arm64 |
| 33 | +package="${bin}_${platform}_${arch}.tar.gz" |
| 34 | +package_url="https://github.com/${repo}/releases/download/${version}/${package}" |
| 35 | +bin_path="${dest_dir}/${bin}" |
| 36 | +tmp_dir="$(mktemp -d)" |
| 37 | + |
| 38 | +trap "rm -r ${tmp_dir}" EXIT |
| 39 | + |
| 40 | +if test -e "${bin_path}"; then |
| 41 | + current_version="v$("${bin_path}" -v | awk '{print $NF}')" |
| 42 | + if test "${current_version}" = "${version}"; then |
| 43 | + echo "${bin} is already updated, no need to upgrade." |
| 44 | + exit 0 |
| 45 | + else |
| 46 | + echo "There is a new version of ${bin}, starting to upgrade from ${current_version} to ${version}." |
| 47 | + fi |
| 48 | +fi |
| 49 | +cd "${tmp_dir}" |
| 50 | +curl -sSL "${package_url}" | tar xzf - |
| 51 | + |
| 52 | +if test $(id -u) -eq 0; then |
| 53 | + mv "${bin}" "${dest_dir}" |
| 54 | +else |
| 55 | + sudo mv "${bin}" "${dest_dir}" |
| 56 | +fi |
| 57 | + |
| 58 | +mkdir -p ~/.${bin} |
| 59 | + |
| 60 | +echo "${bin} ${version} has been installed." |
0 commit comments