Skip to content

Instantly share code, notes, and snippets.

@jandryuk
Last active November 23, 2020 17:13
Show Gist options
  • Save jandryuk/685b15055041e70a789edd22a08e43e8 to your computer and use it in GitHub Desktop.
Save jandryuk/685b15055041e70a789edd22a08e43e8 to your computer and use it in GitHub Desktop.
Script to normalize an XSM Flask policy
#!/bin/bash
# pipe an expanded XSM Flask policy.conf in:
# $0 < policy.conf
#
# The script chokes on booleans, edit policy.conf to remove
# "if ( ) { } else { } " and just leave the case you want to evaluate.
set -o noglob
debug() {
[ -z "$DEBUG" ] && return
echo "$@"
}
print_line() {
l="$@"
l=$( echo $l )
case $l in
*{*)
;;
*)
echo $l
return
;;
esac
header="${l%\{*}"
footer="${l#*\}}"
props="${l#*\{}"
props="${props%\}*}"
sorted=$( echo $props | awk -v RS=' ' -v ORS="\n" '{print}' | sort | xargs )
for l in $sorted ; do
echo "$header{" $l "}$footer"
done
}
holding=0
while read line ; do
line=${line//^[ \t]*/}
line=${line//#*/}
[ -z "$line" ] && continue
case $line in
^$)
debug empty line
continue
;;
^[#]*)
debug comment
continue
;;
*{*}*|*}*{*)
if [ $holding -gt 0 ] ; then
debug holding one line
hold="$hold $line"
else
debug printing one line
print_line $line
fi
;;
*{*)
debug opening "{"
hold="$hold $line"
holding=$(( holding + 1 ))
;;
*}*)
debug closing "}"
holding=$(( holding - 1 ))
print_line "$hold $line"
hold=
;;
*)
if [ $holding -gt 0 ] ; then
debug holding line
hold="$hold $line"
else
debug printing line
print_line $line
fi
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment