-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pleasew
executable file
·90 lines (79 loc) · 2.62 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
#!/usr/bin/env bash
set -u
RED="\x1B[31m"
GREEN="\x1B[32m"
YELLOW="\x1B[33m"
RESET="\x1B[0m"
DEFAULT_URL_BASE="https://get.please.build"
OS="$(uname)"
# Don't have any builds other than amd64 at the moment.
ARCH="amd64"
# Check PLZ_CONFIG_PROFILE or fall back to arguments for a profile.
PROFILE="${PLZ_CONFIG_PROFILE:-$(sed -E 's/.*--profile[= ]([^ ]+).*/\1/g' <<< "$*")}"
# Config files on order of precedence high to low.
CONFIGS=(
".plzconfig.local"
".plzconfig_${OS}_${ARCH}"
"${PROFILE:+.plzconfig.$PROFILE}"
".plzconfig"
"$HOME/.config/please/plzconfig"
"/etc/please/plzconfig"
)
function read_config() {
grep -i "$1" "${CONFIGS[@]}" 2>/dev/null | head -n 1
}
# We might already have it downloaded...
LOCATION="$(read_config "^location" | cut -d '=' -f 2 | tr -d ' ')"
if [ -z "$LOCATION" ]; then
if [ -z "$HOME" ]; then
echo -e >&2 "${RED}\$HOME not set, not sure where to look for Please.${RESET}"
exit 1
fi
LOCATION="${HOME}/.please"
else
# It can contain a literal ~, need to explicitly handle that.
LOCATION="${LOCATION/\~/$HOME}"
fi
# If this exists at any version, let it handle any update.
TARGET="${LOCATION}/please"
if [ -f "$TARGET" ]; then
exec "$TARGET" ${PLZ_ARGS:-} "$@"
fi
URL_BASE="$(read_config "^downloadlocation" | cut -d '=' -f 2 | tr -d ' ')"
if [ -z "$URL_BASE" ]; then
URL_BASE=$DEFAULT_URL_BASE
fi
URL_BASE="${URL_BASE%/}"
VERSION="$(read_config "^version[^a-z]")"
VERSION="${VERSION#*=}" # Strip until after first =
VERSION="${VERSION/ /}" # Remove all spaces
VERSION="${VERSION#>=}" # Strip any initial >=
if [ -z "$VERSION" ]; then
echo -e >&2 "${YELLOW}Can't determine version, will use latest.${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"
else
echo -e >&2 "${RED}Unknown operating system $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
echo -e >&2 "${GREEN}Downloading Please ${VERSION} to ${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 $(ls "$DIR"); do
ln -sf "${DIR}/${x}" "$LOCATION"
done
echo -e >&2 "${GREEN}Should be good to go now, running plz...${RESET}"
exec "$TARGET" ${PLZ_ARGS:-} "$@"