Skip to content

Commit

Permalink
Check Bash version at startup
Browse files Browse the repository at this point in the history
Now, `diktat` terminates if Bash version is less than 4.
  • Loading branch information
0x6675636b796f75676974687562 committed Nov 10, 2022
1 parent 069fde1 commit 1770aa3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
61 changes: 34 additions & 27 deletions bin/diktat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/diktat-cli.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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**
|===

0 comments on commit 1770aa3

Please sign in to comment.