-
Notifications
You must be signed in to change notification settings - Fork 0
/
earthlyw
executable file
·53 lines (41 loc) · 1.43 KB
/
earthlyw
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
#!/bin/bash
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/earthlyw/latest/"
# Check age of cache, if older than 1 month, remove it
if [[ $(find "${CACHE_DIR}" -type f -mtime +30 | wc -l) -gt 0 ]]; then
rm -rf "${CACHE_DIR}"
fi
mkdir -p "${CACHE_DIR}"
# Figure out which architecture to download for, amd64 or arm64
ARCH=$(uname -m)
if [[ "${ARCH}" == "x86_64" ]]; then
ARCH="amd64"
elif [[ "${ARCH}" == "aarch64" ]]; then
ARCH="arm64"
fi
# Figure out OS we are running on
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
EXT=""
if [[ "${OS}" == "windows" ]]; then
EXT=.exe
fi
# Determine executable name
EXECUTABLE_NAME="earthly-${OS}-${ARCH}${EXT}"
EXECUTABLE="${CACHE_DIR}/${EXECUTABLE_NAME}"
# If executable does not exist, fetch it
if [[ ! -x "${EXECUTABLE}" ]]; then
echo "Cache is older than 1 month, fetching latest version" >&2
# Fetch latest release information
DATA_FILE=$(mktemp --suffix=.json)
curl -s https://api.github.com/repos/earthly/earthly/releases/latest > "${DATA_FILE}"
# Find download URL for executable
DOWNLOAD_URL=$(jq -r ".assets[] | select(.name == \"${EXECUTABLE_NAME}\") | .browser_download_url" "${DATA_FILE}")
if [[ -z "${DOWNLOAD_URL}" ]]; then
echo "Failed to find download URL for ${EXECUTABLE_NAME}" >&2
exit 1
fi
# Download binary to user cache directory
curl -sL "${DOWNLOAD_URL}" -o "${EXECUTABLE}"
chmod a+x "${EXECUTABLE}"
fi
# Run binary with all arguments
"${EXECUTABLE}" "$@"