Skip to content

Commit 4334377

Browse files
author
David Wahlstrom
committed
Add tput color and puppetlint.rc support
This merge provides terminal color control with tput (which makes it infinitely easier to read) and allows for users to provide a .puppetlint.rc file to configure puppet-lint to their liking. commit be634db Merge: 4611611 5c21034 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 23:42:51 2014 -0700 Merge branch 'feature/smart-colors' commit 5c21034 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 23:29:32 2014 -0700 Replaced remaining escape sequences with tput commit 4611611 Merge: c7f46d5 a89de65 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 22:23:20 2014 -0700 Merge branch 'feature/smart-colors' commit c7f46d5 Merge: 0934391 95ea4b7 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 22:23:15 2014 -0700 Merge branch 'feature/puppet-lintrc' commit 0934391 Merge: 9c90b18 3379aee Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 22:23:04 2014 -0700 Merge branch 'feature/symlink-pre-receive' commit a89de65 Merge: 06facd8 3379aee Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 22:22:38 2014 -0700 Merge branch 'feature/symlink-pre-receive' into feature/smart-colors commit 95ea4b7 Merge: 621ccdf 3379aee Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 22:22:24 2014 -0700 Merge branch 'feature/symlink-pre-receive' into feature/puppet-lintrc commit 3379aee Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 21:31:22 2014 -0700 Removed dependency on realpath command commit 06facd8 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 20:34:21 2014 -0700 Preventing sed from getting an empty expression when the module directory argument is not passed commit 9c90b18 Merge: 6cf9441 621ccdf Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 20:05:13 2014 -0700 Merged puppet-lintrc feature with smart-colors feature commit 621ccdf Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 20:01:04 2014 -0700 Added a check for a .puppet-lint.rc file in the module root. The custom config will be applied with the --config flag if the file exists. This reproduces the behavior you would see when running the puppet-lint command within the module directory without the --config option. commit 6cf9441 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 19:17:07 2014 -0700 Added tput-based coloring to pre-receive, puppet_lint_checks.sh and puppet_manifest_syntaxt_check.sh. Fixed the STDERR output redirection for puppet_lint_checks.sh and puppet_manifest_syntaxt_check.sh. commit e92c665 Author: Henry Goodman <github@henrygoodman.com> Date: Tue Oct 21 17:16:22 2014 -0700 Allow the pre-receive hook to be used as a relative symlink
1 parent 4402582 commit 4334377

File tree

8 files changed

+81
-52
lines changed

8 files changed

+81
-52
lines changed

commit_hooks/erb_template_syntax_check.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ else
1515
fi
1616

1717
# Check ERB template syntax
18-
echo -e "\x1B[0;36mChecking erb template syntax for $module_path...\x1B[0m"
19-
cat $1 | erb -x -T - | ruby -c 2>&1 > $error_msg
18+
echo -e "$(tput setaf 6)Checking erb template syntax for $module_path...$(tput sgr0)"
19+
cat $1 | erb -x -T - | ruby -c > $error_msg 2>&1
2020
if [ $? -ne 0 ]; then
21-
echo -en "\x1B[0;31m"
22-
cat $error_msg
21+
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
2322
syntax_errors=`expr $syntax_errors + 1`
24-
echo -e "Error: erb syntax error in $module_path (see above)\x1B[0m"
23+
echo -e "$(tput setaf 1)Error: erb syntax error in $module_path (see above)$(tput sgr0)"
2524
fi
2625
rm $error_msg
2726

2827
if [ "$syntax_errors" -ne 0 ]; then
29-
echo -e "\x1B[0;31mError: $syntax_errors syntax errors found in templates. Commit will be aborted.\x1B[0m"
28+
echo -e "$(tput setaf 1)Error: $syntax_errors syntax errors found in templates. Commit will be aborted.$(tput sgr0)"
3029
exit 1
3130
fi
3231

commit_hooks/json_syntax_check.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ else
1212
module_path=$1
1313
fi
1414

15-
# Get list of new/modified manifest and template files to check (in git index)
1615
# Check json file syntax
17-
echo -e "\x1B[0;36mChecking json syntax for $module_path...\x1B[0m"
16+
echo -e "$(tput setaf 6)Checking json syntax for $module_path...$(tput sgr0)"
1817
ruby -e "require 'json'; JSON.parse(File.read($1))" 2> $error_msg > /dev/null
1918
if [ $? -ne 0 ]; then
20-
echo -en "\x1B[0;31m"
21-
cat $error_msg
19+
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
2220
syntax_errors=`expr $syntax_errors + 1`
23-
echo -e "Error: json syntax error in $module_path (see above)\x1B[0m"
21+
echo -e "$(tput setaf 1)Error: json syntax error in $module_path (see above)$(tput sgr0)"
2422
fi
2523
rm -f $error_msg
2624

2725
if [ "$syntax_errors" -ne 0 ]; then
28-
echo -e "\x1B[0;31mError: $syntax_errors syntax error(s) found in json file. Commit will be aborted.\x1B[0m"
26+
echo -e "$(tput setaf 1)Error: $syntax_errors syntax error(s) found in json file. Commit will be aborted.$(tput sgr0)"
2927
exit 1
3028
fi
3129

commit_hooks/puppet_lint_checks.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,47 @@
33
# This script expects $1 to be passed and for $1 to be the filesystem location
44
# to a puppet manifest file for which it will run style guide checks against.
55

6+
manifest_path="$1"
7+
module_dir="$2"
8+
69
syntax_errors=0
710
error_msg=$(mktemp /tmp/error_msg_puppet-lint.XXXXX)
811

9-
if [ $2 ]; then
10-
module_path=$(echo $1 | sed -e 's|'$2'||')
12+
if [ $module_dir ]; then
13+
manifest_name=$(echo $manifest_path | sed -e 's|'$module_dir'||')
14+
error_msg_filter="sed -e s|$module_dir||"
1115
else
12-
module_path=$1
16+
manifest_name="$manifest_path"
17+
error_msg_filter="sed"
1318
fi
1419

1520
# De-lint puppet manifests
16-
echo -e "\x1B[0;36mChecking puppet style guide compliance for $module_path...\x1B[0m"
17-
puppet-lint --fail-on-warnings --with-filename --no-80chars-check $1 2>&1 > $error_msg
21+
echo -e "$(tput setaf 6)Checking puppet style guide compliance for $manifest_name...$(tput sgr0)"
22+
23+
# If a file named .puppet-lint.rc exists at the base of the repo then use it to
24+
# enable or disable checks.
25+
puppet_lint_cmd="puppet-lint --fail-on-warnings --with-filename"
26+
puppet_lint_rcfile="${2}.puppet-lint.rc"
27+
if [ -f $puppet_lint_rcfile ]; then
28+
echo -e "$(tput setaf 6)Applying custom config from .puppet-lint.rc$(tput sgr0)"
29+
puppet_lint_cmd="$puppet_lint_cmd --config $puppet_lint_rcfile"
30+
else
31+
puppet_lint_cmd="$puppet_lint_cmd --no-80chars-check"
32+
fi
33+
34+
$puppet_lint_cmd $1 2>&1 > $error_msg
1835
RC=$?
1936
if [ $RC -ne 0 ]; then
20-
echo -en "\x1B[0;31m"
2137
syntax_errors=$(expr $syntax_errors + 1)
22-
cat $error_msg
23-
echo -e "Error: styleguide violation in $module_path (see above)\x1B[0m"
38+
cat $error_msg | $error_msg_filter -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
39+
echo -e "$(tput setaf 1)Error: styleguide violation in $manifest_name (see above)$(tput sgr0)"
2440
fi
2541
rm -f $error_msg
2642

