forked from huit/puppet-splunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit.puppet-lint
executable file
·88 lines (72 loc) · 2.36 KB
/
pre-commit.puppet-lint
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
#!/bin/bash
# Assumptions:
#
# Puppet >= 2.7 is installed on this machine
# puppet-lint is installed on this machine
# ERB is installed on this machine
# Adjust LINTFLAGS as appropriate
# Redirect output to stderr.
exec 1>&2
PUPPETLINT_FLAGS=${PUPPETLINT_FLAGS:-"--no-autoloader_layout-check --no-80chars-check --no-documentation-check --no-names_containing_dash-check"}
TMPFILE=$(mktemp /tmp/tmp.XXXXXXXXXX)
STATUS=0
# Register exit trap for removing temporary files
trap 'rm -rf $TMPFILE' EXIT INT HUP
# Check for Puppet binary
which puppet >/dev/null 2>&1 || exit 1
# Check for puppet-lint
which puppet-lint >/dev/null 2>&1 || exit 1
# Check for erb
which erb >/dev/null 2>&1 || exit 1
# Get correct git revision
if git rev-parse --quiet --verify HEAD > /dev/null
then
revision=HEAD
else
# Initial commit: diff against an empty tree object
revision=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
IFS="
"
# Get a list of files changed in this transaction
declare -a FILES
FILES=$(git diff --cached --name-only --diff-filter=ACM "${revision}")
for file in ${FILES[@]}
do
# Don't check empty files
if [[ $(git cat-file -s ":0:${file}") -eq 0 ]]; then
continue
fi
extension="${file##*.}"
git cat-file blob ":0:${file}" > $TMPFILE
if [[ $? -ne 0 ]]; then
echo "Unable to checkout ${file}"
STATUS=2
else
case $extension in
pp)
# Puppet syntax check
puppet parser validate $TMPFILE >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Puppet syntax error in ${file}. Run 'puppet parser validate ${file}'" >&2
STATUS=2
fi
# puppet-lint check
puppet-lint $PUPPETLINT_FLAGS --log-format "${file}:%{linenumber} %{KIND} - %{message}" $TMPFILE 2> /dev/null
if [[ $? -ne 0 ]] ; then
STATUS=2
fi
;;
erb)
# syntax check templates - this doesn't catch a lot of mistakes,
# but it should catch gross mistakes
erb -x -T - "${TMPFILE}" | ruby -c > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "ERB syntax error in ${file}" >&2
STATUS=2
fi
;;
esac
fi
done
exit $STATUS