Created
March 4, 2015 16:13
-
-
Save hartfordfive/327a6abf0cb832092300 to your computer and use it in GitHub Desktop.
Git pre-commit hook to validate Chef JSON environment files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Comments: | |
# 1. You must add execution rights to this file (chmod u+x pre-commit) | |
# 2. You need to have the 'jq' package installed to parse the json | |
echo -e "\n#### Validating chef environment files ####" | |
if git-rev-parse --verify HEAD >/dev/null 2>&1; then | |
against=HEAD | |
else | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
bad_file=0 | |
for FILE in `git diff-index --name-status $against -- | cut -c3-` ; do | |
if [ ${FILE: -5} == ".json" ]; then | |
echo -e " Validating $FILE..." | |
cat $FILE | jq 'any' | |
if [[ $? -ne 0 ]]; then | |
echo -e "\tERROR: JSON parser failed!" | |
bad_file=1 | |
fi | |
fi | |
done | |
if [[ $bad_file -eq 1 ]]; then | |
echo -e "\n" | |
exit 1 | |
else | |
echo "#### All JSON files OK! #####" | |
echo -e "\n" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment