From 0c86ec21a63569d491c6b8a1c67ed1f04bf43553 Mon Sep 17 00:00:00 2001 From: John Starich Date: Fri, 13 Nov 2020 02:37:30 -0600 Subject: [PATCH] Enable shellcheck Signed-off-by: John Starich --- Makefile | 5 +++ hack/configure-operator.sh | 63 ++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 721e6509..b2f3d61e 100644 --- a/Makefile +++ b/Makefile @@ -124,10 +124,15 @@ lint-deps: set -o pipefail; \ curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v${LINT_VERSION}; \ fi + @if ! which shellcheck; then \ + set -ex; curl -fsSL https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.$$(uname).x86_64.tar.xz | tar -xJv --strip-components=1 shellcheck-stable/shellcheck; \ + mv shellcheck $(shell go env GOPATH)/bin/shellcheck; chmod +x $(shell go env GOPATH)/bin/shellcheck; \ + fi .PHONY: lint lint: lint-deps golangci-lint run + find . -name '*.*sh' | xargs shellcheck --color .PHONY: lint-fix lint-fix: lint-deps diff --git a/hack/configure-operator.sh b/hack/configure-operator.sh index 2ac26824..d69c32c8 100755 --- a/hack/configure-operator.sh +++ b/hack/configure-operator.sh @@ -32,7 +32,7 @@ set -o pipefail shopt -s extglob TMPDIR=$(mktemp -d) -trap "set -x; rm -rf '$TMPDIR'" EXIT +trap "set -x; rm -rf '""$TMPDIR""'" EXIT # error prints the arguments to stderr. If printing to a TTY, adds red color. error() { @@ -76,7 +76,7 @@ json_grep() { declare -i max_matches=${2:-1} matches=0 while read -r line; do if [[ "$line" =~ $pattern ]]; then - printf "${BASH_REMATCH[1]}" + printf '%s' "${BASH_REMATCH[1]}" if (( max_matches == -1 || max_matches > 1 )); then echo # add new line fi @@ -93,7 +93,7 @@ json_grep_after() { local after=$1 local pattern=$2 while read -r line; do - if [[ "$line" =~ "$after" ]]; then + if [[ "$line" == "$after" ]]; then json_grep "$pattern" break fi @@ -105,7 +105,7 @@ fetch_assets() { local file_urls=("$@") if [[ -n "$DEBUG_OUT" ]]; then # Use custom assets directory for debugging purposes. - printf "$DEBUG_OUT" + printf '%s' "$DEBUG_OUT" return fi @@ -115,7 +115,7 @@ fetch_assets() { ls "$TMPDIR" >&2 popd >/dev/null - printf "$TMPDIR" + printf '%s' "$TMPDIR" } # valid_semver returns 0 if $1 is a valid semver number. Only allows MAJOR.MINOR.PATCH format. @@ -140,24 +140,24 @@ compare_semver() { semver2=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") fi - if (( ${semver1[0]} != ${semver2[0]} )); then - if (( ${semver1[0]} > ${semver2[0]} )); then + if (( semver1[0] != semver2[0] )); then + if (( semver1[0] > semver2[0] )); then echo 1 return fi echo -1 return fi - if (( ${semver1[1]} != ${semver2[1]} )); then - if (( ${semver1[1]} > ${semver2[1]} )); then + if (( semver1[1] != semver2[1] )); then + if (( semver1[1] > semver2[1] )); then echo 1 return fi echo -1 return fi - if (( ${semver1[2]} != ${semver2[2]} )); then - if (( ${semver1[2]} > ${semver2[2]} )); then + if (( semver1[2] != semver2[2] )); then + if (( semver1[2] > semver2[2] )); then echo 1 return fi @@ -170,18 +170,21 @@ compare_semver() { # store_creds ensures an API key Secret and operator ConfigMap are set up store_creds() { if [[ -z "$IBMCLOUD_API_KEY" ]]; then - local key_output=$(ibmcloud iam api-key-create ibmcloud-operator-key -d "Key for IBM Cloud Operator" --output json) + local key_output + key_output=$(ibmcloud iam api-key-create ibmcloud-operator-key -d "Key for IBM Cloud Operator" --output json) IBMCLOUD_API_KEY=$(json_grep apikey <<<"$key_output") fi IBMCLOUD_API_KEY=$(trim "$IBMCLOUD_API_KEY") - local target=$(ibmcloud target --output json) - local region=$(json_grep_after region name <<<"$target") + local target region + target=$(ibmcloud target --output json) + region=$(json_grep_after region name <<<"$target") if [[ -z "$region" ]]; then - error 'Region must be set. Run `ibmcloud target -r $region` and try again.' + error "Region must be set. Run 'ibmcloud target -r \$region' and try again." return 2 fi - local b64_region=$(printf "$region" | base64) - local b64_apikey=$(printf "$IBMCLOUD_API_KEY" | base64) + local b64_region b64_apikey + b64_region=$(printf '%s' "$region" | base64) + b64_apikey=$(printf '%s' "$IBMCLOUD_API_KEY" | base64) kubectl apply -f - <