forked from drwahl/puppet-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson_syntax_check.sh
More file actions
executable file
·43 lines (36 loc) · 1.48 KB
/
json_syntax_check.sh
File metadata and controls
executable file
·43 lines (36 loc) · 1.48 KB
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
#!/bin/bash
# This script expects $1 to be passed and for $1 to be the filesystem location
# to a json file for which it will run syntax checks against.
syntax_errors=0
error_msg=$(mktemp /tmp/error_msg_json-syntax.XXXXX)
if [ $2 ]; then
module_path=$(echo $1 | sed -e 's|'$2'||')
else
module_path=$1
fi
# Check json file syntax
$ERRORS_ONLY || echo -e "$(tput setaf 6)Checking json syntax for $module_path...$(tput sgr0)"
ruby -e "require 'rubygems'; require 'json'; JSON.parse(File.read('$1'))" 2> $error_msg > /dev/null
if [ $? -ne 0 ]; then
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
syntax_errors=`expr $syntax_errors + 1`
echo -e "$(tput setaf 1)Error: json syntax error in $module_path (see above)$(tput sgr0)"
fi
rm -f $error_msg
if which metadata-json-lint > /dev/null 2>&1; then
if [[ "$(basename $1)" == 'metadata.json' ]]; then
metadata-json-lint $1 2> $error_msg >&2
if [ $? -ne 0 ]; then
cat $error_msg | sed -e "s/^/$(tput setaf 1)/" -e "s/$/$(tput sgr0)/"
syntax_errors=`expr $syntax_errors + 1`
echo -e "$(tput setaf 1)Error: json syntax error in $module_path (see above)$(tput sgr0)"
fi
fi
else
echo "metadata-json-lint gem not installed. Skipping metadata-json-lint tests..."
fi
if [ "$syntax_errors" -ne 0 ]; then
echo -e "$(tput setaf 1)Error: $syntax_errors syntax error(s) found in json file. Commit will be aborted.$(tput sgr0)"
exit 1
fi
exit 0