diff --git a/README.md b/README.md index a998707..63e0028 100644 --- a/README.md +++ b/README.md @@ -17,12 +17,19 @@ Un fichier "manifeste" (par défaut `scaffold.yml`) peut être créé afin de d ``` go install forge.cadoles.com/wpetit/scaffold/cmd/scaffold ``` - -### À partir des binaires +### Manuellement, à partir des binaires 1. [Télécharger la dernière version du binaire correspondant à votre plateforme](https://forge.cadoles.com/wpetit/scaffold/releases) (dernière version stable: 0.0.2) 2. Extraire l'archive et place le binaire `bin/scaffold` dans votre `$PATH` (par exemple dans le répertoire `/usr/local/bin`) +### Automatiquement, via le script d'installation + +```shell +curl -s https://forge.cadoles.com/wpetit/scaffold/raw/branch/master/misc/script/install | bash +``` + +L'utilitaire [`jq`](https://stedolan.github.io/jq/) doit être installé sur votre système. + ## Usage ``` diff --git a/misc/script/install b/misc/script/install new file mode 100755 index 0000000..47977f4 --- /dev/null +++ b/misc/script/install @@ -0,0 +1,98 @@ +#!/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 \ No newline at end of file