-
Notifications
You must be signed in to change notification settings - Fork 3
/
test
executable file
·90 lines (68 loc) · 2.21 KB
/
test
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
#!/bin/bash
# BeEF/test
# test
# Find bash scripts then run shellcheck
# Adapted from jessfraz/dotfiles/bin/test.sh
# https://github.com/jessfraz/dotfiles/blob/master/test.sh
set -euo pipefail
# -e exit if any command returns non-zero status code
# -u prevent using undefined variables
# -o pipefail force pipelines to fail on first non-zero status code
### Define Colours ###
tput sgr0;
# reset colors
readonly RESET=$(tput sgr0)
readonly BOLD=$(tput bold)
readonly RED=$(tput setaf 1)
readonly GREEN=$(tput setaf 64)
readonly BLUE=$(tput setaf 4)
### END Colours ###
FAIL="${RED}FATAL${RESET}"
PASS="${GREEN}PASS${RESET}"
INFO="${BLUE}INFO${RESET}"
function check_shellcheck {
if ! [ -x "$(command -v shellcheck)" ]; then
echo -e "[${FAIL}] shellcheck not installed"
echo -e "[${INFO}] macOS: brew install shellcheck"
echo -e "[${INFO}] Linux: apt install shellcheck"
exit 1
fi
}
function lint_shell_files {
for file in $(find . -type f -not -iwholename '*.git*' \
-not -iwholename '*venv*' \
| sort -u); do
# Find all regular files in source directory
# ignore any files in .git and venv directories
if file "${file}" | grep --quiet "shell" || file "${file}" | grep --quiet "bash" ; then
# Find bash scripts
# Running file on a script with the shebang "#!/usr/bin/env bash" returns
# "a /usr/bin/env bash script, ASCII text executable"
# Versus a script with the shebang "#!/bin/bash" which returns
# "Bourne-Again shell script, ASCII text executable"
if shellcheck "${file}" ; then
# Run shellcheck on the file
echo -e "[${PASS}] Sucessfully linted ${file}"
else
echo -e "[${FAIL}] Failed to lint ${file}"
ERRORS+=("${file}")
# If shellcheck fails add failing file name to array
fi
fi
done
}
function main {
ERRORS=()
check_shellcheck
lint_shell_files
if [ ${#ERRORS[@]} -eq 0 ]; then
# If ERRORS empty then
echo -e "[${PASS}] No errors, hooray"
exit 0
else
# If ERRORS not empty, print the names of files which failed
echo -e "[${FAIL}] These files failed linting: ${ERRORS[*]}"
exit 1
fi
}
main "$@"