From bb92568f3a1db52519b88c5eebfa9f7af94eb1e0 Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Fri, 18 May 2018 00:31:30 +0800 Subject: [PATCH] Improve coding style of shell scripts --- functions.sh | 54 ++++++++++----------- generate-stackbrew-library.sh | 27 +++++------ generate-stackbrew-pr.sh | 88 +++++++++++++++++------------------ test-build.sh | 36 +++++++------- update.sh | 8 ++-- 5 files changed, 107 insertions(+), 106 deletions(-) diff --git a/functions.sh b/functions.sh index c54e8479..eb738561 100755 --- a/functions.sh +++ b/functions.sh @@ -1,5 +1,5 @@ -#!/bin/bash - +#!/usr/bin/env bash +# # Utlity functions info() { @@ -37,12 +37,12 @@ function get_arch() { arch="arm32v7" ;; *) - echo "$0 does not support architecture $arch ... aborting" + echo "$0 does not support architecture ${arch} ... aborting" exit 1 ;; esac - echo "$arch" + echo "${arch}" } # Get corresponding variants based on the architecture. @@ -62,12 +62,12 @@ function get_variants() { arch=$(get_arch) variantsfilter=("$@") - IFS=' ' read -ra availablevariants <<<"$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')" + IFS=' ' read -ra availablevariants <<<"$(grep "^${arch}" "${dir}/architectures" | sed -E 's/'"${arch}"'[[:space:]]*//' | sed -E 's/,/ /g')" if [ ${#variantsfilter[@]} -gt 0 ]; then for variant1 in "${availablevariants[@]}"; do for variant2 in "${variantsfilter[@]}"; do - if [[ "$variant1" == "$variant2" ]]; then + if [ "$variant1" = "$variant2" ]; then variants+=("$variant1") fi done @@ -100,16 +100,16 @@ function get_supported_arches() { shift # Get default supported arches - lines=$(grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1) + lines=$(grep "$variant" "$(dirname "${version}")"/architectures 2>/dev/null | cut -d' ' -f1) # Get version specific supported architectures if there is specialized information - if [ -a "$version"/architectures ]; then - lines=$(grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1) + if [ -a "${version}"/architectures ]; then + lines=$(grep "$variant" "${version}"/architectures 2>/dev/null | cut -d' ' -f1) fi while IFS='' read -r line; do - arches+=("$line") - done <<<"$lines" + arches+=("${line}") + done <<<"${lines}" echo "${arches[@]}" } @@ -127,7 +127,7 @@ function get_config() { shift local value - value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//') + value=$(grep "^$name" "${dir}/config" | sed -E 's/'"$name"'[[:space:]]*//') echo "$value" } @@ -150,13 +150,13 @@ function get_versions() { fi for dir in "${dirs[@]}"; do - if [ -a "$dir/config" ]; then + if [ -a "${dir}/config" ]; then local subdirs IFS=' ' read -ra subdirs <<<"$(get_versions "${dir#./}")" for subdir in "${subdirs[@]}"; do versions+=("$subdir") done - elif [ -a "$dir/Dockerfile" ]; then + elif [ -a "${dir}/Dockerfile" ]; then versions+=("${dir#./}") fi done @@ -171,7 +171,7 @@ function get_fork_name() { version=$1 shift - IFS='/' read -ra versionparts <<<"$version" + IFS='/' read -ra versionparts <<<"${version}" if [ ${#versionparts[@]} -gt 1 ]; then echo "${versionparts[0]}" fi @@ -182,7 +182,7 @@ function get_full_version() { version=$1 shift - grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3 + grep -m1 'ENV NODE_VERSION ' "${version}/Dockerfile" | cut -d' ' -f3 } function get_major_minor_version() { @@ -191,9 +191,9 @@ function get_major_minor_version() { shift local fullversion - fullversion=$(get_full_version "$version") + fullversion=$(get_full_version "${version}") - echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)" + echo "$(echo "${fullversion}" | cut -d'.' -f1).$(echo "${fullversion}" | cut -d'.' -f2)" } function get_tag() { @@ -206,14 +206,14 @@ function get_tag() { shift local tagversion - if [ "$versiontype" = full ]; then - tagversion=$(get_full_version "$version") - elif [ "$versiontype" = majorminor ]; then - tagversion=$(get_major_minor_version "$version") + if [ "${versiontype}" = full ]; then + tagversion=$(get_full_version "${version}") + elif [ "${versiontype}" = majorminor ]; then + tagversion=$(get_major_minor_version "${version}") fi local tagparts - IFS=' ' read -ra tagparts <<<"$(get_fork_name "$version") $tagversion" + IFS=' ' read -ra tagparts <<<"$(get_fork_name "${version}") ${tagversion}" IFS='-' echo "${tagparts[*]}" unset IFS @@ -230,12 +230,12 @@ function sort_versions() { unset IFS while IFS='' read -r line; do - sorted+=("$line") - done <<<"$(echo "$lines" | grep "^[0-9]" | sort -r)" + sorted+=("${line}") + done <<<"$(echo "${lines}" | grep "^[0-9]" | sort -r)" while IFS='' read -r line; do - sorted+=("$line") - done <<<"$(echo "$lines" | grep -v "^[0-9]" | sort -r)" + sorted+=("${line}") + done <<<"$(echo "${lines}" | grep -v "^[0-9]" | sort -r)" echo "${sorted[@]}" } diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh index f40879d1..82f60848 100755 --- a/generate-stackbrew-library.sh +++ b/generate-stackbrew-library.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash + set -e . functions.sh @@ -31,7 +32,7 @@ fileCommit() { git log -1 --format='format:%H' HEAD -- "$@" } -echo "# this file is generated via ${url}/blob/$(fileCommit "$self")/$self" +echo "# this file is generated via ${url}/blob/$(fileCommit "${self}")/${self}" echo echo "Maintainers: The Node.js Docker Team <${url}> (@nodejs)" echo "GitRepo: ${url}.git" @@ -49,7 +50,7 @@ join() { get_stub() { local version="$1" shift - IFS='/' read -ra versionparts <<<"$version" + IFS='/' read -ra versionparts <<<"${version}" local stub eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')" echo "$stub" @@ -57,16 +58,16 @@ get_stub() { for version in "${versions[@]}"; do # Skip "docs" and other non-docker directories - [ -f "$version/Dockerfile" ] || continue + [ -f "${version}/Dockerfile" ] || continue - stub=$(get_stub "$version") - commit="$(fileCommit "$version")" - fullVersion="$(get_tag "$version" full)" - majorMinorVersion="$(get_tag "$version" majorminor)" + stub=$(get_stub "${version}") + commit="$(fileCommit "${version}")" + fullVersion="$(get_tag "${version}" full)" + majorMinorVersion="$(get_tag "${version}" majorminor)" IFS=' ' read -ra versionAliases <<<"$fullVersion $majorMinorVersion $stub" # Get supported architectures for a specific version. See details in function.sh - IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "default")" + IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "${version}" "default")" echo "Tags: $(join ', ' "${versionAliases[@]}")" echo "Architectures: $(join ', ' "${supportedArches[@]}")" @@ -76,19 +77,19 @@ for version in "${versions[@]}"; do # Get supported variants according to the target architecture. # See details in function.sh - IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")")" + IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "${version}")")" for variant in "${variants[@]}"; do # Skip non-docker directories - [ -f "$version/$variant/Dockerfile" ] || continue + [ -f "${version}/${variant}/Dockerfile" ] || continue - commit="$(fileCommit "$version/$variant")" + commit="$(fileCommit "${version}/${variant}")" slash='/' variantAliases=("${versionAliases[@]/%/-${variant//$slash/-}}") variantAliases=("${variantAliases[@]//latest-/}") # Get supported architectures for a specific version and variant. # See details in function.sh - IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "$variant")" + IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "${version}" "${variant}")" echo "Tags: $(join ', ' "${variantAliases[@]}")" echo "Architectures: $(join ', ' "${supportedArches[@]}")" diff --git a/generate-stackbrew-pr.sh b/generate-stackbrew-pr.sh index a4972ae4..968f8bd6 100755 --- a/generate-stackbrew-pr.sh +++ b/generate-stackbrew-pr.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash + set -e . functions.sh @@ -16,19 +17,19 @@ else fi fi -if [[ "$COMMIT_MESSAGE" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then +if [[ "${COMMIT_MESSAGE}" =~ Merge\ pull\ request\ \#([0-9]*) ]]; then # This is a merge from a pull request PR_NUMBER="${BASH_REMATCH[1]}" - COMMIT_MESSAGE="$(printf "%s" "$COMMIT_MESSAGE" | tail -n 1)" + COMMIT_MESSAGE="$(printf "%s" "${COMMIT_MESSAGE}" | tail -n 1)" fi IMAGES_FILE="library/node" REPO_NAME="official-images" -ORIGIN_SLUG="$GITHUB_USERNAME/$REPO_NAME" -UPSTREAM_SLUG="docker-library/$REPO_NAME" +ORIGIN_SLUG="${GITHUB_USERNAME}/${REPO_NAME}" +UPSTREAM_SLUG="docker-library/${REPO_NAME}" DOCKER_SLUG="nodejs/docker-node" -gitpath="../$REPO_NAME" +gitpath="../${REPO_NAME}" function updated() { local versions @@ -38,13 +39,12 @@ function updated() { IFS=',' get_versions )" - images_changed=$(git diff --name-only "$COMMIT_ID".."$COMMIT_ID"~1 "${versions[@]}") + images_changed=$(git diff --name-only "${COMMIT_ID}".."${COMMIT_ID}"~1 "${versions[@]}") if [ -z "$images_changed" ]; then return 1 - else - return 0 fi + return 0 } function auth_header() { @@ -60,48 +60,48 @@ function permission_check() { -s \ "https://api.github.com")" - if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then + if [ "$(echo "$auth" | jq -r .message)" = "Bad credentials" ]; then fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN" fi auth="$(curl -H "$(auth_header)" \ -s \ - "https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")" - if [ "$(echo "$auth" | jq .message)" != "null" ]; then - fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git" + "https://api.github.com/repos/${ORIGIN_SLUG}/collaborators/${GITHUB_USERNAME}/permission")" + if [ "$(echo "$auth" | jq -r .message)" != "null" ]; then + fatal "\$GITHUB_API_TOKEN can't push to https://github.com/${ORIGIN_SLUG}.git" fi } function setup_git_author() { - GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$COMMIT_ID")" - GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$COMMIT_ID")" - GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$COMMIT_ID")" - GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$COMMIT_ID")" + GIT_AUTHOR_NAME="$(git show -s --format="%aN" "${COMMIT_ID}")" + GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "${COMMIT_ID}")" + GIT_COMMITTER_NAME="$(git show -s --format="%cN" "${COMMIT_ID}")" + GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "${COMMIT_ID}")" export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL } function message() { - echo "Node: $COMMIT_MESSAGE" + echo "Node: ${COMMIT_MESSAGE}" } function pr_payload() { local escaped_message - escaped_message="$(echo "$COMMIT_MESSAGE" | sed -E -e "s/\"/\\\\\"/g")" - echo '{ - "title": "Node: '"$escaped_message"'", - "body": "Commit: nodejs/docker-node@'"$COMMIT_ID"'", - "head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'", - "base": "master" - }' + escaped_message="$(echo "${COMMIT_MESSAGE}" | sed -E -e "s/\"/\\\\\"/g")" + echo "{ + 'title': 'Node: ${escaped_message}', + 'body': 'Commit: nodejs/docker-node@${COMMIT_ID}', + 'head': '${GITHUB_USERNAME}:${BRANCH_NAME}', + 'base': 'master' + }" } function comment_payload() { local pr_url pr_url="$1" - echo '{ - "body": "Created PR to the '"$REPO_NAME"' repo ('"$pr_url"')" - }' + echo "{ + 'body': 'Created PR to the ${REPO_NAME} repo (${pr_url})' + }" } if updated; then @@ -112,19 +112,19 @@ if updated; then setup_git_author info "Cloning..." - git clone --depth 50 "https://github.com/$UPSTREAM_SLUG.git" $gitpath 2>/dev/null + git clone --depth 50 "https://github.com/${UPSTREAM_SLUG}.git" ${gitpath} 2>/dev/null stackbrew="$(./generate-stackbrew-library.sh)" cd $gitpath - echo "$stackbrew" >"$IMAGES_FILE" - git checkout -b "$BRANCH_NAME" - git add "$IMAGES_FILE" + echo "${stackbrew}" >"{$IMAGES_FILE}" + git checkout -b "${BRANCH_NAME}" + git add "${IMAGES_FILE}" git commit -m "$(message)" info "Pushing..." - git push "https://$GITHUB_API_TOKEN:x-oauth-basic@github.com/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2>/dev/null || fatal "Error pushing the updated stackbrew" + git push "https://${GITHUB_API_TOKEN}:x-oauth-basic@github.com/${ORIGIN_SLUG}.git" -f "${BRANCH_NAME}" 2>/dev/null || fatal "Error pushing the updated stackbrew" cd - && rm -rf $gitpath @@ -133,16 +133,16 @@ if updated; then -s \ -X POST \ -d "$(pr_payload)" \ - "https://api.github.com/repos/$UPSTREAM_SLUG/pulls")" + "https://api.github.com/repos/${UPSTREAM_SLUG}/pulls")" - url="$(echo "$pr_response_payload" | jq -r .html_url)" + url="$(echo "${pr_response_payload}" | jq -r .html_url)" if [ "$url" != "null" ]; then info "Pull request created at $url" - if [ ! -z "$PR_NUMBER" ]; then - comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/issues/$PR_NUMBER/comments" + if [ ! -z "${PR_NUMBER}" ]; then + comment_endpoint="https://api.github.com/repos/${DOCKER_SLUG}/issues/${PR_NUMBER}/comments" else - comment_endpoint="https://api.github.com/repos/$DOCKER_SLUG/commits/$COMMIT_ID/comments" + comment_endpoint="https://api.github.com/repos/${DOCKER_SLUG}/commits/${COMMIT_ID}/comments" fi info "Creating Commit Comment" @@ -152,15 +152,15 @@ if updated; then -d "$(comment_payload "$url")" \ "$comment_endpoint")" - if [ "$(echo "$commit_response_payload" | jq .message)" != "null" ]; then - fatal "Error linking the pull request ($error_message)" + if [ "$(echo "${commit_response_payload}" | jq -r .message)" != "null" ]; then + fatal "Error linking the pull request (${error_message})" else - comment_url="$(echo "$commit_response_payload" | jq -r .html_url)" - info "Created comment at $comment_url" + comment_url="$(echo "${commit_response_payload}" | jq -r .html_url)" + info "Created comment at ${comment_url}" fi else - error_message=$(echo "$pr_response_payload" | jq .message) - fatal "Error creating pull request ($error_message)" + error_message=$(echo "${pr_response_payload}" | jq -r .message) + fatal "Error creating pull request (${error_message})" fi else info "No change!" diff --git a/test-build.sh b/test-build.sh index 8af85b9a..65416568 100755 --- a/test-build.sh +++ b/test-build.sh @@ -25,23 +25,23 @@ function build() { tag="$1" shift - if [ -z "$variant" ]; then - full_tag="$tag" - path="$version/$variant" + if [ -z "${variant}" ]; then + full_tag="${tag}" + path="${version}/${variant}" else - full_tag="$tag-$variant" - path="$version/$variant" + full_tag="${tag}-${variant}" + path="${version}/${variant}" fi - info "Building $full_tag..." + info "Building ${full_tag}..." - if ! docker build --cpuset-cpus="0,1" -t node:"$full_tag" "$path"; then - fatal "Build of $full_tag failed!" + if ! docker build --cpuset-cpus="0,1" -t node:"${full_tag}" "${path}"; then + fatal "Build of ${full_tag} failed!" fi - info "Build of $full_tag succeeded." + info "Build of ${full_tag} succeeded." - info "Testing $full_tag" - docker run --rm -v "$PWD/test-image.sh:/usr/local/bin/test.sh" node:"$full_tag" test.sh "$full_version" + info "Testing ${full_tag}" + docker run --rm -v "$PWD/test-image.sh:/usr/local/bin/test.sh" node:"${full_tag}" test.sh "${full_version}" } cd "$(cd "${0%/*}" && pwd -P)" || exit @@ -53,25 +53,25 @@ fi for version in "${versions[@]}"; do # Skip "docs" and other non-docker directories - [ -f "$version/Dockerfile" ] || continue + [ -f "${version}/Dockerfile" ] || continue - tag=$(get_tag "$version") - full_version=$(get_full_version "$version") + tag=$(get_tag "${version}") + full_version=$(get_full_version "${version}") # Get supported variants according to the target architecture. # See details in function.sh - IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")" "${variant_arg[@]}")" + IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "${version}")" "${variant_arg[@]}")" # Only build the default Dockerfile if "default" is in the variant list if [[ "${variants[*]}" =~ "default" ]] || [[ "${variants[*]}" =~ "onbuild" ]]; then - build "$version" "" "$tag" + build "${version}" "" "${tag}" fi for variant in "${variants[@]}"; do # Skip non-docker directories - [ -f "$version/$variant/Dockerfile" ] || continue + [ -f "${version}/${variant}/Dockerfile" ] || continue - build "$version" "$variant" "$tag" + build "${version}" "${variant}" "${tag}" done done diff --git a/update.sh b/update.sh index f9f432d5..36f26209 100755 --- a/update.sh +++ b/update.sh @@ -29,7 +29,7 @@ function update_node_version() { local dockerfile=$1 shift local variant - if [[ $# -eq 1 ]]; then + if [ $# -eq 1 ]; then variant=$1 shift fi @@ -38,7 +38,7 @@ function update_node_version() { ( cp "$template" "$dockerfile" local fromprefix="" - if [[ "$arch" != "amd64" && "$variant" != "onbuild" ]]; then + if [ "$arch" != "amd64" ] && [ "$variant" != "onbuild" ]; then fromprefix="$arch\\/" fi @@ -59,10 +59,10 @@ function update_node_version() { sed -E -i.bak "/$pattern/d" "$dockerfile" && rm "$dockerfile".bak done - if [[ "${version/.*/}" -ge 10 ]]; then + if [ "${version/.*/}" -ge 10 ]; then sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.7/' "$dockerfile" rm "$dockerfile.bak" - elif [[ "${version/.*/}" -ge 8 || "$arch" == "ppc64le" || "$arch" == "s390x" || "$arch" == "arm64" || "$arch" == "arm32v7" ]]; then + elif [ "${version/.*/}" -ge 8 ] || [ "$arch" = "ppc64le" ] || [ "$arch" = "s390x" ] || [ "$arch" == "arm64" ] || [ "$arch" == "arm32v7" ]; then sed -E -i.bak 's/FROM (.*)alpine:3.4/FROM \1alpine:3.6/' "$dockerfile" rm "$dockerfile.bak" fi