Skip to content

Commit

Permalink
add new installer
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Mar 9, 2018
1 parent b90dc15 commit c0b55c8
Showing 1 changed file with 78 additions and 34 deletions.
112 changes: 78 additions & 34 deletions install-misspell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ usage() {
cat <<EOF
$this: download go binaries for client9/misspell
Usage: $this [-b] bindir [version]
-b sets bindir or installation directory, default "./bin"
[version] is a version number from
Usage: $this [-b] bindir [tag]
-b sets bindir or installation directory, Defaults to ./bin
[tag] is a tag from
https://github.com/client9/misspell/releases
If version is missing, then an attempt to find the latest will be found.
If tag is missing, then an attempt to find the latest will be found.
Generated by godownloader
https://github.com/goreleaser/godownloader
Consider setting GITHUB_TOKEN to avoid triggering GitHub rate limits.
See the following for more details:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
Generated by godownloader
https://github.com/goreleaser/godownloader
EOF
exit 2
Expand All @@ -33,35 +37,36 @@ parse_args() {
esac
done
shift $((OPTIND - 1))
VERSION=$1
TAG=$1
}
# this function wraps all the destructive operations
# if a curl|bash cuts off the end of the script due to
# network, either nothing will happen or will syntax error
# out preventing half-done work
execute() {
TMPDIR=$(mktmpdir)
echo "$PREFIX: downloading ${TARBALL_URL}"
log_debug "downloading tarball ${TARBALL_URL}"
http_download "${TMPDIR}/${TARBALL}" "${TARBALL_URL}"

echo "$PREFIX: verifying checksums"
log_debug "downloading checksum ${CHECKSUM_URL}"
http_download "${TMPDIR}/${CHECKSUM}" "${CHECKSUM_URL}"
hash_sha256_verify "${TMPDIR}/${TARBALL}" "${TMPDIR}/${CHECKSUM}"

(cd "${TMPDIR}" && untar "${TARBALL}")
install -d "${BINDIR}"
install "${TMPDIR}/${BINARY}" "${BINDIR}/"
echo "$PREFIX: installed as ${BINDIR}/${BINARY}"
log_info "installed as ${BINDIR}/${BINARY}"
}
is_supported_platform() {
platform=$1
found=1
case "$platform" in
darwin/amd64) found=0 ;;
linux/amd64) found=0 ;;
windows/amd64) found=0 ;;
esac
case "$platform" in
darwin/386) found=1 ;;
windows/386) found=1 ;;
esac
return $found
}
Expand All @@ -70,17 +75,17 @@ check_platform() {
# optional logging goes here
true
else
echo "${PREFIX}: platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
exit 1
fi
}
adjust_version() {
if [ -z "${VERSION}" ]; then
echo "$PREFIX: checking GitHub for latest version"
VERSION=$(github_last_release "$OWNER/$REPO")
tag_to_version() {
if [ -z "${TAG}" ]; then
log_info "checking GitHub for latest tag"
TAG=$(github_last_release "$OWNER/$REPO")
fi
# if version starts with 'v', remove it
VERSION=${VERSION#v}
VERSION=${TAG#v}
}
adjust_format() {
# change format (tar.gz or zip) based on ARCH
Expand Down Expand Up @@ -116,8 +121,40 @@ EOF
is_command() {
command -v "$1" >/dev/null
}
echoerr() {
echo "$@" 1>&2
}
log_prefix() {
echo "$0"
}
_logp=6
log_set_priority() {
_logp="$1"
}
log_priority() {
if test -z "$1"; then
echo "$_logp"
return
fi
[ "$1" -ge "$_logp" ]
}
log_debug() {
log_priority 7 && echoerr "$(log_prefix)" "DEBUG" "$@"
}
log_info() {
log_priority 6 && echoerr "$(log_prefix)" "INFO" "$@"
}
log_err() {
log_priority 3 && echoerr "$(log_prefix)" "ERR" "$@"
}
log_crit() {
log_priority 2 && echoerr "$(log_prefix)" "CRIT" "$@"
}
uname_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
msys_nt) os="windows" ;;
esac
echo "$os"
}
uname_arch() {
Expand Down Expand Up @@ -149,7 +186,7 @@ uname_os_check() {
solaris) return 0 ;;
windows) return 0 ;;
esac
echo "$0: uname_os_check: internal error '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
return 1
}
uname_arch_check() {
Expand All @@ -170,7 +207,7 @@ uname_arch_check() {
s390x) return 0 ;;
amd64p32) return 0 ;;
esac
echo "$0: uname_arch_check: internal error '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
return 1
}
untar() {
Expand All @@ -180,7 +217,7 @@ untar() {
*.tar) tar -xf "${tarball}" ;;
*.zip) unzip "${tarball}" ;;
*)
echo "Unknown archive format for ${tarball}"
log_err "untar unknown archive format for ${tarball}"
return 1
;;
esac
Expand All @@ -205,7 +242,7 @@ http_download() {
destflag='-O'
headerflag='--header'
else
echo "http_download: unable to find wget or curl"
log_crit "http_download unable to find wget or curl"
return 1
fi
if [ -z "$header" ]; then
Expand All @@ -227,9 +264,11 @@ github_api() {
}
github_last_release() {
owner_repo=$1
giturl="https://api.github.com/repos/${owner_repo}/releases/latest"
html=$(github_api - "$giturl")
version=$(echo "$html" | grep -m 1 "\"tag_name\":" | cut -f4 -d'"')
version=$2
test -z "$version" && version="latest"
giturl="https://github.com/${owner_repo}/releases/${version}"
json=$(http_download "-" "$giturl" "Accept:application/json")
version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
test -z "$version" && return 1
echo "$version"
}
Expand All @@ -248,26 +287,26 @@ hash_sha256() {
hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
echo "$hash" | cut -d ' ' -f a
else
echo "hash_sha256: unable to find command to compute sha-256 hash"
log_crit "hash_sha256 unable to find command to compute sha-256 hash"
return 1
fi
}
hash_sha256_verify() {
TARGET=$1
checksums=$2
if [ -z "$checksums" ]; then
echo "hash_sha256_verify: checksum file not specified in arg2"
log_err "hash_sha256_verify checksum file not specified in arg2"
return 1
fi
BASENAME=${TARGET##*/}
want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
if [ -z "$want" ]; then
echo "hash_sha256_verify: unable to find checksum for '${TARGET}' in '${checksums}'"
log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
return 1
fi
got=$(hash_sha256 "$TARGET")
if [ "$want" != "$got" ]; then
echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got"
log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
return 1
fi
}
Expand All @@ -278,12 +317,17 @@ End of functions from https://github.com/client9/shlib
EOF

OWNER=client9
REPO=misspell
REPO="misspell"
BINARY=misspell
FORMAT=tar.gz
OS=$(uname_os)
ARCH=$(uname_arch)
PREFIX="$OWNER/$REPO"

# use in logging routines
log_prefix() {
echo "$PREFIX"
}
PLATFORM="${OS}/${ARCH}"
GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download

Expand All @@ -294,21 +338,21 @@ parse_args "$@"

check_platform

adjust_version
tag_to_version

adjust_format

adjust_os

adjust_arch

echo "$PREFIX: found version ${VERSION} for ${OS}/${ARCH}"
log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"

NAME=${BINARY}_${VERSION}_${OS}_${ARCH}
TARBALL=${NAME}.${FORMAT}
TARBALL_URL=${GITHUB_DOWNLOAD}/v${VERSION}/${TARBALL}
CHECKSUM=${REPO}_checksums.txt
CHECKSUM_URL=${GITHUB_DOWNLOAD}/v${VERSION}/${CHECKSUM}
TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
CHECKSUM=${BINARY}_${VERSION}_checksums.txt
CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}

# Adjust binary name if windows
if [ "$OS" = "windows" ]; then
Expand Down

1 comment on commit c0b55c8

@irajkermansha
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good

Please sign in to comment.