-
Notifications
You must be signed in to change notification settings - Fork 1
/
hephw
executable file
·107 lines (94 loc) · 3 KB
/
hephw
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
#!/usr/bin/env bash
set -u
# Inspired by https://github.com/thought-machine/please/blob/master/pleasew
RED="\x1B[31m"
GREEN="\x1B[32m"
YELLOW="\x1B[33m"
RESET="\x1B[0m"
DEFAULT_URL_BASE="https://storage.googleapis.com/heph-build"
OS="$(uname)"
ARCH="$(uname -m)"
# 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
echo -e >&2 "${RED}Unknown operating system $OS${RESET}"
exit 1
fi
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64"
elif [ "$ARCH" = "aarch64" ]; then
ARCH="arm64"
elif [ "$ARCH" = "arm64" ]; then
:
else
echo -e >&2 "${RED}Unknown cpu arch $ARCH${RESET}"
exit 1
fi
# Check HEPH_CONFIG_PROFILE or fall back to arguments for a profile.
PROFILE="${HEPH_CONFIG_PROFILE:-$(sed -E 's/.*--profile[= ]([^ ]+).*/\1/g' <<< "$*")}"
# Config files on order of precedence high to low.
CONFIGS=(
".hephconfig.local"
"${PROFILE:+.hephconfig.$PROFILE}"
".hephconfig_${GOOS}_${ARCH}"
".hephconfig"
"$HOME/.config/heph/hephconfig"
"/etc/heph/hephconfig"
)
function read_config() {
grep --no-filename -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 Heph.${RESET}"
exit 1
fi
LOCATION="${HOME}/.heph"
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}/heph"
if [ -f "$TARGET" ]; then
exec "$TARGET" ${HEPH_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[^\n]")"
VERSION="${VERSION#*:}" # Strip until after first :
VERSION="${VERSION/ /}" # Remove all spaces
VERSION="${VERSION#>=}" # Strip any initial >=
if [ "$VERSION" = "latest" ]; then
VERSION=$(curl -fsSL ${URL_BASE}/latest_version)
elif [ -z "$VERSION" ]; then
echo -e >&2 "${YELLOW}Can't determine version, will use latest.${RESET}"
VERSION=$(curl -fsSL ${URL_BASE}/latest_version)
fi
HEPH_URL="$DEFAULT_URL_BASE/${VERSION}/heph_${GOOS}_${ARCH}"
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 Heph ${VERSION} to ${DIR}...${RESET}"
mkdir -p "$DIR"
curl -fsSL -o "$DIR/heph" "${HEPH_URL}"
# 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 heph...${RESET}"
chmod +x "$TARGET"
exec "$TARGET" ${HEPH_ARGS:-} "$@"