From 96d94382cddf2b67bf4f018624a826a2bd156867 Mon Sep 17 00:00:00 2001 From: nickg Date: Fri, 19 May 2017 08:54:03 -0700 Subject: [PATCH] resync shell, add shellcheck --- Makefile | 9 ++++++- makeshellfn.sh | 12 ++++----- samples/godownloader-goreleaser.sh | 39 +++++++++++++++--------------- samples/godownloader-hugo.sh | 39 +++++++++++++++--------------- samples/godownloader-misspell.sh | 39 +++++++++++++++--------------- shellfn.go | 39 +++++++++++++++--------------- 6 files changed, 94 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index 5aa1eab..279a33e 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ setup: ## Install all the build and lint dependencies test: ## Run all the tests gotestcover $(TEST_OPTIONS) -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s + shellcheck samples/godownloader-goreleaser.sh cover: test ## Run all the tests and opens the coverage report go tool cover -html=coverage.txt @@ -35,7 +36,13 @@ lint: ## Run all the linters --deadline=10m \ ./... -ci: build lint test samples ## Run all the tests and code checks as travis-ci does +lint_shell: ## shellcheck the shell scripts + shellcheck -s sh samples/godownloader-goreleaser.sh + shellcheck -s bash samples/godownloader-goreleaser.sh + shellcheck -s dash samples/godownloader-goreleaser.sh + shellcheck -s ksh samples/godownloader-goreleaser.sh + +ci: build samples lint test lint_shell ## Run all the tests and code checks as travis-ci does build: ## Build a beta version of goreleaser go build diff --git a/makeshellfn.sh b/makeshellfn.sh index 0f53fed..b5ad44b 100755 --- a/makeshellfn.sh +++ b/makeshellfn.sh @@ -2,13 +2,13 @@ set -e git_clone_or_update() { - URL=$1 - REPO=${URL##*/} # foo.git - REPO=${REPO%.git} # foo - if [ ! -d "$REPO" ]; then - git clone ${URL} + giturl=$1 + gitrepo=${giturl##*/} # foo.git + gitrepo=${gitrepo%.git} # foo + if [ ! -d "$gitrepo" ]; then + git clone "$giturl" else - (cd ${REPO} && git pull > /dev/null) + (cd "$gitrepo" && git pull > /dev/null) fi } diff --git a/samples/godownloader-goreleaser.sh b/samples/godownloader-goreleaser.sh index 17ef13c..a0c9de6 100644 --- a/samples/godownloader-goreleaser.sh +++ b/samples/godownloader-goreleaser.sh @@ -28,11 +28,11 @@ but credits (and pull requests) appreciated. ------------------------------------------------------------------------ EOF is_command() { - type $1 > /dev/null 2> /dev/null + command -v "$1" > /dev/null } uname_os() { os=$(uname -s | tr '[:upper:]' '[:lower:]') - echo ${os} + echo "$os" } uname_arch() { arch=$(uname -m) @@ -83,10 +83,10 @@ uname_arch_check() { } untar() { tarball=$1 - case ${tarball} in - *.tar.gz|*.tgz) tar -xzf ${tarball} ;; - *.tar) tar -xf ${tarball} ;; - *.zip) unzip ${tarball} ;; + case "${tarball}" in + *.tar.gz|*.tgz) tar -xzf "${tarball}" ;; + *.tar) tar -xf "${tarball}" ;; + *.zip) unzip "${tarball}" ;; *) echo "Unknown archive format for ${tarball}" return 1 @@ -94,8 +94,8 @@ untar() { } mktmpdir() { test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" - mkdir -p ${TMPDIR} - echo ${TMPDIR} + mkdir -p "${TMPDIR}" + echo "${TMPDIR}" } http_download() { local_file=$1 @@ -134,7 +134,8 @@ github_api() { } github_last_release() { owner_repo=$1 - html=$(github_api - https://api.github.com/repos/${owner_repo}/releases/latest) + giturl="https://api.github.com/repos/${owner_repo}/releases/latest" + html=$(github_api - "$giturl") version=$(echo "$html" | grep -m 1 "\"name\":" | cut -d ":" -f 2 | tr -d ' ",') test -z "$version" && return 1 echo "$version" @@ -142,17 +143,17 @@ github_last_release() { hash_sha256() { TARGET=${1:-/dev/stdin}; if is_command gsha256sum; then - hash=$(gsha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(gsha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command sha256sum; then - hash=$(sha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(sha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command shasum; then - hash=$(shasum -a 256 $TARGET 2>/dev/null) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command openssl; then - hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 - echo $hash | cut -d ' ' -f a + 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" return 1 @@ -166,12 +167,12 @@ hash_sha256_verify() { return 1 fi BASENAME=${TARGET##*/} - want=$(grep ${BASENAME} "${checksums}" 2> /dev/null | tr '\t' ' ' | cut -d ' ' -f 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) + got=$(hash_sha256 "$TARGET") if [ "$want" != "$got" ]; then echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" return 1 diff --git a/samples/godownloader-hugo.sh b/samples/godownloader-hugo.sh index d755ba3..2f8395d 100644 --- a/samples/godownloader-hugo.sh +++ b/samples/godownloader-hugo.sh @@ -28,11 +28,11 @@ but credits (and pull requests) appreciated. ------------------------------------------------------------------------ EOF is_command() { - type $1 > /dev/null 2> /dev/null + command -v "$1" > /dev/null } uname_os() { os=$(uname -s | tr '[:upper:]' '[:lower:]') - echo ${os} + echo "$os" } uname_arch() { arch=$(uname -m) @@ -83,10 +83,10 @@ uname_arch_check() { } untar() { tarball=$1 - case ${tarball} in - *.tar.gz|*.tgz) tar -xzf ${tarball} ;; - *.tar) tar -xf ${tarball} ;; - *.zip) unzip ${tarball} ;; + case "${tarball}" in + *.tar.gz|*.tgz) tar -xzf "${tarball}" ;; + *.tar) tar -xf "${tarball}" ;; + *.zip) unzip "${tarball}" ;; *) echo "Unknown archive format for ${tarball}" return 1 @@ -94,8 +94,8 @@ untar() { } mktmpdir() { test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" - mkdir -p ${TMPDIR} - echo ${TMPDIR} + mkdir -p "${TMPDIR}" + echo "${TMPDIR}" } http_download() { local_file=$1 @@ -134,7 +134,8 @@ github_api() { } github_last_release() { owner_repo=$1 - html=$(github_api - https://api.github.com/repos/${owner_repo}/releases/latest) + giturl="https://api.github.com/repos/${owner_repo}/releases/latest" + html=$(github_api - "$giturl") version=$(echo "$html" | grep -m 1 "\"name\":" | cut -d ":" -f 2 | tr -d ' ",') test -z "$version" && return 1 echo "$version" @@ -142,17 +143,17 @@ github_last_release() { hash_sha256() { TARGET=${1:-/dev/stdin}; if is_command gsha256sum; then - hash=$(gsha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(gsha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command sha256sum; then - hash=$(sha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(sha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command shasum; then - hash=$(shasum -a 256 $TARGET 2>/dev/null) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command openssl; then - hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 - echo $hash | cut -d ' ' -f a + 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" return 1 @@ -166,12 +167,12 @@ hash_sha256_verify() { return 1 fi BASENAME=${TARGET##*/} - want=$(grep ${BASENAME} "${checksums}" 2> /dev/null | tr '\t' ' ' | cut -d ' ' -f 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) + got=$(hash_sha256 "$TARGET") if [ "$want" != "$got" ]; then echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" return 1 diff --git a/samples/godownloader-misspell.sh b/samples/godownloader-misspell.sh index c6acb33..07cac9b 100644 --- a/samples/godownloader-misspell.sh +++ b/samples/godownloader-misspell.sh @@ -28,11 +28,11 @@ but credits (and pull requests) appreciated. ------------------------------------------------------------------------ EOF is_command() { - type $1 > /dev/null 2> /dev/null + command -v "$1" > /dev/null } uname_os() { os=$(uname -s | tr '[:upper:]' '[:lower:]') - echo ${os} + echo "$os" } uname_arch() { arch=$(uname -m) @@ -83,10 +83,10 @@ uname_arch_check() { } untar() { tarball=$1 - case ${tarball} in - *.tar.gz|*.tgz) tar -xzf ${tarball} ;; - *.tar) tar -xf ${tarball} ;; - *.zip) unzip ${tarball} ;; + case "${tarball}" in + *.tar.gz|*.tgz) tar -xzf "${tarball}" ;; + *.tar) tar -xf "${tarball}" ;; + *.zip) unzip "${tarball}" ;; *) echo "Unknown archive format for ${tarball}" return 1 @@ -94,8 +94,8 @@ untar() { } mktmpdir() { test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" - mkdir -p ${TMPDIR} - echo ${TMPDIR} + mkdir -p "${TMPDIR}" + echo "${TMPDIR}" } http_download() { local_file=$1 @@ -134,7 +134,8 @@ github_api() { } github_last_release() { owner_repo=$1 - html=$(github_api - https://api.github.com/repos/${owner_repo}/releases/latest) + giturl="https://api.github.com/repos/${owner_repo}/releases/latest" + html=$(github_api - "$giturl") version=$(echo "$html" | grep -m 1 "\"name\":" | cut -d ":" -f 2 | tr -d ' ",') test -z "$version" && return 1 echo "$version" @@ -142,17 +143,17 @@ github_last_release() { hash_sha256() { TARGET=${1:-/dev/stdin}; if is_command gsha256sum; then - hash=$(gsha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(gsha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command sha256sum; then - hash=$(sha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(sha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command shasum; then - hash=$(shasum -a 256 $TARGET 2>/dev/null) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command openssl; then - hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 - echo $hash | cut -d ' ' -f a + 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" return 1 @@ -166,12 +167,12 @@ hash_sha256_verify() { return 1 fi BASENAME=${TARGET##*/} - want=$(grep ${BASENAME} "${checksums}" 2> /dev/null | tr '\t' ' ' | cut -d ' ' -f 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) + got=$(hash_sha256 "$TARGET") if [ "$want" != "$got" ]; then echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" return 1 diff --git a/shellfn.go b/shellfn.go index aae6723..d636df6 100644 --- a/shellfn.go +++ b/shellfn.go @@ -11,11 +11,11 @@ but credits (and pull requests) appreciated. ------------------------------------------------------------------------ EOF is_command() { - type $1 > /dev/null 2> /dev/null + command -v "$1" > /dev/null } uname_os() { os=$(uname -s | tr '[:upper:]' '[:lower:]') - echo ${os} + echo "$os" } uname_arch() { arch=$(uname -m) @@ -66,10 +66,10 @@ uname_arch_check() { } untar() { tarball=$1 - case ${tarball} in - *.tar.gz|*.tgz) tar -xzf ${tarball} ;; - *.tar) tar -xf ${tarball} ;; - *.zip) unzip ${tarball} ;; + case "${tarball}" in + *.tar.gz|*.tgz) tar -xzf "${tarball}" ;; + *.tar) tar -xf "${tarball}" ;; + *.zip) unzip "${tarball}" ;; *) echo "Unknown archive format for ${tarball}" return 1 @@ -77,8 +77,8 @@ untar() { } mktmpdir() { test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" - mkdir -p ${TMPDIR} - echo ${TMPDIR} + mkdir -p "${TMPDIR}" + echo "${TMPDIR}" } http_download() { local_file=$1 @@ -117,7 +117,8 @@ github_api() { } github_last_release() { owner_repo=$1 - html=$(github_api - https://api.github.com/repos/${owner_repo}/releases/latest) + giturl="https://api.github.com/repos/${owner_repo}/releases/latest" + html=$(github_api - "$giturl") version=$(echo "$html" | grep -m 1 "\"name\":" | cut -d ":" -f 2 | tr -d ' ",') test -z "$version" && return 1 echo "$version" @@ -125,17 +126,17 @@ github_last_release() { hash_sha256() { TARGET=${1:-/dev/stdin}; if is_command gsha256sum; then - hash=$(gsha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(gsha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command sha256sum; then - hash=$(sha256sum $TARGET) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(sha256sum "$TARGET") || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command shasum; then - hash=$(shasum -a 256 $TARGET 2>/dev/null) || return 1 - echo $hash | cut -d ' ' -f 1 + hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 + echo "$hash" | cut -d ' ' -f 1 elif is_command openssl; then - hash=$(openssl -dst openssl dgst -sha256 $TARGET) || return 1 - echo $hash | cut -d ' ' -f a + 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" return 1 @@ -149,12 +150,12 @@ hash_sha256_verify() { return 1 fi BASENAME=${TARGET##*/} - want=$(grep ${BASENAME} "${checksums}" 2> /dev/null | tr '\t' ' ' | cut -d ' ' -f 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) + got=$(hash_sha256 "$TARGET") if [ "$want" != "$got" ]; then echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" return 1