Skip to content

Commit

Permalink
Enable shellcheck
Browse files Browse the repository at this point in the history
Signed-off-by: John Starich <[email protected]>
  • Loading branch information
JohnStarich committed Nov 13, 2020
1 parent 91ecd0b commit 0c86ec2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 37 additions & 26 deletions hack/configure-operator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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 - <<EOT
apiVersion: v1
Expand All @@ -197,15 +200,16 @@ data:
region: $b64_region
EOT

local org=$(json_grep_after org name <<<"$target")
local space=$(json_grep_after space name <<<"$target")
local resource_group=$(json_grep_after resource_group name <<<"$target")
local resource_group_id=$(json_grep_after resource_group guid <<<"$target")
local org space resource_group resource_group_id user
org=$(json_grep_after org name <<<"$target")
space=$(json_grep_after space name <<<"$target")
resource_group=$(json_grep_after resource_group name <<<"$target")
resource_group_id=$(json_grep_after resource_group guid <<<"$target")
if [[ -z "$resource_group_id" ]]; then
error 'Resource group must be set. Run `ibmcloud target -g $resource_group` and try again.'
error "Resource group must be set. Run 'ibmcloud target -g \$resource_group' and try again."
return 2
fi
local user=$(json_grep_after user display_name <<<"$target")
user=$(json_grep_after user display_name <<<"$target")

kubectl apply -f - <<EOT
apiVersion: v1
Expand Down Expand Up @@ -235,16 +239,18 @@ release_action() {
if [[ "$version" != latest ]]; then
download_version="tags/v${version}"
fi
local release=$(curl -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/IBM/cloud-operators/releases/$download_version")
local urls=$(json_grep browser_download_url -1 <<<"$release")
local release urls
release=$(curl -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/IBM/cloud-operators/releases/$download_version")
urls=$(json_grep browser_download_url -1 <<<"$release")
local file_urls=()
while read -r url; do
if ! [[ "$url" =~ package.yaml|clusterserviceversion.yaml ]]; then
file_urls+=("$url")
fi
done <<<"$urls"

local assets=$(fetch_assets "${file_urls[@]}")
local assets
assets=$(fetch_assets "${file_urls[@]}")

if [[ "$action" == apply ]]; then
# Apply specially prefixed resources first. Typically these are namespaces and services.
Expand Down Expand Up @@ -288,6 +294,11 @@ while getopts "hv:" opt; do
fi
VERSION=$OPTARG
;;
*)
error "Invalid flag: $opt"
usage
exit 2
;;
esac
done
shift $((OPTIND-1))
Expand Down

0 comments on commit 0c86ec2

Please sign in to comment.