scaffold/misc/script/install

98 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
set -eo pipefail
SCAFFOLD_VERSION=${SCAFFOLD_VERSION:-0.0.2}
RELEASE_INFO_URL=https://forge.cadoles.com/api/v1/repos/wpetit/scaffold/releases/tags/0.0.2
function assert_command_available {
local command=$1
local command_path=$(which $command)
if [ -z "$command_path" ]; then
echo "The '$command' command could not be found. Please install it before using this script." 1>&2
exit 1
fi
}
function fetch_release_info {
curl -s -H "accept: application/json" -XGET "${RELEASE_INFO_URL}"
}
function get_archive_name {
local os="$(get_normalized_os)"
local arch="$(get_normalized_arch)"
echo "scaffold-${os}-${arch}.tar.gz"
}
function get_normalized_os {
echo "$(uname -s | tr '[:upper:]' '[:lower:]')"
}
function get_normalized_arch {
local arch=$(uname -m)
case $(uname -m) in
i?86) arch="386" ;;
x86_64) arch="amd64" ;;
arm[4567]*) arch="arm" ;;
arm[8]*) arch="arm64" ;;
*) echo 1>&2 "Unsupported architecture '${arch}' !"; exit 1 ;;
esac
echo "$arch"
}
function find_attachment_url {
local release_info=$1
local expected_attachment_name=$2
echo "${release_info}" | jq -r ".assets[] | select(.name == \"${expected_attachment_name}\") | .browser_download_url"
}
function download_attachment {
local attachment_url=$1
local attachment_name=$2
curl -o "${attachment_name}" "$attachment_url"
}
function main {
assert_command_available 'curl'
assert_command_available 'jq'
assert_command_available 'tar'
assert_command_available 'tr'
local archive_name="$(get_archive_name)"
echo "Fetching scaffold release '${SCAFFOLD_VERSION}' informations..."
local release_info="$(fetch_release_info)"
local archive_url=$(find_attachment_url "$release_info" "$archive_name")
if [ -z "$archive_url" ]; then
echo 1>&2 "Could not find archive url !"
exit 1
fi
local tmpdir=$(mktemp -d)
(
cd "${tmpdir}";
echo "Downloading release file '${archive_name}' to '${tmpdir}'..."
download_attachment "${archive_url}" "${archive_name}"
echo "Extracting archive..."
tar -xzf "${archive_name}"
local dirname="$(echo "${archive_name}" | cut -f 1 -d '.')"
echo "Copying scaffold binary to /usr/local/bin..."
sudo cp -i "${dirname}/bin/scaffold" /usr/local/bin/
)
rm -rf "${tmpdir}"
echo "Done."
}
main