-
Notifications
You must be signed in to change notification settings - Fork 1
/
pleasew
executable file
·145 lines (114 loc) · 3.57 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
#!/bin/sh
set -e
set -u
set -o errexit
RED='\x1B[31m'
GREEN='\x1B[32m'
YELLOW='\x1B[33m'
RESET='\x1B[0m'
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
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 '^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 '^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 '^version[^a-z]')"
VERSION="${VERSION#*=}" # Strip until after first =
VERSION="$(echo "${VERSION}" | tr -d ' ')" # Remove all spaces
VERSION="${VERSION#>=}" # Strip any initial >=
if [ "${VERSION:+x}" != 'x' ]; then
printf >&2 "%bCan't determine version, will use latest.%b\n" "${YELLOW}" "${RESET}"
VERSION=$(curl -fsSL "${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}"
curl -fsSL "${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:-} "${@}"