Skip to content

Commit

Permalink
feat: get.sh for simpler installation
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Dec 11, 2019
1 parent 232db44 commit ade8485
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions scripts/get.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash

## Derived from https://github.com/goreleaser/get/blob/03c4bfde763b30bfe270892ab3ff74949d8e9351/get

set -e

die () {
echo >&2 "$@"
exit 1
}
BINARY_NAME="ghc"
TAR_FILE="/tmp/${BINARY_NAME}.tar.gz"
RELEASES_URL="https://github.com/bartoszmajsak/github-changelog-generator/releases"

show_help() {
echo "get - downloads ${BINARY_NAME} binary matching your operating system"
echo " "
echo "./get.sh [options]"
echo " "
echo "Options:"
echo "-h, --help shows brief help"
echo "-v, --version defines version specific version of the binary to download (defaults to latest)"
echo "-d, --dir target directory to which the binary is downloaded (defaults to random tmp dir in /tmp suffixed with ${BINARY_NAME}-version)"
}

last_version() {
curl -sL -o /dev/null -w %{url_effective} "$RELEASES_URL/latest" |
rev |
cut -f1 -d'/'|
rev
}

download() {
version=$1
if [[ ${version} == "" ]]; then
echo >&2 "Undefined version (pass using -v|--version). Please use semantic version. Read more about it here: https://semver.org/ \n\n"
show_help
exit 1
fi

url="$RELEASES_URL/download/$version/${BINARY_NAME}_${version:1}_$(uname -s)_$(uname -m).tar.gz"

rm -f "$TAR_FILE"
curl -fsLo "$TAR_FILE" "$url" || die "Unable to download $url"
}

while test $# -gt 0; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-v)
shift
if test $# -gt 0; then
version=$1
else
die "Please provide a version name"
fi
shift
;;
--version*)
version=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-d)
shift
if test $# -gt 0; then
dir=$1
else
die "Please provide a version"
fi
shift
;;
--dir*)
dir=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
*)
die "Unknown param $1"
break
;;
esac
done

test -z "$version" && version="$(last_version)"
test -z "$version" && {
die "Unable to get ${BINARY_NAME} version. You can still try to download it manually from $RELEASES_URL."
}
test -z "$dir" && dir="$(mktemp -d --suffix=-${BINARY_NAME}-${version})"

download ${version}
tar -C "$dir" -xzf "$TAR_FILE" ${BINARY_NAME}

echo "Downloaded ${BINARY_NAME} binary ($version) to $dir"
echo "Make sure it's on your \$PATH."

0 comments on commit ade8485

Please sign in to comment.