Skip to content

Instantly share code, notes, and snippets.

@ConorSheehan1
Last active April 25, 2018 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConorSheehan1/7fff73e825ec7bfc042962adab47987d to your computer and use it in GitHub Desktop.
Save ConorSheehan1/7fff73e825ec7bfc042962adab47987d to your computer and use it in GitHub Desktop.
before commit, check if puppet files have valid syntax
#!/bin/sh
# this hook aborts commits which contain invalid puppet syntax
errors=0
# get all files in commit, use awk to get file name only, use grep to get puppet files only
files=$(git diff --cached --name-status | awk '$1 != "D" { print $2 }' | grep "\.pp")
# if you are committing .pp files
if [[ "$files" ]]; then
# if puppet is not found, warn the user and exit (don't show output)
$(which puppet &> /dev/null)
if [[ $? != 0 ]]; then
echo "'which puppet' did not find puppet comand."
echo "to validate puppet syntax please install puppet."
echo "To override this check add the --no-verify flag "
exit 1
fi
while read -r pp_file; do
echo "Validating: puppet parser validate $pp_file"
# assign output from stdout and stderr to var
cmd_out=$(puppet parser validate $pp_file 2>&1)
# parser should output nothing if successful
# if parser outputs anything, increment errors
if [[ "$cmd_out" ]]; then
let errors=errors+1
# send output to stderr
(>&2 echo "$cmd_out")
fi
done <<< "$files"
# while executes in subshell so use <<< to get access to errors variable
fi
# if any of the files about to be committed did not pass the syntax validation, exit the commit
if [[ $errors != 0 ]]; then
echo "Aborting commit due to $errors puppet syntax errors."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment