Skip to content

Commit 4402582

Browse files
committed
Add json checks (useful for metadata.json)
This patch provides syntax checks for metadata.json, a file that helps define characteristics of a puppet modue (usually pertaining to puppet forge). The error provided by this check tends to be vague or misleading (such is the nature when referencing improperly formatted structured data). Fixes: #11
1 parent 916f904 commit 4402582

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

commit_hooks/json_syntax_check.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
# This script expects $1 to be passed and for $1 to be the filesystem location
4+
# to a json file for which it will run syntax checks against.
5+
6+
syntax_errors=0
7+
error_msg=$(mktemp /tmp/error_msg_json-syntax.XXXXX)
8+
9+
if [ $2 ]; then
10+
module_path=$(echo $1 | sed -e 's|'$2'||')
11+
else
12+
module_path=$1
13+
fi
14+
15+
# Get list of new/modified manifest and template files to check (in git index)
16+
# Check json file syntax
17+
echo -e "\x1B[0;36mChecking json syntax for $module_path...\x1B[0m"
18+
ruby -e "require 'json'; JSON.parse(File.read($1))" 2> $error_msg > /dev/null
19+
if [ $? -ne 0 ]; then
20+
echo -en "\x1B[0;31m"
21+
cat $error_msg
22+
syntax_errors=`expr $syntax_errors + 1`
23+
echo -e "Error: json syntax error in $module_path (see above)\x1B[0m"
24+
fi
25+
rm -f $error_msg
26+
27+
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"
29+
exit 1
30+
fi
31+
32+
exit 0

pre-commit

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,17 @@ for changedfile in `git diff --cached --name-only --diff-filter=ACM`; do
6767
failures=`expr $failures + 1`
6868
fi
6969
fi
70+
71+
#check json (i.e. metadata.json) syntax
72+
if [ $(echo $changedfile | grep -q '\.*.json$'; echo $?) -eq 0 ]; then
73+
${subhook_root}/json_syntax_check.sh $changedfile
74+
RC=$?
75+
if [ "$RC" -ne 0 ]; then
76+
failures=`expr $failures + 1`
77+
fi
78+
fi
7079
else
71-
echo "ruby not installed. Skipping erb/yaml checks..."
80+
echo "ruby not installed. Skipping erb/yaml/json checks..."
7281
fi
7382

7483
#puppet manifest styleguide compliance

0 commit comments

Comments
 (0)