binhonglee/GlobeTrotte

View on GitHub
pleasew

Summary

Maintainability
Test Coverage
#!/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"
# We might already have it downloaded...
LOCATION=`grep -i "^location" .plzconfig | cut -d '=' -f 2 | tr -d ' '`
if [ -z "$LOCATION" ]; then
    if [ -z "$HOME" ]; then
        echo -e "${RED}\$HOME not set, not sure where to look for Please.${RESET}"
        exit 1
    fi
    LOCATION="${HOME}/.please"
fi
# If this exists at any version, let it handle any update.
TARGET="${LOCATION}/please"
if [ -f "$TARGET" ]; then
    exec "$TARGET" $@
fi

URL_BASE="`grep -i "^downloadlocation" .plzconfig | cut -d '=' -f 2 | tr -d ' '`"
if [ -z "$URL_BASE" ]; then
    URL_BASE=$DEFAULT_URL_BASE
fi
URL_BASE="${URL_BASE%/}"

VERSION="`grep -i "^version" .plzconfig`"
VERSION="${VERSION#*=}"    # Strip until after first =
VERSION="${VERSION/ /}"    # Remove all spaces
VERSION="${VERSION#>=}"    # Strip any initial >=
if [ -z "$VERSION" ]; then
    echo -e "${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.
OS=`uname`
if [ "$OS" = "Linux" ]; then
    GOOS="linux"
elif [ "$OS" = "Darwin" ]; then
    GOOS="darwin"
else
    echo -e "${RED}Unknown operating system $OS${RESET}"
    exit 1
fi
# Don't have any builds other than amd64 at the moment.
ARCH="amd64"

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 "${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
ln -sf "${DIR}/please" "${LOCATION}/plz"
echo -e "${GREEN}Should be good to go now, running plz...${RESET}"
exec "$TARGET" $@