Scripting Site Audit suggested by @jmooring

This automates the audit suggested by @jmooring in

This is a little trickier than one might think because of the return code behavior of grep.

At least with ‘GNU’ grep:

  • 0 is returned if there is a match
  • 1 is returned if there is no match
  • 2 is returned on an actual error

I make no claims of great code writing; and my usual caveats that it might harm your kittens :skull:, frighten your children :ghost: or eat your data :magnet: :pizza: apply.

#/bin/bash

set -o pipefail

[ -z "$HUGO_COMMAND" ] && HUGO_COMMAND="hugo"

if HUGO_MINIFY_TDEWOLFF_HTML_KEEPCOMMENTS=true HUGO_ENABLEMISSINGTRANSLATIONPLACEHOLDERS=true HUGO_RESOURCEDIR=$(pwd)/resources $HUGO_COMMAND --gc --cleanDestinationDir  --destination $(pwd)/public --source $(pwd)/exampleSite
then
    # If hugo build succeeds but possible audit issues are present, check further
    # Check for problem indicators (see https://discourse.gohugo.io/t/audit-your-published-site-for-problems/35184)
    grep -iIrnE '<\!-- raw HTML omitted -->|ZgotmplZ|hahahugo|\[i18n\]|\(<nil>\)|\(&lt;nil&gt;\)' public/ >hugo-audit.log; RET=$?
    if [ "$RET" != "1" ] && [ "$RET" != "0" ]
    then
        # Make sure we fail if there is is error executing the check command
        echo "not ok"
        exit 1
    fi

    # Check if problem indicators are part of some page's content (e.g. a page describing how
    # to check for Hugo audit issues).
    if [ "$RET" = "0" ]
    then
        grep -iIvE 'grep(.+(-- raw HTML omitted --|ZgotmplZ|hahahugo|\[i18n\]|\(<nil>\)|\(&lt;nil&gt;\))+)' hugo-audit.log; RET2=$?
        if [ "$RET2" != "1" ]
        then
            # Make sure we fail if there is an error executing the false positive elimination command (exit code 2)
            # Also fail if there was a true positive (exit code 0)
            echo "not ok"
            exit 1
        fi
    fi
    echo "ok"
    exit 0
else
    # If Hugo build fails, audit fails
    echo "not ok"
    exit 1
fi

Hope someone finds this useful.

And apologies to those who saw my first attempt (which I cancelled) at posting this a few days ago ; it apparently wasn’t my day and the code was ‘strange’.

In any event I have successfully been using this for local audits.

For my GitHub actions audit I use an Action I created (WIP; experimental, etc, etc):

https://github.com/danielfdickinson/hugo-action-build-audit

EDIT: Note that prior to 2022-01-16 16:58 EST (-0500) that the script erroneously had grep -iIrnoE instead of grep -iIrnE in the first grep. That results in failure to detect a false positive due to describing the edit command in one’s page content.

2 Likes

Thanks! I just saw this after I ended up writing something similar: Audit your published site for problems - #8 by kaushalmodi

1 Like