|
| 1 | +#!/bin/bash |
| 2 | +set -o errexit |
| 3 | +set -o pipefail |
| 4 | +set -o noclobber |
| 5 | +set -o nounset |
| 6 | +set -o allexport |
| 7 | +readonly githubRepository='twpayne/chezmoi' |
| 8 | +readonly binaryName='chezmoi' |
| 9 | +readonly versionArgument='--version' |
| 10 | +readonly downloadUrlTemplate='https://github.com/${githubRepository}/releases/download/v${version}/${name}_${version}_linux_${architecture}.tar.gz' |
| 11 | +readonly binaryTargetFolder='/usr/local/bin' |
| 12 | +readonly name="${githubRepository##*/}" |
| 13 | +apt_get_update() { |
| 14 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 15 | + echo "Running apt-get update..." |
| 16 | + apt-get update -y |
| 17 | + fi |
| 18 | +} |
| 19 | +apt_get_checkinstall() { |
| 20 | + if ! dpkg -s "$@" >/dev/null 2>&1; then |
| 21 | + apt_get_update |
| 22 | + DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends --no-install-suggests --option 'Debug::pkgProblemResolver=true' --option 'Debug::pkgAcquire::Worker=1' "$@" |
| 23 | + fi |
| 24 | +} |
| 25 | +apt_get_cleanup() { |
| 26 | + apt-get clean |
| 27 | + rm -rf /var/lib/apt/lists/* |
| 28 | +} |
| 29 | +check_curl_envsubst_file_tar_installed() { |
| 30 | + declare -a requiredAptPackagesMissing=() |
| 31 | + if ! [ -r '/etc/ssl/certs/ca-certificates.crt' ]; then |
| 32 | + requiredAptPackagesMissing+=('ca-certificates') |
| 33 | + fi |
| 34 | + if ! command -v curl >/dev/null 2>&1; then |
| 35 | + requiredAptPackagesMissing+=('curl') |
| 36 | + fi |
| 37 | + if ! command -v envsubst >/dev/null 2>&1; then |
| 38 | + requiredAptPackagesMissing+=('gettext-base') |
| 39 | + fi |
| 40 | + if ! command -v file >/dev/null 2>&1; then |
| 41 | + requiredAptPackagesMissing+=('file') |
| 42 | + fi |
| 43 | + if ! command -v tar >/dev/null 2>&1; then |
| 44 | + requiredAptPackagesMissing+=('tar') |
| 45 | + fi |
| 46 | + declare -i requiredAptPackagesMissingCount=${#requiredAptPackagesMissing[@]} |
| 47 | + if [ $requiredAptPackagesMissingCount -gt 0 ]; then |
| 48 | + apt_get_update |
| 49 | + apt_get_checkinstall "${requiredAptPackagesMissing[@]}" |
| 50 | + apt_get_cleanup |
| 51 | + fi |
| 52 | +} |
| 53 | +curl_check_url() { |
| 54 | + local url=$1 |
| 55 | + local status_code |
| 56 | + status_code=$(curl -s -o /dev/null -w '%{http_code}' "$url") |
| 57 | + if [ "$status_code" -ne 200 ] && [ "$status_code" -ne 302 ]; then |
| 58 | + echo "Failed to download '$url'. Status code: $status_code." |
| 59 | + return 1 |
| 60 | + fi |
| 61 | +} |
| 62 | +curl_download_stdout() { |
| 63 | + local url=$1 |
| 64 | + curl \ |
| 65 | + --silent \ |
| 66 | + --location \ |
| 67 | + --output '-' \ |
| 68 | + --connect-timeout 5 \ |
| 69 | + "$url" |
| 70 | +} |
| 71 | +curl_download_untar() { |
| 72 | + local url=$1 |
| 73 | + local strip=$2 |
| 74 | + local target=$3 |
| 75 | + local bin_path=$4 |
| 76 | + curl_download_stdout "$url" | tar \ |
| 77 | + -xz \ |
| 78 | + -f '-' \ |
| 79 | + --strip-components="$strip" \ |
| 80 | + -C "$target" \ |
| 81 | + "$bin_path" |
| 82 | +} |
| 83 | +debian_get_arch() { |
| 84 | + echo "$(dpkg --print-architecture)" |
| 85 | +} |
| 86 | +echo_banner() { |
| 87 | + local text="$1" |
| 88 | + echo -e "\e[1m\e[97m\e[41m$text\e[0m" |
| 89 | +} |
| 90 | +github_list_releases() { |
| 91 | + if [ -z "$1" ]; then |
| 92 | + echo "Usage: list_github_releases <owner/repo>" |
| 93 | + return 1 |
| 94 | + fi |
| 95 | + local repo="$1" |
| 96 | + local url="https://api.github.com/repos/$repo/releases" |
| 97 | + curl -s "$url" | grep -Po '"tag_name": "\K.*?(?=")' | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | sed 's/^v//' |
| 98 | +} |
| 99 | +github_get_latest_release() { |
| 100 | + if [ -z "$1" ]; then |
| 101 | + echo "Usage: get_latest_github_release <owner/repo>" |
| 102 | + return 1 |
| 103 | + fi |
| 104 | + github_list_releases "$1" | head -n 1 |
| 105 | +} |
| 106 | +utils_check_version() { |
| 107 | + local version=$1 |
| 108 | + if ! [[ "${version:-}" =~ ^(latest|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then |
| 109 | + printf >&2 '=== [ERROR] Option "version" (value: "%s") is not "latest" or valid semantic version format "X.Y.Z" !\n' \ |
| 110 | + "$version" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | +} |
| 114 | +install() { |
| 115 | + utils_check_version "$VERSION" |
| 116 | + check_curl_envsubst_file_tar_installed |
| 117 | + readonly architecture="$(debian_get_arch)" |
| 118 | + readonly binaryTargetPathTemplate='${binaryTargetFolder}/${binaryName}' |
| 119 | + if [ "$VERSION" == 'latest' ] || [ -z "$VERSION" ]; then |
| 120 | + VERSION=$(github_get_latest_release "$githubRepository") |
| 121 | + fi |
| 122 | + readonly version="${VERSION:?}" |
| 123 | + readonly downloadUrl="$(echo -n "$downloadUrlTemplate" | envsubst)" |
| 124 | + curl_check_url "$downloadUrl" |
| 125 | + readonly binaryPathInArchive="$binaryName" |
| 126 | + readonly stripComponents="$(echo -n "$binaryPathInArchive" | awk -F'/' '{print NF-1}')" |
| 127 | + readonly binaryTargetPath="$(echo -n "$binaryTargetPathTemplate" | envsubst)" |
| 128 | + curl_download_untar "$downloadUrl" "$stripComponents" "$binaryTargetFolder" "$binaryPathInArchive" |
| 129 | + chmod 755 "$binaryTargetPath" |
| 130 | +} |
| 131 | +echo_banner "devcontainer.community" |
| 132 | +echo "Installing $name..." |
| 133 | +install "$@" |
| 134 | +echo "(*) Done!" |
0 commit comments