|
1 | | -#!/usr/bin/env bash |
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -e |
2 | 4 | set -u |
3 | 5 |
|
4 | | -RED="\x1B[31m" |
5 | | -GREEN="\x1B[32m" |
6 | | -YELLOW="\x1B[33m" |
7 | | -RESET="\x1B[0m" |
| 6 | +ESC="$(printf '\033')" |
8 | 7 |
|
9 | | -DEFAULT_URL_BASE="https://get.please.build" |
| 8 | +if [ "${NOCOLOR+x}" != 'x' ] || [ "${NO_COLOR+x}" != 'x' ]; then |
| 9 | + RED="${ESC}[31m" |
| 10 | + GREEN="${ESC}[32m" |
| 11 | + YELLOW="${ESC}[33m" |
| 12 | + RESET="${ESC}[0m" |
| 13 | +else |
| 14 | + RED='' |
| 15 | + GREEN='' |
| 16 | + YELLOW='' |
| 17 | + RESET='' |
| 18 | +fi |
10 | 19 |
|
11 | | -OS="$(uname)" |
| 20 | +OS="" |
| 21 | +ARCH="" |
12 | 22 |
|
13 | | -if [[ $OS == "Darwin" ]]; then |
14 | | - # switch between mac amd64/arm64 |
15 | | - ARCH="$(uname -m)" |
16 | | -else |
17 | | - # default to amd64 on other operating systems |
18 | | - # because we only build intel binaries |
19 | | - ARCH="amd64" |
| 23 | +case "$(uname)" in |
| 24 | + Linux) |
| 25 | + OS=linux |
| 26 | + case "$(uname -m)" in |
| 27 | + x86_64) ARCH=amd64 ;; |
| 28 | + aarch64*|armv8b|armv8l) ARCH=arm64 ;; |
| 29 | + esac |
| 30 | + ;; |
| 31 | + Darwin) |
| 32 | + OS=darwin |
| 33 | + case "$(uname -m)" in |
| 34 | + x86_64) ARCH=amd64 ;; |
| 35 | + arm64) ARCH=arm64 ;; |
| 36 | + esac |
| 37 | + ;; |
| 38 | + FreeBSD) |
| 39 | + OS=freebsd |
| 40 | + case "$(uname -m)" in |
| 41 | + amd64) ARCH=amd64 ;; |
| 42 | + esac |
| 43 | + ;; |
| 44 | + *) |
| 45 | + printf >&2 '%bPlease does not support the %s operating system.%b\n' \ |
| 46 | + "${RED}" "$(uname)" "${RESET}" |
| 47 | + exit 1 |
| 48 | + ;; |
| 49 | +esac |
| 50 | + |
| 51 | +if [ -z "$ARCH" ]; then |
| 52 | + printf >&2 '%bPlease does not support the %s architecture on %s.%b\n' \ |
| 53 | + "${RED}" "$(uname -m)" "$(uname)" "${RESET}" |
| 54 | + exit 1 |
20 | 55 | fi |
21 | 56 |
|
22 | | -# Check PLZ_CONFIG_PROFILE or fall back to arguments for a profile. |
23 | | -PROFILE="${PLZ_CONFIG_PROFILE:-$(sed -E 's/.*--profile[= ]([^ ]+).*/\1/g' <<< "$*")}" |
| 57 | +DEFAULT_URL_BASE='https://get.please.build' |
| 58 | + |
| 59 | +has_command () { |
| 60 | + command -v "${1}" > /dev/null 2>&1 |
| 61 | +} |
| 62 | + |
| 63 | +get_profile () { |
| 64 | + while [ "${#}" -gt 0 ] |
| 65 | + do |
| 66 | + case "${1}" in |
| 67 | + --profile=*) echo "${1#*=}"; return;; |
| 68 | + --profile) echo "${2}"; return;; |
| 69 | + *) shift;; |
| 70 | + esac |
| 71 | + done |
| 72 | +} |
| 73 | + |
| 74 | +# Check `PLZ_CONFIG_PROFILE` or fall back to arguments for a profile. |
| 75 | +PROFILE="${PLZ_CONFIG_PROFILE:-$(get_profile "${@}")}" |
24 | 76 |
|
25 | 77 | # Config files on order of precedence high to low. |
26 | | -CONFIGS=( |
27 | | - ".plzconfig.local" |
28 | | - "${PROFILE:+.plzconfig.$PROFILE}" |
29 | | - ".plzconfig_${OS}_${ARCH}" |
30 | | - ".plzconfig" |
31 | | - "$HOME/.config/please/plzconfig" |
32 | | - "/etc/please/plzconfig" |
33 | | -) |
34 | | - |
35 | | -function read_config() { |
36 | | - grep -i "$1" "${CONFIGS[@]}" 2>/dev/null | head -n 1 |
| 78 | +CONFIGS="$(cat <<- EOS |
| 79 | + .plzconfig.local |
| 80 | + ${PROFILE:+.plzconfig.${PROFILE}} |
| 81 | + .plzconfig_${OS}_${ARCH} |
| 82 | + .plzconfig |
| 83 | + ${HOME}/.config/please/plzconfig |
| 84 | + /etc/please/plzconfig |
| 85 | +EOS |
| 86 | +)" |
| 87 | + |
| 88 | +read_config() { |
| 89 | + # Disable globbing to ensure word-splitting is safe. |
| 90 | + set -f |
| 91 | + |
| 92 | + old_ifs="${IFS}" |
| 93 | + search_term="${1}" |
| 94 | + |
| 95 | + IFS=' |
| 96 | +' |
| 97 | + |
| 98 | + # This is intended, we *do* want word-splitting here. |
| 99 | + # shellcheck disable=2086 |
| 100 | + set -- ${CONFIGS} |
| 101 | + |
| 102 | + grep -i "${search_term}" "${@}" 2> /dev/null | head -n 1 |
| 103 | + |
| 104 | + IFS="${old_ifs}" |
| 105 | + set +f |
37 | 106 | } |
38 | 107 |
|
39 | 108 | # We might already have it downloaded... |
40 | | -LOCATION="$(read_config "^location" | cut -d '=' -f 2 | tr -d ' ')" |
41 | | -if [ -z "$LOCATION" ]; then |
42 | | - if [ -z "$HOME" ]; then |
43 | | - echo -e >&2 "${RED}\$HOME not set, not sure where to look for Please.${RESET}" |
| 109 | +LOCATION="$(read_config '^\s*location' | cut -d '=' -f 2 | tr -d ' ')" |
| 110 | + |
| 111 | +if [ "${LOCATION:+x}" != 'x' ]; then |
| 112 | + if [ "${HOME:+x}" != 'x' ]; then |
| 113 | + # shellcheck disable=2016 |
| 114 | + printf >&2 '%b$HOME not set, not sure where to look for Please.%b\n' "${RED}" "${RESET}" |
44 | 115 | exit 1 |
45 | 116 | fi |
| 117 | + |
46 | 118 | LOCATION="${HOME}/.please" |
47 | 119 | else |
48 | 120 | # It can contain a literal ~, need to explicitly handle that. |
49 | | - LOCATION="${LOCATION/\~/$HOME}" |
| 121 | + LOCATION="$(echo "${LOCATION}" | sed "s|~|${HOME}|")" |
50 | 122 | fi |
| 123 | + |
51 | 124 | # If this exists at any version, let it handle any update. |
52 | 125 | TARGET="${LOCATION}/please" |
53 | | -if [ -f "$TARGET" ]; then |
54 | | - exec "$TARGET" ${PLZ_ARGS:-} "$@" |
| 126 | + |
| 127 | +if [ -f "${TARGET}" ]; then |
| 128 | + # shellcheck disable=2086 |
| 129 | + exec "${TARGET}" ${PLZ_ARGS:-} "${@}" |
55 | 130 | fi |
56 | 131 |
|
57 | | -URL_BASE="$(read_config "^downloadlocation" | cut -d '=' -f 2 | tr -d ' ')" |
58 | | -if [ -z "$URL_BASE" ]; then |
59 | | - URL_BASE=$DEFAULT_URL_BASE |
| 132 | +URL_BASE="$(read_config '^\s*downloadlocation' | cut -d '=' -f 2 | tr -d ' ')" |
| 133 | + |
| 134 | +if [ "${URL_BASE:+x}" != 'x' ]; then |
| 135 | + URL_BASE="${DEFAULT_URL_BASE}" |
60 | 136 | fi |
| 137 | + |
61 | 138 | URL_BASE="${URL_BASE%/}" |
62 | 139 |
|
63 | | -VERSION="$(read_config "^version[^a-z]")" |
64 | | -VERSION="${VERSION#*=}" # Strip until after first = |
65 | | -VERSION="${VERSION/ /}" # Remove all spaces |
66 | | -VERSION="${VERSION#>=}" # Strip any initial >= |
67 | | -if [ -z "$VERSION" ]; then |
68 | | - echo -e >&2 "${YELLOW}Can't determine version, will use latest.${RESET}" |
69 | | - VERSION=$(curl -fsSL ${URL_BASE}/latest_version) |
70 | | -fi |
| 140 | +VERSION="$(read_config '^\s*version[^a-z]')" |
| 141 | +VERSION="${VERSION#*=}" # Strip until after first = |
| 142 | +VERSION="$(echo "${VERSION}" | tr -d ' ')" # Remove all spaces |
| 143 | +VERSION="${VERSION#>=}" # Strip any initial >= |
71 | 144 |
|
72 | | -# Find the os / arch to download. You can do this quite nicely with go env |
73 | | -# but we use this script on machines that don't necessarily have Go itself. |
74 | | -if [ "$OS" = "Linux" ]; then |
75 | | - GOOS="linux" |
76 | | -elif [ "$OS" = "Darwin" ]; then |
77 | | - GOOS="darwin" |
| 145 | +if has_command curl; then |
| 146 | + TRANSFER_TOOL='curl' |
| 147 | + TRANSFER_SILENT_OPTS='-fsSL' |
| 148 | + TRANSFER_PROGRESS_OPTS='-fSL' |
| 149 | +elif has_command wget; then |
| 150 | + TRANSFER_TOOL='wget' |
| 151 | + TRANSFER_SILENT_OPTS='-qO-' |
| 152 | + TRANSFER_PROGRESS_OPTS='-O-' |
78 | 153 | else |
79 | | - echo -e >&2 "${RED}Unknown operating system $OS${RESET}" |
| 154 | + printf >&2 '%bUnable to find a command for network operations%b\n' "${RED}" "${RESET}" |
| 155 | + printf >&2 'Please install either curl or wget\n' |
80 | 156 | exit 1 |
81 | 157 | fi |
82 | 158 |
|
83 | | -PLEASE_URL="${URL_BASE}/${GOOS}_${ARCH}/${VERSION}/please_${VERSION}.tar.xz" |
| 159 | +if [ "${VERSION:+x}" != 'x' ]; then |
| 160 | + printf >&2 "%bCan't determine version, will use latest.%b\n" "${YELLOW}" "${RESET}" |
| 161 | + VERSION=$(${TRANSFER_TOOL} ${TRANSFER_SILENT_OPTS} "${URL_BASE}"/latest_version) |
| 162 | +fi |
| 163 | + |
| 164 | +PLEASE_URL="${URL_BASE}/${OS}_${ARCH}/${VERSION}/please_${VERSION}.tar.xz" |
84 | 165 | DIR="${LOCATION}/${VERSION}" |
| 166 | + |
85 | 167 | # Potentially we could reuse this but it's easier not to really. |
86 | | -if [ ! -d "$DIR" ]; then |
87 | | - rm -rf "$DIR" |
| 168 | +if [ ! -d "${DIR}" ]; then |
| 169 | + rm -Rf "${DIR}" |
| 170 | +fi |
| 171 | + |
| 172 | +printf >&2 '%bDownloading Please %s to %s...%b\n' "${GREEN}" "${VERSION}" "${DIR}" "${RESET}" |
| 173 | +mkdir -p "${DIR}" |
| 174 | +${TRANSFER_TOOL} ${TRANSFER_PROGRESS_OPTS} "${PLEASE_URL}" | tar -xJpf- --strip-components=1 -C "${DIR}" |
| 175 | +if [ $? -ne 0 ]; then |
| 176 | + printf >&2 '%bFailed to download Please%b\n' "${RED}" "${RESET}" |
| 177 | + exit 1 |
88 | 178 | fi |
89 | | -echo -e >&2 "${GREEN}Downloading Please ${VERSION} to ${DIR}...${RESET}" |
90 | | -mkdir -p "$DIR" |
91 | | -curl -fsSL "${PLEASE_URL}" | tar -xJpf- --strip-components=1 -C "$DIR" |
| 179 | + |
92 | 180 | # Link it all back up a dir |
93 | | -for x in $(ls "$DIR"); do |
94 | | - ln -sf "${DIR}/${x}" "$LOCATION" |
| 181 | +for x in "${DIR}"/*; do |
| 182 | + ln -sf "${x}" "${LOCATION}" |
95 | 183 | done |
96 | | -echo -e >&2 "${GREEN}Should be good to go now, running plz...${RESET}" |
97 | | -exec "$TARGET" ${PLZ_ARGS:-} "$@" |
| 184 | + |
| 185 | +printf >&2 '%bShould be good to go now, running plz...%b\n' "${GREEN}" "${RESET}" |
| 186 | +# shellcheck disable=2086 |
| 187 | +exec "${TARGET}" ${PLZ_ARGS:-} "${@}" |
0 commit comments