Last active
August 29, 2015 14:06
-
-
Save kakwa/3983068d34449e1dca23 to your computer and use it in GitHub Desktop.
script validating puppet and erb syntax
This file contains 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/sh | |
# Released under MIT License | |
RCol='\33[0m' # Text Reset | |
# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds | |
Bla='\33[0;30m'; BBla='\33[1;30m'; UBla='\33[4;30m'; IBla='\33[0;90m'; BIBla='\33[1;90m'; On_Bla='\33[40m'; On_IBla='\33[0;100m'; | |
Red='\33[0;31m'; BRed='\33[1;31m'; URed='\33[4;31m'; IRed='\33[0;91m'; BIRed='\33[1;91m'; On_Red='\33[41m'; On_IRed='\33[0;101m'; | |
Gre='\33[0;32m'; BGre='\33[1;32m'; UGre='\33[4;32m'; IGre='\33[0;92m'; BIGre='\33[1;92m'; On_Gre='\33[42m'; On_IGre='\33[0;102m'; | |
Yel='\33[0;33m'; BYel='\33[1;33m'; UYel='\33[4;33m'; IYel='\33[0;93m'; BIYel='\33[1;93m'; On_Yel='\33[43m'; On_IYel='\33[0;103m'; | |
Blu='\33[0;34m'; BBlu='\33[1;34m'; UBlu='\33[4;34m'; IBlu='\33[0;94m'; BIBlu='\33[1;94m'; On_Blu='\33[44m'; On_IBlu='\33[0;104m'; | |
Pur='\33[0;35m'; BPur='\33[1;35m'; UPur='\33[4;35m'; IPur='\33[0;95m'; BIPur='\33[1;95m'; On_Pur='\33[45m'; On_IPur='\33[0;105m'; | |
Cya='\33[0;36m'; BCya='\33[1;36m'; UCya='\33[4;36m'; ICya='\33[0;96m'; BICya='\33[1;96m'; On_Cya='\33[46m'; On_ICya='\33[0;106m'; | |
Whi='\33[0;37m'; BWhi='\33[1;37m'; UWhi='\33[4;37m'; IWhi='\33[0;97m'; BIWhi='\33[1;97m'; On_Whi='\33[47m'; On_IWhi='\33[0;107m'; | |
DIR=./ | |
help(){ | |
echo "usage: `basename $0` [-h] [-d <directory>]" | |
echo "" | |
echo "simple syntax checker for puppet and erb files" | |
echo "arguments:" | |
echo " -h: displays this help" | |
echo " -d <directory>: checks .erb and .pp file of <directory>" | |
echo " if not specified, checks from current" | |
echo " directory (./)" | |
exit 1 | |
} | |
while getopts ":hd:" opt; do | |
case $opt in | |
h) help | |
;; | |
d) | |
DIR="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
help | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
help | |
exit 1 | |
;; | |
esac | |
done | |
ok_message(){ | |
file="$1" | |
width=$((80 - `echo ${file}|wc -c`)) | |
printf "${Gre}Syntax of ${Blu}[${BYel}${file}${Blu}]${Gre}%${width}sOK${RCol}\n" ' ' | |
} | |
ko_message(){ | |
file="$1" | |
width=$((66 - `echo ${file}|wc -c`)) | |
printf "${Red}Syntax check failed for ${Blu}[${BPur}${file}${Blu}]${Red}%${width}sKO${RCol}\n" ' ' | |
} | |
validate_pp(){ | |
file="$1" | |
puppet parser validate "$file" | |
ret=$? | |
if [ $ret -eq 0 ] | |
then | |
ok_message "$file" | |
else | |
ko_message "$file" | |
fi | |
} | |
validate_erb(){ | |
file="$1" | |
erb -x -T '-' "$file" | ruby -c 1>/dev/null 2>&1 | |
ret=$? | |
if [ $ret -eq 0 ] | |
then | |
ok_message "$file" | |
else | |
ko_message "$file" | |
fi | |
} | |
if ! [ -d "$DIR" ] | |
then | |
printf "Directory $DIR doesn't exist\n" | |
help | |
fi | |
find "$DIR" -name "*.pp" |while read file | |
do | |
validate_pp "$file" | |
done | |
find "$DIR" -name "*.erb" |while read file | |
do | |
validate_erb "$file" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment