From b49798f81d72845cd61aced06e905414f6d7163f Mon Sep 17 00:00:00 2001 From: nickg Date: Thu, 18 May 2017 00:15:47 -0700 Subject: [PATCH] regenerate samples, get latest shell functions --- Makefile | 2 +- samples/godownloader-goreleaser.sh | 110 ++++++++++++++++------------- samples/godownloader-hugo.sh | 110 ++++++++++++++++------------- shellfn.go | 46 ++++++------ 4 files changed, 149 insertions(+), 119 deletions(-) diff --git a/Makefile b/Makefile index 9e1b5eb..b24aec7 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ help: .DEFAULT_GOAL := build -samples: build +samples: ./godownloader -repo spf13/hugo > samples/godownloader-hugo.sh ./godownloader -repo goreleaser/goreleaser > samples/godownloader-goreleaser.sh .PHONY: samples diff --git a/samples/godownloader-goreleaser.sh b/samples/godownloader-goreleaser.sh index 2af17ac..d1c0ae0 100644 --- a/samples/godownloader-goreleaser.sh +++ b/samples/godownloader-goreleaser.sh @@ -61,37 +61,39 @@ mktmpdir() { echo ${TMPDIR} } http_download() { - DEST=$1 - SOURCE=$2 - HEADER=$3 + local_file=$1 + source_url=$2 + header=$3 + headerflag='' + destflag='' if is_command curl; then - WGET="curl --fail -sSL" - test -z "${HEADER}" || WGET="${WGET} -H \"${HEADER}\"" - if [ "${DEST}" != "-" ]; then - WGET="$WGET -o $DEST" - fi - elif is_command wget &> /dev/null; then - WGET="wget -q -O $DEST" - test -z "${HEADER}" || WGET="${WGET} --header \"${HEADER}\"" + cmd='curl --fail -sSL' + destflag='-o' + headerflag='-H' + elif is_command wget; then + cmd='wget -q' + destflag='-O' + headerflag='--header' else - echo "Unable to find wget or curl. Exit" - exit 1 + echo "http_download: unable to find wget or curl" + return 1 fi - if [ "${DEST}" != "-" ]; then - rm -f "${DEST}" + if [ -z "$header" ]; then + $cmd $destflag "$local_file" "$source_url" + else + $cmd $headerflag "$header" $destflag "$local_file" "$source_url" fi - ${WGET} ${SOURCE} } github_api() { - DEST=$1 - SOURCE=$2 - HEADER="" - case $SOURCE in + local_file=$1 + source_url=$2 + header="" + case "$source_url" in https://api.github.com*) - test -z "$GITHUB_TOKEN" || HEADER="Authorization: token $GITHUB_TOKEN" + test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN" ;; esac - http_download $DEST $SOURCE $HEADER + http_download "$local_file" "$source_url" "$header" } github_last_release() { OWNER_REPO=$1 @@ -103,31 +105,41 @@ github_last_release() { echo ${VERSION} } hash_sha256() { - TARGET=${1:-$(/dev/null) || return 1 + echo $hash | cut -d ' ' -f 1 elif is_command openssl; then - openssl -dst openssl dgst -sha256 $TARGET | cut -d ' ' -f a + hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 + echo $hash | cut -d ' ' -f a else - echo "Unable to compute hash. exiting" - exit 1 + echo "hash_sha256: unable to find command to compute sha-256 hash" + return 1 fi } hash_sha256_verify() { TARGET=$1 - SUMS=$2 + checksums=$2 + if [ -z "$checksums" ]; then + echo "hash_sha256_verify: checksum file not specified in arg2" + return 1 + fi BASENAME=${TARGET##*/} - WANT=$(grep ${BASENAME} ${SUMS} | tr '\t' ' ' | cut -d ' ' -f 1) - GOT=$(hash_sha256 $TARGET) - if [ "$GOT" != "$WANT" ]; then - echo "Checksum for $TARGET did not verify" - echo "WANT: ${WANT}" - echo "GOT : ${GOT}" - exit 1 + 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}'" + return 1 + fi + got=$(hash_sha256 $TARGET) + if [ "$want" != "$got" ]; then + echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" + return 1 fi } cat /dev/null << EOF @@ -172,19 +184,21 @@ TARBALL_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${ CHECKSUM=${REPO}_checksums.txt CHECKSUM_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${CHECKSUM} -# Destructive operations start here -# -# -TMPDIR=$(mktmpdir) -http_download ${TMPDIR}/${TARBALL} ${TARBALL_URL} +# 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) + http_download ${TMPDIR}/${TARBALL} ${TARBALL_URL} -# checksum goes here -if [ 1 -eq 1 ]; then http_download ${TMPDIR}/${CHECKSUM} ${CHECKSUM_URL} hash_sha256_verify ${TMPDIR}/${TARBALL} ${TMPDIR}/${CHECKSUM} -fi -(cd ${TMPDIR} && untar ${TARBALL}) -install -d ${BINDIR} -install ${TMPDIR}/${BINARY} ${BINDIR}/ + (cd ${TMPDIR} && untar ${TARBALL}) + install -d ${BINDIR} + install ${TMPDIR}/${BINARY} ${BINDIR}/ +} + +execute diff --git a/samples/godownloader-hugo.sh b/samples/godownloader-hugo.sh index 599d53a..c972e2a 100644 --- a/samples/godownloader-hugo.sh +++ b/samples/godownloader-hugo.sh @@ -61,37 +61,39 @@ mktmpdir() { echo ${TMPDIR} } http_download() { - DEST=$1 - SOURCE=$2 - HEADER=$3 + local_file=$1 + source_url=$2 + header=$3 + headerflag='' + destflag='' if is_command curl; then - WGET="curl --fail -sSL" - test -z "${HEADER}" || WGET="${WGET} -H \"${HEADER}\"" - if [ "${DEST}" != "-" ]; then - WGET="$WGET -o $DEST" - fi - elif is_command wget &> /dev/null; then - WGET="wget -q -O $DEST" - test -z "${HEADER}" || WGET="${WGET} --header \"${HEADER}\"" + cmd='curl --fail -sSL' + destflag='-o' + headerflag='-H' + elif is_command wget; then + cmd='wget -q' + destflag='-O' + headerflag='--header' else - echo "Unable to find wget or curl. Exit" - exit 1 + echo "http_download: unable to find wget or curl" + return 1 fi - if [ "${DEST}" != "-" ]; then - rm -f "${DEST}" + if [ -z "$header" ]; then + $cmd $destflag "$local_file" "$source_url" + else + $cmd $headerflag "$header" $destflag "$local_file" "$source_url" fi - ${WGET} ${SOURCE} } github_api() { - DEST=$1 - SOURCE=$2 - HEADER="" - case $SOURCE in + local_file=$1 + source_url=$2 + header="" + case "$source_url" in https://api.github.com*) - test -z "$GITHUB_TOKEN" || HEADER="Authorization: token $GITHUB_TOKEN" + test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN" ;; esac - http_download $DEST $SOURCE $HEADER + http_download "$local_file" "$source_url" "$header" } github_last_release() { OWNER_REPO=$1 @@ -103,31 +105,41 @@ github_last_release() { echo ${VERSION} } hash_sha256() { - TARGET=${1:-$(/dev/null) || return 1 + echo $hash | cut -d ' ' -f 1 elif is_command openssl; then - openssl -dst openssl dgst -sha256 $TARGET | cut -d ' ' -f a + hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 + echo $hash | cut -d ' ' -f a else - echo "Unable to compute hash. exiting" - exit 1 + echo "hash_sha256: unable to find command to compute sha-256 hash" + return 1 fi } hash_sha256_verify() { TARGET=$1 - SUMS=$2 + checksums=$2 + if [ -z "$checksums" ]; then + echo "hash_sha256_verify: checksum file not specified in arg2" + return 1 + fi BASENAME=${TARGET##*/} - WANT=$(grep ${BASENAME} ${SUMS} | tr '\t' ' ' | cut -d ' ' -f 1) - GOT=$(hash_sha256 $TARGET) - if [ "$GOT" != "$WANT" ]; then - echo "Checksum for $TARGET did not verify" - echo "WANT: ${WANT}" - echo "GOT : ${GOT}" - exit 1 + 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}'" + return 1 + fi + got=$(hash_sha256 $TARGET) + if [ "$want" != "$got" ]; then + echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" + return 1 fi } cat /dev/null << EOF @@ -199,19 +211,21 @@ TARBALL_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${ CHECKSUM=${REPO}_checksums.txt CHECKSUM_URL=https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}/${CHECKSUM} -# Destructive operations start here -# -# -TMPDIR=$(mktmpdir) -http_download ${TMPDIR}/${TARBALL} ${TARBALL_URL} +# 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) + http_download ${TMPDIR}/${TARBALL} ${TARBALL_URL} -# checksum goes here -if [ 1 -eq 1 ]; then http_download ${TMPDIR}/${CHECKSUM} ${CHECKSUM_URL} hash_sha256_verify ${TMPDIR}/${TARBALL} ${TMPDIR}/${CHECKSUM} -fi -(cd ${TMPDIR} && untar ${TARBALL}) -install -d ${BINDIR} -install ${TMPDIR}/${BINARY} ${BINDIR}/ + (cd ${TMPDIR} && untar ${TARBALL}) + install -d ${BINDIR} + install ${TMPDIR}/${BINARY} ${BINDIR}/ +} + +execute diff --git a/shellfn.go b/shellfn.go index a9f7605..7b042f8 100644 --- a/shellfn.go +++ b/shellfn.go @@ -44,37 +44,39 @@ mktmpdir() { echo ${TMPDIR} } http_download() { - DEST=$1 - SOURCE=$2 - HEADER=$3 + local_file=$1 + source_url=$2 + header=$3 + headerflag='' + destflag='' if is_command curl; then - WGET="curl --fail -sSL" - test -z "${HEADER}" || WGET="${WGET} -H \"${HEADER}\"" - if [ "${DEST}" != "-" ]; then - WGET="$WGET -o $DEST" - fi - elif is_command wget &> /dev/null; then - WGET="wget -q -O $DEST" - test -z "${HEADER}" || WGET="${WGET} --header \"${HEADER}\"" + cmd='curl --fail -sSL' + destflag='-o' + headerflag='-H' + elif is_command wget; then + cmd='wget -q' + destflag='-O' + headerflag='--header' else - echo "Unable to find wget or curl. Exit" - exit 1 + echo "http_download: unable to find wget or curl" + return 1 fi - if [ "${DEST}" != "-" ]; then - rm -f "${DEST}" + if [ -z "$header" ]; then + $cmd $destflag "$local_file" "$source_url" + else + $cmd $headerflag "$header" $destflag "$local_file" "$source_url" fi - ${WGET} ${SOURCE} } github_api() { - DEST=$1 - SOURCE=$2 - HEADER="" - case $SOURCE in + local_file=$1 + source_url=$2 + header="" + case "$source_url" in https://api.github.com*) - test -z "$GITHUB_TOKEN" || HEADER="Authorization: token $GITHUB_TOKEN" + test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN" ;; esac - http_download $DEST $SOURCE $HEADER + http_download "$local_file" "$source_url" "$header" } github_last_release() { OWNER_REPO=$1