utils/generate_html_pages.sh
#!/usr/bin/env bash# suppress stdout from pushd and popd commandspushd () { command pushd "$@" > /dev/null} popd references arguments, but none are ever passed.popd () { command popd "$@" > /dev/null} if [ -z "$1" ]; then echo "You must inform a directory to store built files." exit 1fi PAGES_DIR=$1 Double quote to prevent globbing and word splitting.mkdir -p $PAGES_DIRSTATS_DIR=$PAGES_DIR/statisticsDouble quote to prevent globbing and word splitting.mkdir -p $STATS_DIRDouble quote to prevent globbing and word splitting.touch $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "<!DOCTYPE html>" > $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.
Consider using { cmd1; cmd2; } >> file instead of individual redirects.echo '<html lang="en">' >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "<head>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo '<meta charset="utf-8" />' >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "<title>Statistics</title>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "</head>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "<body>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "<h1>Statistics</h1>" >> $STATS_DIR/index.html# get supported productsproducts=$(echo -e "import ssg.constants\nprint(ssg.constants.product_directories)" | python3 | sed -s "s/'//g; s/,//g; s/\[//g; s/\]//g")for product in $productsdoDouble quote to prevent globbing and word splitting. if [ -d build/$product ]; thenDouble quote to prevent globbing and word splitting. echo "<h4>Product: ${product}</h4>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting. echo "<ul>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting. mkdir -p $STATS_DIR/$productDouble quote to prevent globbing and word splitting. if [ -f build/$product/product-statistics/statistics.html ]; thenDouble quote to prevent globbing and word splitting. cp -rf build/$product/product-statistics $STATS_DIR/$product/product-statisticsDouble quote to prevent globbing and word splitting. echo "<li><a href=\"$product/product-statistics/statistics.html\">Product Statistics</a></li>" >> $STATS_DIR/index.html fiDouble quote to prevent globbing and word splitting. if [ -f build/$product/profile-statistics/statistics.html ]; thenDouble quote to prevent globbing and word splitting. cp -rf build/$product/profile-statistics $STATS_DIR/$product/profile-statisticsDouble quote to prevent globbing and word splitting. echo "<li><a href=\"$product/profile-statistics/statistics.html\">Profile statistics</a></li>" >> $STATS_DIR/index.html fiDouble quote to prevent globbing and word splitting. echo "</ul>" >> $STATS_DIR/index.html fidoneDouble quote to prevent globbing and word splitting.echo "</body>" >> $STATS_DIR/index.htmlDouble quote to prevent globbing and word splitting.echo "</html>" >> $STATS_DIR/index.html # Generate Guides pageDouble quote to prevent globbing and word splitting.mkdir -p $PAGES_DIR/guidesDouble quote to prevent globbing and word splitting.cp -rf build/guides $PAGES_DIRDouble quote to prevent globbing and word splitting.utils/gen_html_guides_index.py . $PAGES_DIR/guides/index.htmlretVal=$?if [ $retVal -ne 0 ]; then echo "Something wrong happened while generating the HTML Guides Index page" exit 1fi # Generate Mapping Tables pagepushd build/tablestouch index.htmlecho "<!DOCTYPE html>" > index.htmlConsider using { cmd1; cmd2; } >> file instead of individual redirects.echo '<html lang="en">' >> index.htmlecho "<head>" >> index.htmlecho '<meta charset="utf-8" />' >> index.htmlecho "<title>Mapping Tables</title>" >> index.htmlecho "</head>" >> index.htmlecho "<body>" >> index.htmlecho "<h1>Mapping Tables</h1>" >> index.htmlecho "<ul>" >> index.htmlfor table in table-*.htmldo echo "<li><a href=\"${table}\">${table}</a></li>" >> index.htmldoneConsider using { cmd1; cmd2; } >> file instead of individual redirects.echo "</ul>" >> index.htmlecho "</body>" >> index.htmlecho "</html>" >> index.htmlUse popd "$@" if function's $1 should mean script's $1.popd # Generate Rendered Policies pagePOLICY_DIR="$PAGES_DIR/rendered-policies"mkdir -p "$POLICY_DIR"products=$(echo -e "import ssg.constants\nprint(ssg.constants.product_directories)" | python3 | sed -s "s/'//g; s/,//g; s/\[//g; s/\]//g")for product in $productsdoDouble quote to prevent globbing and word splitting. if [ -d build/$product ]; then mkdir -p "$POLICY_DIR/$product" if [ -d "build/$product/rendered-policies/" ]; then cp -rf "build/${product}/rendered-policies/"* "$POLICY_DIR/$product/" fi fidoneutils/gen_rendered_policies_index.py . "$PAGES_DIR/rendered-policies/index.html"retVal=$?if [ $retVal -ne 0 ]; then echo "Something wrong happened while generating the HTML Rendered Policy Index page" exit 1fi # Generate Components pageCOMPONENTS_DIR="$PAGES_DIR/components"mkdir -p "$COMPONENTS_DIR"utils/render_components.py -r "$(pwd)" "$COMPONENTS_DIR"retVal=$?if [ $retVal -ne 0 ]; then echo "Something wrong happened while generating components HTML pages." exit 1fi # Generate Prometheus StatsPROMETHEUS_STATS_DIR="$PAGES_DIR/prometheus_stats"mkdir -p "$PROMETHEUS_STATS_DIR"mv build/policies_metrics "$PROMETHEUS_STATS_DIR" Double quote to prevent globbing and word splitting.pushd $PAGES_DIRtouch index.htmlecho "<!DOCTYPE html>" > index.htmlConsider using { cmd1; cmd2; } >> file instead of individual redirects.echo '<html lang="en">' >> index.htmlecho "<head>" >> index.htmlecho '<meta charset="utf-8" />' >> index.htmlecho "<title>Available Artifacts</title>" >> index.htmlecho "</head>" >> index.htmlecho "<body>" >> index.htmlecho "<h1>Available Artifacts</h1>" >> index.htmlecho "<ul>" >> index.htmlecho "<li><a href=\"statistics/index.html\">Statistics</a></li>" >> index.htmlecho "<li><a href=\"guides/index.html\">Guides</a></li>" >> index.htmlecho "<li><a href=\"tables/index.html\">Mapping Tables</a></li>" >> index.htmlecho "<li><a href=\"srg_mapping/index.html\">SRG Mapping Tables</a></li>" >> index.htmlecho "<li><a href=\"rendered-policies/index.html\">Rendered Policies</a></li>" >> index.htmlecho "<li><a href=\"components/index.html\">Components</a></li>" >> index.htmlecho "<li><a href=\"prometheus_stats/policies_metrics\">Prometheus Policies Metrics</a></li>" >> index.htmlecho "</ul>" >> index.htmlecho "</body>" >> index.htmlecho "</html>" >> index.htmlUse popd "$@" if function's $1 should mean script's $1.popd Double quote to prevent globbing and word splitting.cp -rf build/tables $PAGES_DIR exit 0