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