From 1770aa32d889f04880a902ca9b1bb70aafb22101 Mon Sep 17 00:00:00 2001 From: Andrey Shcheglov Date: Thu, 10 Nov 2022 17:40:40 +0300 Subject: [PATCH] Check Bash version at startup Now, `diktat` terminates if Bash version is less than 4. --- bin/diktat | 61 ++++++++++++++++++++++++-------------------- docs/diktat-cli.adoc | 4 +++ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/bin/diktat b/bin/diktat index b18eda0..e80bc2c 100755 --- a/bin/diktat +++ b/bin/diktat @@ -14,6 +14,40 @@ set -euo pipefail OLD_IFS="${IFS}" IFS=$'\n' +function error() { + local message + message="$*" + + if [[ "${GITHUB_ACTIONS:=false}" == 'true' ]] + then + # Echoing to GitHub. + echo "::error::${message}" + elif [[ -t 1 ]] + then + # Echoing to a terminal. + echo -e "\e[1m$(basename "$0"): \e[31merror:\e[0m ${message}" >&2 + else + # Echoing to a pipe. + echo "$(basename "$0"): error: ${message}" >&2 + fi +} + +# Exit codes. +# The code of 1 is returned by ktlint in the event of failure. +declare -ir ERROR_JAVA_NOT_FOUND=2 +declare -ir ERROR_DOWNLOAD_FAILURE=3 +declare -ir ERROR_UNSUPPORTED_FLAG=4 +declare -ir ERROR_DIKTAT_JAR_NOT_FOUND=5 +declare -ir ERROR_OPTION_REQUIRES_ARGUMENT=6 +declare -ir ERROR_NO_INPUT_FILES_FOUND=7 +declare -ir ERROR_INCOMPATIBLE_BASH_VERSION=8 + +if (( BASH_VERSINFO[0] < 4 )) +then + error "bash version ${BASH_VERSION} is too old, version 4+ is required" + exit ${ERROR_INCOMPATIBLE_BASH_VERSION} +fi + # Default flag values. declare -i COLOR=0 declare -i DEBUG=0 @@ -57,15 +91,6 @@ declare -r OPTION_REPORTER_LONG='--reporter' declare -r OPTION_OUTPUT_SHORT='-o' declare -r OPTION_OUTPUT_LONG='--output' -# Exit codes. -# The code of 1 is returned by ktlint in the event of failure. -declare -ir ERROR_JAVA_NOT_FOUND=2 -declare -ir ERROR_DOWNLOAD_FAILURE=3 -declare -ir ERROR_UNSUPPORTED_FLAG=4 -declare -ir ERROR_DIKTAT_JAR_NOT_FOUND=5 -declare -ir ERROR_OPTION_REQUIRES_ARGUMENT=6 -declare -ir ERROR_NO_INPUT_FILES_FOUND=7 - # Error messages. declare -r MESSAGE_UNSUPPORTED_FLAG='unsupported flag' declare -r MESSAGE_OPTION_REQUIRES_ARGUMENT='option requires an argument' @@ -126,24 +151,6 @@ THE SOFTWARE. EOF } -function error() { - local message - message="$*" - - if [[ "${GITHUB_ACTIONS:=false}" == 'true' ]] - then - # Echoing to GitHub. - echo "::error::${message}" - elif [[ -t 1 ]] - then - # Echoing to a terminal. - echo -e "\e[1m$(basename "$0"): \e[31merror:\e[0m ${message}" >&2 - else - # Echoing to a pipe. - echo "$(basename "$0"): error: ${message}" >&2 - fi -} - # Locates Java, preferring JAVA_HOME. # # The 1st variable expansion prevents the "unbound variable" error if JAVA_HOME diff --git a/docs/diktat-cli.adoc b/docs/diktat-cli.adoc index 4a52ad2..8c5c4d7 100644 --- a/docs/diktat-cli.adoc +++ b/docs/diktat-cli.adoc @@ -104,4 +104,8 @@ the `java-distribution` and `java-version` input parameters) | 7 | No source files to check were found | **Yes** + +| 8 +| Incompatible _Bash_ version +| **Yes** |===