From 2c1ac4e60418acf8fa556ccd2890773430104bae Mon Sep 17 00:00:00 2001 From: Nils Plaschke Date: Wed, 26 Jan 2022 14:57:12 +0100 Subject: [PATCH] fix: shellcheck errors (#263) Co-authored-by: Michael Kriese --- src/usr/local/bin/install-buildpack | 2 +- src/usr/local/buildpack/tools/dotnet.sh | 4 ++-- src/usr/local/buildpack/tools/git.sh | 5 +++-- src/usr/local/buildpack/tools/java-jre.sh | 1 + src/usr/local/buildpack/tools/java.sh | 1 + src/usr/local/buildpack/tools/node.sh | 8 ++++++-- src/usr/local/buildpack/tools/php.sh | 7 ++++--- src/usr/local/buildpack/tools/powershell.sh | 4 ++-- src/usr/local/buildpack/tools/python.sh | 11 +++++++---- src/usr/local/buildpack/tools/ruby.sh | 7 ++++--- src/usr/local/buildpack/tools/swift.sh | 7 +++++-- src/usr/local/buildpack/util.sh | 6 ++++++ 12 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/usr/local/bin/install-buildpack b/src/usr/local/bin/install-buildpack index c3a3510e5..8e2221550 100755 --- a/src/usr/local/bin/install-buildpack +++ b/src/usr/local/bin/install-buildpack @@ -104,7 +104,7 @@ apt_install \ # check for /usr/local/share/ca-certificates/*.crt -if [[ "$(find /usr/local/share/ca-certificates/ -name *.crt -type f -printf '.' | wc -c)" -gt 0 ]]; then +if [[ "$(find /usr/local/share/ca-certificates/ -name "*.crt" -type f -printf '.' | wc -c)" -gt 0 ]]; then echo "Found user certs, calling update-ca-certificates" update-ca-certificates fi diff --git a/src/usr/local/buildpack/tools/dotnet.sh b/src/usr/local/buildpack/tools/dotnet.sh index c2fdacb71..85205465b 100644 --- a/src/usr/local/buildpack/tools/dotnet.sh +++ b/src/usr/local/buildpack/tools/dotnet.sh @@ -17,9 +17,9 @@ if [[ -d "${DOTNET_INSTALL_DIR}/sdk/${TOOL_VERSION}" ]]; then exit 0 fi -VERSION_CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") +version_codename=$(get_distro) -case "$VERSION_CODENAME" in +case "$version_codename" in "bionic") apt_install libc6 libgcc1 libgssapi-krb5-2 libicu60 libssl1.1 libstdc++6 zlib1g;; "focal") apt_install libc6 libgcc1 libgssapi-krb5-2 libicu66 libssl1.1 libstdc++6 zlib1g;; esac diff --git a/src/usr/local/buildpack/tools/git.sh b/src/usr/local/buildpack/tools/git.sh index fb7c529bb..8e40bc995 100644 --- a/src/usr/local/buildpack/tools/git.sh +++ b/src/usr/local/buildpack/tools/git.sh @@ -3,9 +3,10 @@ set -e require_root -VERSION_CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") -echo "deb http://ppa.launchpad.net/git-core/ppa/ubuntu ${VERSION_CODENAME} main" | tee /etc/apt/sources.list.d/git.list +version_codename=$(get_distro) + +echo "deb http://ppa.launchpad.net/git-core/ppa/ubuntu ${version_codename} main" | tee /etc/apt/sources.list.d/git.list curl -sSL \ 'http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xE1DD270288B4E6030699E45FA1715D88E1DF1F24' \ | apt-key add - diff --git a/src/usr/local/buildpack/tools/java-jre.sh b/src/usr/local/buildpack/tools/java-jre.sh index 963694bd6..ed10f9a87 100644 --- a/src/usr/local/buildpack/tools/java-jre.sh +++ b/src/usr/local/buildpack/tools/java-jre.sh @@ -4,6 +4,7 @@ set -e check_semver "$TOOL_VERSION" +# shellcheck disable=SC2153 if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then echo Invalid version: "${TOOL_VERSION}" exit 1 diff --git a/src/usr/local/buildpack/tools/java.sh b/src/usr/local/buildpack/tools/java.sh index ec6bc50e4..464283d6f 100644 --- a/src/usr/local/buildpack/tools/java.sh +++ b/src/usr/local/buildpack/tools/java.sh @@ -4,6 +4,7 @@ set -e check_semver "$TOOL_VERSION" +# shellcheck disable=SC2153 if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then echo Invalid version: "${TOOL_VERSION}" exit 1 diff --git a/src/usr/local/buildpack/tools/node.sh b/src/usr/local/buildpack/tools/node.sh index 50e46d488..ec46cf5d0 100644 --- a/src/usr/local/buildpack/tools/node.sh +++ b/src/usr/local/buildpack/tools/node.sh @@ -16,6 +16,8 @@ PREFIX="${USER_HOME}/.npm-global" function update_env () { + local tool_env + reset_tool_env link_wrapper "${TOOL_NAME}" "$tool_path/bin" @@ -24,7 +26,9 @@ function update_env () { export_tool_path "${PREFIX}/bin" - cat >> $(find_tool_env) <<- EOM + tool_env=$(find_tool_env) + + cat >> "$tool_env" <<- EOM # openshift override unknown user home if [ "\${EUID}" != 0 ] && [ "\${EUID}" != ${USER_ID} ]; then export NPM_CONFIG_PREFIX="${PREFIX}" @@ -47,7 +51,7 @@ function prepare_global_config () { function prepare_user_config () { local prefix=${1} - if [[ -r "${USER_HOME}/.npmrc" && $(cat "${USER_HOME}/.npmrc" | grep 'prefix') ]]; then + if grep 'prefix' "${USER_HOME}/.npmrc"; then return fi diff --git a/src/usr/local/buildpack/tools/php.sh b/src/usr/local/buildpack/tools/php.sh index c9d4f985e..38d41e5d0 100644 --- a/src/usr/local/buildpack/tools/php.sh +++ b/src/usr/local/buildpack/tools/php.sh @@ -22,12 +22,13 @@ if [[ -z "${tool_path}" ]]; then file=/tmp/php.tar.xz ARCH=$(uname -p) - CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") BASE_URL="https://github.com/containerbase/php-prebuild/releases/download" + version_codename=$(get_distro) + # TODO: extract to separate preparation tool require_root - case "$CODENAME" in + case "$version_codename" in "bionic") apt_install \ libjpeg-turbo8 \ libmcrypt4 \ @@ -48,7 +49,7 @@ if [[ -z "${tool_path}" ]]; then ;; esac - curl -sSfLo ${file} "${BASE_URL}/${TOOL_VERSION}/php-${TOOL_VERSION}-${CODENAME}-${ARCH}.tar.xz" + curl -sSfLo ${file} "${BASE_URL}/${TOOL_VERSION}/php-${TOOL_VERSION}-${version_codename}-${ARCH}.tar.xz" if [[ -f ${file} ]]; then echo 'Using prebuild php' diff --git a/src/usr/local/buildpack/tools/powershell.sh b/src/usr/local/buildpack/tools/powershell.sh index 4a10af185..e47f79573 100644 --- a/src/usr/local/buildpack/tools/powershell.sh +++ b/src/usr/local/buildpack/tools/powershell.sh @@ -18,9 +18,9 @@ if [[ -z "${tool_path}" ]]; then base_path=${INSTALL_DIR}/${TOOL_NAME} tool_path=${base_path}/${TOOL_VERSION} - VERSION_CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") + version_codename=$(get_distro) - case "$VERSION_CODENAME" in + case "$version_codename" in "bionic") apt_install libc6 libgcc1 libgssapi-krb5-2 libicu60 libssl1.1 libstdc++6 zlib1g;; "focal") apt_install libc6 libgcc1 libgssapi-krb5-2 libicu66 libssl1.1 libstdc++6 zlib1g;; esac diff --git a/src/usr/local/buildpack/tools/python.sh b/src/usr/local/buildpack/tools/python.sh index 274b67aef..5ccdd3d6c 100644 --- a/src/usr/local/buildpack/tools/python.sh +++ b/src/usr/local/buildpack/tools/python.sh @@ -22,10 +22,11 @@ if [[ -z "${tool_path}" ]]; then file=/tmp/python.tar.xz ARCH=$(uname -p) - CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") PYTHON_URL="https://github.com/containerbase/python-prebuild/releases/download" - curl -sSfLo ${file} "${PYTHON_URL}/${TOOL_VERSION}/python-${TOOL_VERSION}-${CODENAME}-${ARCH}.tar.xz" || echo 'Ignore download error' + version_codename=$(get_distro) + + curl -sSfLo ${file} "${PYTHON_URL}/${TOOL_VERSION}/python-${TOOL_VERSION}-${version_codename}-${ARCH}.tar.xz" || echo 'Ignore download error' if [[ -f ${file} ]]; then echo 'Using prebuild python' @@ -56,7 +57,9 @@ if [[ -z "${tool_path}" ]]; then fi fix_python_shebangs() { - for file in $(find "${tool_path}/bin" -type f -exec grep -Iq . {} \; -print); do + # https://github.com/koalaman/shellcheck/wiki/SC2044 + while IFS= read -r -d '' file + do case "$(head -1 "${file}")" in "#!"*"/bin/python" ) sed -i "1 s:.*:#\!${tool_path}\/bin\/python:" "${file}" @@ -68,7 +71,7 @@ if [[ -z "${tool_path}" ]]; then sed -i "1 s:.*:#\!${tool_path}\/bin\/python${MAJOR}.${MINOR}:" "${file}" ;; esac - done + done < <(find "${tool_path}/bin" -type f -exec grep -Iq . {} \; -print0) } fix_python_shebangs diff --git a/src/usr/local/buildpack/tools/ruby.sh b/src/usr/local/buildpack/tools/ruby.sh index bc8a288eb..1fe317b6b 100644 --- a/src/usr/local/buildpack/tools/ruby.sh +++ b/src/usr/local/buildpack/tools/ruby.sh @@ -19,13 +19,14 @@ if [[ ! -d "$tool_path" ]]; then mkdir -p "$base_path" ARCH=$(uname -p) - CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") RUBY_URL="https://github.com/containerbase/ruby-prebuild/releases/download" - curl -sSfLo ruby.tar.xz "${RUBY_URL}/${TOOL_VERSION}/ruby-${TOOL_VERSION}-${CODENAME}-${ARCH}.tar.xz" || echo 'Ignore download error' + version_codename=$(get_distro) + + curl -sSfLo ruby.tar.xz "${RUBY_URL}/${TOOL_VERSION}/ruby-${TOOL_VERSION}-${version_codename}-${ARCH}.tar.xz" || echo 'Ignore download error' if [[ -f ruby.tar.xz ]]; then - echo "Using prebuild ruby for ${CODENAME}" + echo "Using prebuild ruby for ${version_codename}" apt_install \ build-essential \ libffi-dev \ diff --git a/src/usr/local/buildpack/tools/swift.sh b/src/usr/local/buildpack/tools/swift.sh index 578d1bd77..927bf5a82 100644 --- a/src/usr/local/buildpack/tools/swift.sh +++ b/src/usr/local/buildpack/tools/swift.sh @@ -14,13 +14,16 @@ base_path=/usr/local/buildpack/${TOOL_NAME} tool_path=$base_path/$TOOL_VERSION if [[ ! -d "${tool_path}" ]]; then - VERSION_CODENAME=$(. /etc/os-release && echo "${VERSION_CODENAME}") + + version_codename=$(get_distro) + + # shellcheck source=/dev/null VERSION_ID=$(. /etc/os-release && echo "${VERSION_ID}") # https://swift.org/getting-started/#on-linux # already installed: git - case "$VERSION_CODENAME" in + case "${version_codename}" in "bionic") apt_install \ binutils \ diff --git a/src/usr/local/buildpack/util.sh b/src/usr/local/buildpack/util.sh index c4cca7610..555c2424f 100644 --- a/src/usr/local/buildpack/util.sh +++ b/src/usr/local/buildpack/util.sh @@ -190,6 +190,12 @@ function require_distro () { esac } +function get_distro() { + require_distro + # shellcheck source=/dev/null disable=SC2005 + echo "$(. /etc/os-release && echo "${VERSION_CODENAME}")" +} + function require_root () { if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" >&2