From 01da8d48d4c3d9813f410f9b8966412462b149a3 Mon Sep 17 00:00:00 2001 From: Oleksandr Garagatyi Date: Thu, 25 Apr 2019 11:57:04 +0300 Subject: [PATCH] Fix bash usage by scripts Fix critical shell mistakes from shellcheck linter POV Signed-off-by: Oleksandr Garagatyi --- check_plugins_images.sh | 6 +++--- check_plugins_location_v1.sh | 2 +- check_plugins_location_v2.sh | 4 ++-- check_plugins_viewer_mandatory_fields_v1.sh | 4 ++-- check_plugins_viewer_mandatory_fields_v2.sh | 6 +++--- check_plugins_viewer_mandatory_fields_v3.sh | 6 +++--- cico_build.sh | 16 ++++++++-------- index.sh | 11 ++++++----- index_v2.sh | 14 ++++++++------ set_plugin_dates.sh | 5 ++--- 10 files changed, 38 insertions(+), 36 deletions(-) diff --git a/check_plugins_images.sh b/check_plugins_images.sh index 518cd37ce9..4599fa8510 100755 --- a/check_plugins_images.sh +++ b/check_plugins_images.sh @@ -13,13 +13,13 @@ set -e source ./util.sh ## check that icon tags in meta.yaml files points to the .svg images -declare -a arr=(`find . -name "meta.yaml"`) +readarray -d '' arr < <(find . -name 'meta.yaml' -print0) for i in "${arr[@]}" do - ICON=$(yq r $i icon | sed 's/^"\(.*\)"$/\1/') + ICON=$(yq r "$i" icon | sed 's/^"\(.*\)"$/\1/') # Regex: contains .svg and not contains dots after it (to avoid xxx.svg.jpg hacks) if [[ ! $ICON =~ (\.svg)+[^\.]*$ ]]; then - plugin_id=$(evaluate_plugin_id $i) + plugin_id=$(evaluate_plugin_id "$i") plugin_version=$(yq r "$i" version | sed 's/^"\(.*\)"$/\1/') plugin_publisher=$(yq r "$i" publisher | sed 's/^"\(.*\)"$/\1/') echo "!!! Wrong icon type found in '${plugin_id}' of publisher '${plugin_publisher}' with version '${plugin_version}':" diff --git a/check_plugins_location_v1.sh b/check_plugins_location_v1.sh index 872eb43fa6..47c78ea280 100755 --- a/check_plugins_location_v1.sh +++ b/check_plugins_location_v1.sh @@ -12,7 +12,7 @@ set -e source ./util.sh -declare -a arr=(`find plugins -name "meta.yaml"`) +readarray -d '' arr < <(find plugins -name 'meta.yaml' -print0) for i in "${arr[@]}" do id=$(yq r "$i" id | sed 's/^"\(.*\)"$/\1/') diff --git a/check_plugins_location_v2.sh b/check_plugins_location_v2.sh index 712c34e2f5..8b9962731c 100755 --- a/check_plugins_location_v2.sh +++ b/check_plugins_location_v2.sh @@ -16,10 +16,10 @@ set -e source ./util.sh -declare -a arr=(`find "$1" -name "meta.yaml"`) +readarray -d '' arr < <(find "$1" -name 'meta.yaml' -print0) for i in "${arr[@]}" do - plugin_id=$(evaluate_plugin_id $i) + plugin_id=$(evaluate_plugin_id "$i") expected_path="$1/plugins/${plugin_id}/meta.yaml" if [[ "${expected_path}" != "$i" ]]; then diff --git a/check_plugins_viewer_mandatory_fields_v1.sh b/check_plugins_viewer_mandatory_fields_v1.sh index 282f7b70f8..7ea1f06759 100755 --- a/check_plugins_viewer_mandatory_fields_v1.sh +++ b/check_plugins_viewer_mandatory_fields_v1.sh @@ -42,7 +42,7 @@ function check_category() { return 1 } -declare -a arr=(`find plugins -name "meta.yaml"`) +readarray -d '' arr < <(find plugins -name 'meta.yaml' -print0) for i in "${arr[@]}" do id=$(yq r "$i" id | sed 's/^"\(.*\)"$/\1/') @@ -55,7 +55,7 @@ do for FIELD in "${FIELDS[@]}" do - VALUE=$(yq r $i "$FIELD") + VALUE=$(yq r "$i" "$FIELD") if [[ "${FIELD}" == "category" ]];then if ! check_category "$i" "${VALUE}";then echo "!!! Invalid category in '${full_id}': $VALUE" diff --git a/check_plugins_viewer_mandatory_fields_v2.sh b/check_plugins_viewer_mandatory_fields_v2.sh index 5d79d29fd4..ca016d21d0 100755 --- a/check_plugins_viewer_mandatory_fields_v2.sh +++ b/check_plugins_viewer_mandatory_fields_v2.sh @@ -42,10 +42,10 @@ function check_category() { return 1 } -declare -a arr=(`find v2 -name "meta.yaml"`) +readarray -d '' arr < <(find v2 -name 'meta.yaml' -print0) for i in "${arr[@]}" do - plugin_id=$(evaluate_plugin_id $i) + plugin_id=$(evaluate_plugin_id "$i") echo "Checking plugin '${plugin_id}'" @@ -53,7 +53,7 @@ do for FIELD in "${FIELDS[@]}" do - VALUE=$(yq r $i "$FIELD") + VALUE=$(yq r "$i" "$FIELD") if [[ "${FIELD}" == "category" ]];then if ! check_category "$i" "${VALUE}";then echo "!!! Invalid category in '${plugin_id}': $VALUE" diff --git a/check_plugins_viewer_mandatory_fields_v3.sh b/check_plugins_viewer_mandatory_fields_v3.sh index 78f47c2f91..adf4fbf1d2 100755 --- a/check_plugins_viewer_mandatory_fields_v3.sh +++ b/check_plugins_viewer_mandatory_fields_v3.sh @@ -44,10 +44,10 @@ function check_category() { return 1 } -declare -a arr=(`find v3 -name "meta.yaml"`) +readarray -d '' arr < <(find v3 -name 'meta.yaml' -print0) for i in "${arr[@]}" do - plugin_id=$(evaluate_plugin_id $i) + plugin_id=$(evaluate_plugin_id "$i") echo "Checking plugin '${plugin_id}'" @@ -55,7 +55,7 @@ do for FIELD in "${FIELDS[@]}" do - VALUE=$(yq r $i "$FIELD") + VALUE=$(yq r "$i" "$FIELD") if [[ "${FIELD}" == "category" ]];then if ! check_category "$i" "${VALUE}";then echo "!!! Invalid category in '${plugin_id}': $VALUE" diff --git a/cico_build.sh b/cico_build.sh index 95d1da28cd..7208399c1f 100755 --- a/cico_build.sh +++ b/cico_build.sh @@ -50,15 +50,15 @@ function install_deps() { function tag_push() { local TARGET=$1 - docker tag ${IMAGE} $TARGET - docker push $TARGET + docker tag "${IMAGE}" "$TARGET" + docker push "$TARGET" } function deploy() { TARGET=${TARGET:-"centos"} REGISTRY="quay.io" - if [ $TARGET == "rhel" ]; then + if [ "$TARGET" == "rhel" ]; then DOCKERFILE="Dockerfile.rhel" IMAGE="rhel-che-plugin-registry" else @@ -66,8 +66,8 @@ function deploy() { IMAGE="che-plugin-registry" fi - if [ -n "${QUAY_USERNAME}" -a -n "${QUAY_PASSWORD}" ]; then - docker login -u ${QUAY_USERNAME} -p ${QUAY_PASSWORD} ${REGISTRY} + if [ -n "${QUAY_USERNAME}" ] && [ -n "${QUAY_PASSWORD}" ]; then + docker login -u "${QUAY_USERNAME}" -p "${QUAY_PASSWORD}" "${REGISTRY}" else echo "Could not login, missing credentials for the registry" fi @@ -75,10 +75,10 @@ function deploy() { # Let's deploy docker build -t ${IMAGE} -f ${DOCKERFILE} . - TAG=$(echo $GIT_COMMIT | cut -c1-${DEVSHIFT_TAG_LEN}) + TAG=$(echo "$GIT_COMMIT" | cut -c1-"${DEVSHIFT_TAG_LEN}") - tag_push ${REGISTRY}/openshiftio/$IMAGE:$TAG - tag_push ${REGISTRY}/openshiftio/$IMAGE:latest + tag_push "${REGISTRY}/openshiftio/$IMAGE:$TAG" + tag_push "${REGISTRY}/openshiftio/$IMAGE:latest" echo 'CICO: Image pushed, ready to update deployed app' } diff --git a/index.sh b/index.sh index 0454de96d4..3af9ac8c3b 100755 --- a/index.sh +++ b/index.sh @@ -26,7 +26,7 @@ function getId() { function buildIndex() { fields=('displayName' 'version' 'type' 'name' 'description' 'publisher') ## search for all editors and plugins - declare -a arr=(`find "$1" -name "meta.yaml"`) + readarray -d '' arr < <(find "$1" -name 'meta.yaml' -print0) FIRST_LINE=true echo "[" ## now loop through meta files @@ -39,12 +39,13 @@ function buildIndex() { echo ",{" fi - plugin_id=$(getId $i) + plugin_id=$(getId "$i") echo " \"id\": \"$plugin_id\"," - for field in ${fields[@]} + for field in "${fields[@]}" do - echo " \"$field\":\""$(yq r "$i" "$field" | sed 's/^"\(.*\)"$/\1/')"\"," + value="$(yq r "$i" "$field" | sed 's/^"\(.*\)"$/\1/')" + echo " \"$field\":\"$value\"," done # Add deprecate section @@ -60,7 +61,7 @@ function buildIndex() { echo " }," fi - echo " \"links\": {\"self\":\"/$(echo $i)\" }" + echo " \"links\": {\"self\":\"/$i\" }" echo "}" done echo "]" diff --git a/index_v2.sh b/index_v2.sh index 65017dd88a..2b0cf109d4 100755 --- a/index_v2.sh +++ b/index_v2.sh @@ -20,7 +20,7 @@ source ./util.sh # Arguments: # 1 - meta.yaml location function getId() { - evaluate_plugin_id $1 + evaluate_plugin_id "$1" } # getId function MUST be defined to use this function @@ -29,7 +29,7 @@ function getId() { function buildIndex() { fields=('displayName' 'version' 'type' 'name' 'description' 'publisher') ## search for all editors and plugins - declare -a arr=(`find "$1" -name "meta.yaml"`) + readarray -d '' arr < <(find "$1" -name 'meta.yaml' -print0) FIRST_LINE=true echo "[" ## now loop through meta files @@ -42,12 +42,13 @@ function buildIndex() { echo ",{" fi - plugin_id=$(getId $i) + plugin_id=$(getId "$i") echo " \"id\": \"$plugin_id\"," - for field in ${fields[@]} + for field in "${fields[@]}" do - echo " \"$field\":\""$(yq r "$i" "$field" | sed 's/^"\(.*\)"$/\1/')"\"," + value="$(yq r "$i" "$field" | sed 's/^"\(.*\)"$/\1/')" + echo " \"$field\":\"$value\"," done # Add deprecate section @@ -63,7 +64,8 @@ function buildIndex() { echo " }," fi - echo " \"links\": {\"self\":\"/$(echo $i|sed 's/\/meta.yaml$//g')\" }" + path=$(echo "$i" | sed 's/\/meta.yaml$//g') + echo " \"links\": {\"self\":\"/$path\" }" echo "}" done echo "]" diff --git a/set_plugin_dates.sh b/set_plugin_dates.sh index 8f32f4b542..8f673bb33a 100755 --- a/set_plugin_dates.sh +++ b/set_plugin_dates.sh @@ -13,8 +13,7 @@ set -e is_first_publication_date_present() { # check that first publication date is present in yaml, # and is not an null or empty value - VALUE=$(yq r "$1" firstPublicationDate) - if [ $? -ne 0 ]; then + if ! VALUE=$(yq r "$1" firstPublicationDate); then exit 1 fi @@ -24,7 +23,7 @@ is_first_publication_date_present() { return 0; } -declare -a arr=(`find . -name "meta.yaml"`) +readarray -d '' arr < <(find . -name 'meta.yaml' -print0) for i in "${arr[@]}" do DATE=$(date -I)