2743
if [ $syntax_errors -ne 0 ]; then
28-
echo -e "\x1B[0;31mError: $syntax_errors styleguide violation(s) found. Commit will be aborted.
44+
echo -e "Error: $syntax_errors styleguide violation(s) found. Commit will be aborted.
2945
Please follow the puppet style guide outlined at:
30-
http://docs.puppetlabs.com/guides/style_guide.html\x1B[0m"
46+
http://docs.puppetlabs.com/guides/style_guide.html" | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
3147
exit 1
3248
fi
3349

commit_hooks/puppet_manifest_syntax_check.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,33 @@
33
# This script expects $1 to be passed and for $1 to be the filesystem location
44
# to a puppet manifest file for which it will run syntax checks against.
55

6+
manifest_path="$1"
7+
module_dir="$2"
8+
69
syntax_errors=0
710
error_msg=$(mktemp /tmp/error_msg_puppet-syntax.XXXXX)
811

9-
if [ $2 ]; then
10-
module_path=$(echo $1 | sed -e 's|'$2'||')
12+
if [ $module_dir ]; then
13+
manifest_name=$(echo $manifest_path | sed -e 's|'$module_dir'||')
14+
error_msg_filter="sed -e s|$module_dir||"
1115
else
12-
module_path=$1
16+
manifest_name="$manifest_path"
17+
error_msg_filter="sed"
1318
fi
1419

1520
# Get list of new/modified manifest and template files to check (in git index)
1621
# Check puppet manifest syntax
17-
echo -e "\x1B[0;36mChecking puppet manifest syntax for $module_path...\x1B[0m"
18-
puppet parser validate --color=false $1 2>&1 > $error_msg
22+
echo -e "$(tput setaf 6)Checking puppet manifest syntax for $manifest_name...$(tput sgr0)"
23+
puppet parser validate --color=false $1 > $error_msg 2>&1
1924
if [ $? -ne 0 ]; then
20-
echo -en "\x1B[0;31m"
2125
syntax_errors=`expr $syntax_errors + 1`
22-
cat $error_msg
23-
echo -e "Error: puppet syntax error in $module_path (see above)\x1B[0m"
26+
cat $error_msg | $error_msg_filter -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
27+
echo -e "$(tput setaf 1)Error: puppet syntax error in $manifest_name (see above)$(tput sgr0)"
2428
fi
2529
rm -f $error_msg
2630

2731
if [ "$syntax_errors" -ne 0 ]; then
28-
echo -e "\x1B[0;31mError: $syntax_errors syntax error(s) found in puppet manifests. Commit will be aborted.\x1B[0m"
32+
echo -e "$(tput setaf 1)Error: $syntax_errors syntax error(s) found in puppet manifests. Commit will be aborted.$(tput sgr0)"
2933
exit 1
3034
fi
3135

commit_hooks/rspec_puppet_checks.sh

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ changedmodules=$(echo -e "$tmpchangedmodules" | sort -u)
2828
for module_dir in $changedmodules; do
2929
#only run rspec if the "spec" directory exists
3030
if [ -d "${module_dir}/spec" ]; then
31-
echo -e "\x1B[0;36mRunning rspec-puppet tests for module $module_path...\x1B[0m"
32-
cd $module_dir
33-
#this will run rspec for every test in the module
34-
rspec > $error_msg
35-
RC=$?
36-
if [ $RC -ne 0 ]; then
37-
echo -en "\x1B[0;31m"
38-
cat $error_msg
39-
echo -e "Error: rspec-puppet test(s) failed for $module_dir (see above)\x1B[0m"
40-
syntax_errors=`expr $syntax_errors + 1`
41-
fi
31+
echo -e "$(tput setaf 6)Running rspec-puppet tests for module $module_path...$(tput sgr0)"
32+
cd $module_dir
33+
#this will run rspec for every test in the module
34+
rspec > $error_msg
35+
RC=$?
36+
if [ $RC -ne 0 ]; then
37+
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
38+
echo -e "$(tput setaf 1)Error: rspec-puppet test(s) failed for $module_dir (see above)$(tput sgr0)"
39+
syntax_errors=`expr $syntax_errors + 1`
40+
fi
4241
fi
4342
done
4443

