forked from rickstaa/action-black
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·97 lines (90 loc) · 2.87 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
set -e # Increase bash strictness
set -o pipefail
pwd
ls
# If no arguments are given use current working directory
black_args=(".")
if [[ "$#" -eq 0 && "${INPUT_BLACK_ARGS}" != "" ]]; then
black_args+=(${INPUT_BLACK_ARGS})
elif [[ "$#" -ne 0 && "${INPUT_BLACK_ARGS}" != "" ]]; then
black_args+=($* ${INPUT_BLACK_ARGS})
elif [[ "$#" -ne 0 && "${INPUT_BLACK_ARGS}" == "" ]]; then
black_args+=($*)
else
# Default (if no args provided).
black_args+=("--check" "--diff")
fi
# Check if formatting was requested
regex='\s+(--diff|--check)\s?'
if [[ "${black_args[*]}" =~ $regex ]]; then
formatting="false"
black_print_str="Checking"
else
formatting="true"
black_print_str="Formatting"
fi
# Check if '-q' or `--quiet` flags are present
quiet="false"
black_args_tmp=()
for item in "${black_args[@]}"; do
if [[ "${item}" != "-q" && "${item}" != "--quiet" ]]; then
black_args_tmp+=("${item}")
else
# Remove `quiet` related flags
# NOTE: Prevents us from checking if files were formatted
if [[ "${formatting}" != 'true' ]]; then
black_args_tmp+=("${item}")
fi
quiet="true"
fi
done
black_exit_val="0"
echo "[action-black] ${black_print_str} python code using the black formatter..."
# shellcheck disable=SC2086
black_output="$(/venv/bin/black ${black_args_tmp[*]} 2>&1)" || black_exit_val="$?"
if [[ "${quiet}" != 'true' ]]; then
echo "${black_output}"
fi
# Check for black errors
if [[ "${formatting}" != "true" ]]; then
echo "is_formatted=false" >> $GITHUB_OUTPUT
if [[ "${black_exit_val}" -eq "0" ]]; then
black_error="false"
elif [[ "${black_exit_val}" -eq "1" ]]; then
black_error="true"
elif [[ "${black_exit_val}" -eq "123" ]]; then
black_error="true"
echo "[action-black] ERROR: Black found a syntax error when checking the" \
"files (error code: ${black_exit_val})."
else
echo "[action-black] ERROR: Something went wrong while trying to run the" \
"black formatter (error code: ${black_exit_val})."
exit 1
fi
else
# Check if black formatted files
regex='\s?[0-9]+\sfiles?\sreformatted(\.|,)\s?'
if [[ "${black_output[*]}" =~ $regex ]]; then
echo "is_formatted=true" >> $GITHUB_OUTPUT
else
echo "is_formatted=false" >> $GITHUB_OUTPUT
fi
# Check if error was encountered
if [[ "${black_exit_val}" -eq "0" ]]; then
black_error="false"
elif [[ "${black_exit_val}" -eq "123" ]]; then
black_error="true"
echo "[action-black] ERROR: Black found a syntax error when checking the" \
"files (error code: ${black_exit_val})."
else
echo "is_formatted=false" >> $GITHUB_OUTPUT
echo "[action-black] ERROR: Something went wrong while trying to run the" \
"black formatter (error code: ${black_exit_val})."
exit 1
fi
fi
# Throw error if an error occurred and fail_on_error is true
if [[ "${INPUT_FAIL_ON_ERROR,,}" = 'true' && "${black_error}" = 'true' ]]; then
exit 1
fi