This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhugo-audit.sh
executable file
·87 lines (77 loc) · 2.87 KB
/
hugo-audit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
set -e
set -o pipefail
[ -z "$HUGO_COMMAND" ] && HUGO_COMMAND="hugo"
if [ -z "$SITEROOT" ]; then
SITEROOT="$(pwd)"
fi
if [ -z "$SITESRC" ]; then
if [ -d "$(pwd)"/exampleSite ]; then
SITESRC="$(pwd)"/exampleSite
fi
fi
if [ -n "$SITECONFIG" ]; then
SITECONFIG="--config ""$SITECONFIG"
fi
if [ -z "${HUGO_CACHEDIR}" ]; then
HUGO_CACHEDIR="$(pwd)/hugo-cache"
fi
export HUGO_CACHEDIR
rm -rf "${SITEROOT}/public"
echo "Building for audit in ${SITEROOT}/public for environment ${HUGO_ENV:-development}"
# shellcheck disable=2086
if HUGO_MINIFY_TDEWOLFF_HTML_KEEPCOMMENTS=true HUGO_ENABLEMISSINGTRANSLATIONPLACEHOLDERS=true HUGO_RESOURCEDIR="$(pwd)/resources" "$HUGO_COMMAND" $SITECONFIG --gc --buildDrafts --buildFuture --destination "${SITEROOT}/public" --source "${SITESRC}" --environment "${HUGO_ENV:-development}" ${BASEURL:+-b $BASEURL} && [ -s "${SITEROOT}/public/index.html" ]; then
# If hugo build succeeds, it is possible audit issues are present, check further
# Check for problem indicators (see https://discourse.gohugo.io/t/audit-your-published-site-for-problems/35184)
set +e
grep -iIrnE '<\!-- raw HTML omitted -->|ZgotmplZ|hahahugo|\[i18n\]|\(<nil>\)|\(<nil>\)' "${SITEROOT}/public/" >hugo-audit.log
RET=$?
set -e
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" ] && [ -s hugo-audit.log ]; then
set +e
grep -iIvE 'grep.+((-- raw HTML omitted --|ZgotmplZ|hahahugo|\\\[i18n\\\]|\\\(<nil>\\\)|\\\(<nil>\\\)).*)+' hugo-audit.log
RET2=$?
set -e
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
rm -rf "${SITEROOT}/public"
echo "Building unminified site for checks in ${SITEROOT}/public for environment ${HUGO_ENV:-development}"
# Build unminified site without audit-only information
if HUGO_MINIFY_TDEWOLFF_HTML_KEEPCOMMENTS=false HUGO_ENABLEMISSINGTRANSLATIONPLACEHOLDERS=false HUGO_RESOURCEDIR="$(pwd)/resources" "$HUGO_COMMAND" $SITECONFIG --gc --buildDrafts --buildFuture --destination "${SITEROOT}/public" --source "${SITESRC}" --environment "${HUGO_ENV:-development}" ${BASEURL:+-b $BASEURL} >hugo-build-unminified.log; then
if [ -s "${SITEROOT}/public/index.html" ]; then
set +e
grep -InE '(WARN [0-9:/\ ]+ FAIL: )' hugo-build-unminified.log
RET3=$?
set -e
if [ "$RET3" = "0" ]; then
echo "not ok"
exit 1
fi
else
echo "not ok"
exit 1
fi
echo "ok"
exit 0
else
# If Hugo build fails, audit fails
echo "not ok"
exit 1
fi
else
# If Hugo build fails, audit fails
echo "not ok"
exit 1
fi