Skip to content

Commit 09881cc

Browse files
authored
apply some shellcheck warnings and add no_yaml_in_data check (#1)
* apply some shellcheck warnings and add no_yaml_in_data check * Update trailing_whitespaces.sh * Update libs.sh * Create no_yaml_in_data.sh
1 parent c20e77f commit 09881cc

File tree

4 files changed

+71
-38
lines changed

4 files changed

+71
-38
lines changed

modules/libs.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# declare variables
44
_columwidth='60'
55
_rc=0
6-
_files=
76

87
# declare bash colors
98
declare -A _colors
@@ -15,6 +14,13 @@ _colors[yellow]='[0;33m'
1514
_colors[default]='[0m'
1615
_colors[restore]='[0m'
1716

17+
# manage tmp file
18+
tmp_file="$(mktemp)"
19+
trap 'rm -f ${tmp_file}' EXIT
20+
21+
# errorhandling
22+
set -euo pipefail
23+
1824
# get files, that are staged for commit
1925
_puppet_git_hooks_git_init () {
2026
# change pwd to toplevel, to match dot files
@@ -29,12 +35,14 @@ _puppet_git_hooks_git_init () {
2935
fi
3036
# Fill list of files to test and check if there are
3137
# unstaged changes in any of these files. See also git-diff(1)
32-
_files=$(git diff --staged --name-only --diff-filter=ACM "${revision}" | tr '\n' ' ') # prints staged files
38+
git diff --staged --name-only --diff-filter=ACM "${revision}" > "$tmp_file" # prints staged files
3339
for i in $(git diff --name-only --diff-filter=ACM); do # prints unstaged files
34-
if grep -q "${i}" <<< "${_files}"; then
35-
printf "\n\e${_colors[red]}The file '${i}' has unstaged changes. Abort.\e${_colors[restore]}\n\n"
36-
exit 1
37-
fi
40+
while read -r line; do
41+
if grep -q "${i}" <<< "${line}"; then
42+
echo -e "\n\e${_colors[red]}The file '${i}' has unstaged changes. Abort.\e${_colors[restore]}\n\n"
43+
exit 1
44+
fi
45+
done < "$tmp_file"
3846
done
3947
}
4048

@@ -69,19 +77,21 @@ _puppet_git_hooks_check () {
6977
shift
7078
local checkcommand=$1
7179
shift
72-
local allfiles=$@
7380
local filteredfiles=
7481

75-
if filteredfiles=$(echo $allfiles | tr ' ' '\n' | grep -E "$filenameregex"); then
82+
if filteredfiles=$(grep -E "$filenameregex" "$tmp_file"); then
7683
_puppet_git_hooks_say "checking" "${checkcommand}"
7784

78-
local _base_command=$(awk '{print $1;}' <<< $checkcommand)
79-
if type $_base_command > /dev/null 2>&1; then
85+
local _base_command=
86+
_base_command="$(awk '{print $1;}' <<< "$checkcommand")"
87+
if type "$_base_command" > /dev/null 2>&1; then
88+
# shellcheck disable=SC2086
8089
if ${checkcommand} ${filteredfiles} > /dev/null 2>&1; then
8190
_puppet_git_hooks_say "OK" "${checkcommand}"
8291
else
8392
_puppet_git_hooks_say "nOK" "${checkcommand}"
84-
${checkcommand} ${filteredfiles}
93+
# shellcheck disable=SC2086
94+
${checkcommand} ${filteredfiles} || true
8595
fi
8696
else
8797
_puppet_git_hooks_say "FAILED" "${checkcommand}"

modules/no_yaml_in_data.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2154
3+
4+
_puppet_git_hooks_check_yaml_in_data () {
5+
local say_checkname='.yaml file extension beneath data/'
6+
7+
if grep -qE '^data' "$tmp_file"; then
8+
_puppet_git_hooks_say "checking" "$say_checkname"
9+
if grep -E '^data' "$tmp_file" | grep -qv '.yaml$'; then
10+
_puppet_git_hooks_say "nOK" "$say_checkname"
11+
echo "Found files without .yaml in data/:"
12+
grep -E '^data' "$tmp_file" | grep -v '.yaml$'
13+
else
14+
_puppet_git_hooks_say "OK" "$say_checkname"
15+
fi
16+
fi
17+
}

modules/trailing_whitespaces.sh

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
#!/bin/bash
2+
# shellcheck disable=SC2154
23

34
_puppet_git_hooks_check_trailing_whitespaces () {
4-
local filteredfiles=
55
local say_checkname='trailing whitespaces'
66

7-
if filteredfiles=$(echo $@ | tr ' ' '\n' | grep -vE '\.md$|^files\/'); then
8-
_puppet_git_hooks_say "checking" "$say_checkname"
9-
if type grep > /dev/null 2>&1; then
10-
if ! grep -qIE ' $' ${filteredfiles}; then
11-
_puppet_git_hooks_say "OK" "$say_checkname"
12-
else
13-
_puppet_git_hooks_say "nOK" "$say_checkname"
14-
echo "Found trailing whitespaces in:"
15-
grep --color=auto -HnoE ' $' ${filteredfiles}
7+
if grep -qvE '\.md$|^files\/' "$tmp_file"; then
8+
_puppet_git_hooks_say 'checking' "$say_checkname"
9+
10+
files_with_trailing_whitespaces=()
11+
while read -r line; do
12+
if grep -qIE ' $' <<< "$line"; then
13+
files_with_trailing_whitespaces+=("$line")
1614
fi
15+
done < "$tmp_file"
16+
17+
if [[ "${#files_with_trailing_whitespaces[@]}" -eq 0 ]]; then
18+
_puppet_git_hooks_say 'OK' "$say_checkname"
1719
else
18-
_puppet_git_hooks_say "FAILED" "$say_checkname"
19-
echo "command not found: grep"
20+
_puppet_git_hooks_say 'nOK' "$say_checkname"
21+
echo "Found trailing whitespaces in:"
22+
echo "${files_with_trailing_whitespaces[@]}"
2023
fi
2124
fi
2225
}

pre-commit

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
#!/bin/bash
2+
# shellcheck disable=SC2154
3+
# shellcheck disable=SC1090
24

35
# get functions and checks
4-
if [ -L $0 ]; then # $0 is symlink
5-
for i in $(find $(dirname $(readlink $0))/modules/ -type f -name '*.sh' ); do
6-
source $i
7-
done
6+
if [ -L "$0" ]; then # $0 is symlink
7+
while read -r line; do
8+
source "$line"
9+
done < <(find "$(dirname "$(readlink "$0")")/modules/" -type f -name '*.sh')
810
else
9-
for i in $(find $(dirname $0)/modules/ -type f -name '*.sh' ); do
10-
source $i
11-
done
11+
while read -r line; do
12+
source "$line"
13+
done < <(find "$(dirname "$0")/modules/" -type f -name '*.sh')
1214
fi
1315

1416
# collect changed files
1517
_puppet_git_hooks_git_init
1618

1719
# do checks
1820
#_puppet_git_hooks_check '\.erb$' 'bash -c "erb -x -T - $@ | ruby -c"' ${_files} # not working
19-
_puppet_git_hooks_check '\.pp$' 'puppet-lint --relative' ${_files}
20-
_puppet_git_hooks_check '\.pp$' '/opt/puppetlabs/bin/puppet parser validate' ${_files}
21-
_puppet_git_hooks_check '\.epp$' '/opt/puppetlabs/bin/puppet epp validate' ${_files}
22-
_puppet_git_hooks_check '\.yaml$' 'yamllint --strict' ${_files}
23-
_puppet_git_hooks_check '^Puppetfile$' 'r10k puppetfile check' ${_files}
24-
_puppet_git_hooks_check_trailing_whitespaces ${_files}
21+
_puppet_git_hooks_check_yaml_in_data 'data'
22+
_puppet_git_hooks_check '\.pp$' 'puppet-lint --relative'
23+
_puppet_git_hooks_check '\.pp$' '/opt/puppetlabs/bin/puppet parser validate'
24+
_puppet_git_hooks_check '\.epp$' '/opt/puppetlabs/bin/puppet epp validate'
25+
_puppet_git_hooks_check '\.yaml$' 'yamllint --strict'
26+
_puppet_git_hooks_check '^Puppetfile$' 'r10k puppetfile check'
27+
_puppet_git_hooks_check_trailing_whitespaces
2528

2629
# exit
27-
if [ $_rc -eq 0 ]; then
30+
if [ "$_rc" -eq '0' ]; then
2831
echo
2932
exit 0
3033
else
3134
echo
3235
echo
3336
echo 'git commit aborted.'
34-
exit $_rc
37+
exit "$_rc"
3538
fi

0 commit comments

Comments
 (0)