|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Taken from https://github.com/vapor-ware/gimme-that/commit/7be1b559cb10be240e55dac555e32e9fea729ccf |
| 3 | + |
| 4 | +# set -x |
| 5 | +set -eo pipefail |
| 6 | + |
| 7 | +# Colors for colorizing |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +PURPLE='\033[0;35m' |
| 11 | +BLUE='\033[0;34m' |
| 12 | +YELLOW='\033[0;33m' |
| 13 | +NC='\033[0m' |
| 14 | + |
| 15 | +# Storage for constants |
| 16 | +TARGET_BIN=${TARGET_BIN:-"hashnode"} |
| 17 | +# TODO: Make this dynamic |
| 18 | +GITHUB_API=${GITHUB_API:-"https://api.github.com"} |
| 19 | +TARGET_GITHUB_USER=${TARGET_GITHUB_USER:-"hashnode"} |
| 20 | +TARGET_GITHUB_REPO=${TARGET_GITHUB_REPO:-"hashnode-cli"} |
| 21 | +TARGET_PROJECT=${TARGET_PROJECT:-$TARGET_GITHUB_USER/$TARGET_GITHUB_REPO} |
| 22 | +TARGET_INSTALL_PATH=${TARGET_INSTALL_PATH:-"/usr/local/bin"} |
| 23 | +HOST_OS=${HOST_OS:-$(uname | tr '[:upper:]' '[:lower:]')} |
| 24 | +if [[ $(uname -m) == "x86_64" ]]; then |
| 25 | + HOST_ARCH="amd64" |
| 26 | +else |
| 27 | + HOST_ARCH=${HOST_ARCH:-$(uname -m)} |
| 28 | +fi |
| 29 | + |
| 30 | +# Check for root permission |
| 31 | +touch ${TARGET_INSTALL_PATH}/.gimme &> /dev/null || (echo -e "${RED}Root access is required to install to ${YELLOW}${TARGET_INSTALL_PATH}${NC}" && exit 1) |
| 32 | +rm ${TARGET_INSTALL_PATH}/.gimme |
| 33 | + |
| 34 | +# Basic JSON matching |
| 35 | +# TODO: Clean this up for god's sake! |
| 36 | +function jsonVal { |
| 37 | + temp=`echo $1 | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $2 | sed 's|.*\:\(.*\)|\1|'` |
| 38 | + echo ${temp##*|} |
| 39 | +} |
| 40 | + |
| 41 | +# Check GitHub for the latest release |
| 42 | +echo -e "${BLUE}Checking GitHub for the latest release of ${YELLOW}${TARGET_BIN}${NC}" |
| 43 | +URI=${GITHUB_API}/repos/${TARGET_PROJECT}/releases/latest |
| 44 | +RELEASE_RESPONSE=$(curl -L -S -s ${URI}) |
| 45 | + |
| 46 | +# Parse release info |
| 47 | +RELEASE_TAG=$(jsonVal "${RELEASE_RESPONSE}" "tag_name") |
| 48 | +# TODO: Make this more flexible |
| 49 | +echo -e "${BLUE}Found release tag: ${YELLOW}${RELEASE_TAG}${NC}" |
| 50 | +TARGET_STRING=${TARGET_BIN}_${HOST_OS}_${HOST_ARCH} |
| 51 | +# echo -e $TARGET_STRING |
| 52 | +# TODO: Ditto. This makes me uber sad. |
| 53 | +echo -e "${BLUE}Downloading ${YELLOW}${TARGET_STRING}${NC}" |
| 54 | +DOWNLOAD_URL="https://github.com/"${TARGET_PROJECT}"/releases/download/"${RELEASE_TAG}"/"${TARGET_STRING} |
| 55 | +# echo -e $DOWNLOAD_URL |
| 56 | + |
| 57 | +# Check if we have this already |
| 58 | +if ! command -v ${TARGET_BIN}; then |
| 59 | + echo -e "${RED}No previous install found. Installing ${YELLOW}${TARGET_BIN}${NC}${RED} to ${TARGET_INSTALL_PATH}/${TARGET_BIN}${NC}" |
| 60 | + curl -s -S -L ${DOWNLOAD_URL} -o ${TARGET_INSTALL_PATH}/${TARGET_BIN} |
| 61 | + chmod +x ${TARGET_INSTALL_PATH}/${TARGET_BIN} |
| 62 | +# TODO: Do some version checking here. Hash? |
| 63 | +elif curl -s -S -L -z ${TARGET_INSTALL_PATH}/${TARGET_BIN} ${DOWNLOAD_URL} -o ${TARGET_INSTALL_PATH}/${TARGET_BIN} ; then |
| 64 | + echo -e "${RED}Newer binary found! Installing ${YELLOW}${TARGET_BIN}${NC}${RED} to ${TARGET_INSTALL_PATH}/${TARGET_BIN}${NC}" |
| 65 | + chmod +x ${TARGET_INSTALL_PATH}/${TARGET_BIN} |
| 66 | + exit 0 |
| 67 | +fi |
| 68 | +echo -e "${GREEN}No newer version found. Leaving ${YELLOW}${TARGET_INSTALL_PATH}/${TARGET_BIN}${NC}${NC}" |
0 commit comments