Skip to content

Commit 1770aa3

Browse files
Check Bash version at startup
Now, `diktat` terminates if Bash version is less than 4.
1 parent 069fde1 commit 1770aa3

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

bin/diktat

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@ set -euo pipefail
1414
OLD_IFS="${IFS}"
1515
IFS=$'\n'
1616

17+
function error() {
18+
local message
19+
message="$*"
20+
21+
if [[ "${GITHUB_ACTIONS:=false}" == 'true' ]]
22+
then
23+
# Echoing to GitHub.
24+
echo "::error::${message}"
25+
elif [[ -t 1 ]]
26+
then
27+
# Echoing to a terminal.
28+
echo -e "\e[1m$(basename "$0"): \e[31merror:\e[0m ${message}" >&2
29+
else
30+
# Echoing to a pipe.
31+
echo "$(basename "$0"): error: ${message}" >&2
32+
fi
33+
}
34+
35+
# Exit codes.
36+
# The code of 1 is returned by ktlint in the event of failure.
37+
declare -ir ERROR_JAVA_NOT_FOUND=2
38+
declare -ir ERROR_DOWNLOAD_FAILURE=3
39+
declare -ir ERROR_UNSUPPORTED_FLAG=4
40+
declare -ir ERROR_DIKTAT_JAR_NOT_FOUND=5
41+
declare -ir ERROR_OPTION_REQUIRES_ARGUMENT=6
42+
declare -ir ERROR_NO_INPUT_FILES_FOUND=7
43+
declare -ir ERROR_INCOMPATIBLE_BASH_VERSION=8
44+
45+
if (( BASH_VERSINFO[0] < 4 ))
46+
then
47+
error "bash version ${BASH_VERSION} is too old, version 4+ is required"
48+
exit ${ERROR_INCOMPATIBLE_BASH_VERSION}
49+
fi
50+
1751
# Default flag values.
1852
declare -i COLOR=0
1953
declare -i DEBUG=0
@@ -57,15 +91,6 @@ declare -r OPTION_REPORTER_LONG='--reporter'
5791
declare -r OPTION_OUTPUT_SHORT='-o'
5892
declare -r OPTION_OUTPUT_LONG='--output'
5993

60-
# Exit codes.
61-
# The code of 1 is returned by ktlint in the event of failure.
62-
declare -ir ERROR_JAVA_NOT_FOUND=2
63-
declare -ir ERROR_DOWNLOAD_FAILURE=3
64-
declare -ir ERROR_UNSUPPORTED_FLAG=4
65-
declare -ir ERROR_DIKTAT_JAR_NOT_FOUND=5
66-
declare -ir ERROR_OPTION_REQUIRES_ARGUMENT=6
67-
declare -ir ERROR_NO_INPUT_FILES_FOUND=7
68-
6994
# Error messages.
7095
declare -r MESSAGE_UNSUPPORTED_FLAG='unsupported flag'
7196
declare -r MESSAGE_OPTION_REQUIRES_ARGUMENT='option requires an argument'
@@ -126,24 +151,6 @@ THE SOFTWARE.
126151
EOF
127152
}
128153

129-
function error() {
130-
local message
131-
message="$*"
132-
133-
if [[ "${GITHUB_ACTIONS:=false}" == 'true' ]]
134-
then
135-
# Echoing to GitHub.
136-
echo "::error::${message}"
137-
elif [[ -t 1 ]]
138-
then
139-
# Echoing to a terminal.
140-
echo -e "\e[1m$(basename "$0"): \e[31merror:\e[0m ${message}" >&2
141-
else
142-
# Echoing to a pipe.
143-
echo "$(basename "$0"): error: ${message}" >&2
144-
fi
145-
}
146-
147154
# Locates Java, preferring JAVA_HOME.
148155
#
149156
# The 1st variable expansion prevents the "unbound variable" error if JAVA_HOME

docs/diktat-cli.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ the `java-distribution` and `java-version` input parameters)
104104
| 7
105105
| No source files to check were found
106106
| **Yes**
107+
108+
| 8
109+
| Incompatible _Bash_ version
110+
| **Yes**
107111
|===

0 commit comments

Comments
 (0)