@@ -47,7 +46,7 @@ cd $oldpwd > /dev/null
4746
rm $error_msg
4847

4948
if [ "$syntax_errors" -ne 0 ]; then
50-
echo -e "\x1B[0;31mError: $syntax_errors rspec-puppet test(s) failed. Commit will be aborted.\x1B[0m"
49+
echo -e "$(tput setaf 1)Error: $syntax_errors rspec-puppet test(s) failed. Commit will be aborted.$(tput sgr0)"
5150
exit 1
5251
fi
5352

commit_hooks/yaml_syntax_check.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ else
1212
module_path=$1
1313
fi
1414

15-
# Get list of new/modified manifest and template files to check (in git index)
1615
# Check YAML file syntax
17-
echo -e "\x1B[0;36mChecking yaml syntax for $module_path...\x1B[0m"
16+
echo -e "$(tput setaf 6)Checking yaml syntax for $module_path...$(tput sgr0)"
1817
ruby -e "require 'yaml'; YAML.parse(File.open('$1'))" 2> $error_msg > /dev/null
1918
if [ $? -ne 0 ]; then
20-
echo -en "\x1B[0;31m"
21-
cat $error_msg
19+
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
2220
syntax_errors=`expr $syntax_errors + 1`
23-
echo -e "Error: yaml syntax error in $module_path (see above)\x1B[0m"
21+
echo -e "$(tput setaf 1)Error: yaml syntax error in $module_path (see above)$(tput sgr0)"
2422
fi
2523
rm -f $error_msg
2624

2725
if [ "$syntax_errors" -ne 0 ]; then
28-
echo -e "\x1B[0;31mError: $syntax_errors syntax error(s) found in hiera yaml. Commit will be aborted.\x1B[0m"
26+
echo -e "$(tput setaf 1)Error: $syntax_errors syntax error(s) found in hiera yaml. Commit will be aborted.$(tput sgr0)"
2927
exit 1
3028
fi
3129

pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fi
108108

109109
#summary
110110
if [ "$failures" -ne 0 ]; then
111-
echo -e "\x1B[0;31mError: $failures subhooks failed. Aborting commit.\x1B[0m"
111+
echo -e "$(tput setaf 1)Error: $failures subhooks failed. Aborting commit.$(tput sgr0)"
112112
exit 1
113113
fi
114114

pre-receive

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@
33
failures=0
44
RC=0
55

6-
subhook_root=hooks/commit_hooks
6+
hook_dir="$(dirname $0)"
7+
hook_symlink="$(readlink -f $0)"
8+
9+
# Figure out where commit hooks are if pre-receive is setup as a symlink
10+
if [ ! -z "$hook_symlink" ]; then
11+
subhook_root="$(dirname $hook_symlink)/commit_hooks"
12+
else
13+
subhook_root="$hook_dir/commit_hooks"
14+
fi
15+
716
tmptree=$(mktemp -d)
817

18+
# Prevent tput from throwing an error by ensuring that $TERM is always set
19+
if [ -z "$TERM" ]; then
20+
TERM=dumb
21+
fi
22+
export TERM
23+
924
while read oldrev newrev refname; do
1025
git archive $newrev | tar x -C ${tmptree}
1126
for changedfile in $(git diff --name-only $oldrev $newrev --diff-filter=ACM); do
@@ -67,7 +82,7 @@ rm -rf ${tmptree}
6782

6883
#summary
6984
if [ "$failures" -ne 0 ]; then
70-
echo -e "\x1B[0;31mError: $failures subhooks failed. Declining push.\x1B[0m"
85+
echo -e "$(tput setaf 1)Error: $failures subhooks failed. Declining push.$(tput sgr0)"
7186
exit 1
7287
fi
7388

0 commit comments

Comments
 (0)