I put this together when I first started working with hugo, mostly to gain familiarity with the options, then to deal with multiple sites I set up for toying around with. It’s in my path as h
and h --help
explains the options.
#!/bin/bash
############################################################################
# written for hugo 0.13
############################################################################
#
doBuild=NO
doClean=NO
doCreate=NO
doPost=NO
doServer=NO
doWatch=
############################################################################
# you can set SITE and THEME to constant values for your convenience
#
HUGO=hugo
POST=
SITEROOT=path/to/your/sites/
THEMESDIR=themes/
SITE= # your site name
THEME= # theme if you use one
############################################################################
#
while [ -n "${1}" ]
do
case "${1}" in
--help)
echo
echo "usage: h [hugoOptions]"
echo " opt: --server run server"
echo " opt: --watch run server and watch for changes"
echo " opt: --build generate content"
echo " opt: --post=_title_ create a new post"
echo " opt: --clean remove all generated files"
echo " opt: --create create a new site"
echo " opt: --site=_site_ specify site name"
echo " opt: --theme=_theme_ specify theme name"
echo
echo " SITEROOT == '${SITEROOT}'"
echo " defaultSITE == '${SITE}'"
echo " defaultTHEME == '${THEME}'"
echo
exit 0 ;;
--build)
doBuild=YES ;;
--clean)
doClean=YES ;;
--create)
doCreate=YES ;;
--post=*)
doPost=YES
POST=${1##--post=}
if [ -z "${POST}" ]
then
echo
echo "error: invalid option '${1}'"
echo
exit 2
fi ;;
--server)
doServer=YES ;;
--site=*)
SITE=${1##--site=}
if [ -z "${SITE}" ]
then
echo
echo "error: invalid option '${1}'"
echo
exit 2
fi ;;
--theme=*)
THEME=${1##--theme=}
if [ -z "${THEME}" ]
then
echo
echo "error: invalid option '${1}'"
echo
exit 2
fi ;;
--watch)
doServer=YES
doWatch=--watch ;;
*)
break ;;
esac
shift
done
echo " SITEROOT == '${SITEROOT}'"
echo " SITE == '${SITE}'"
echo " THEME == '${THEME}'"
############################################################################
#
if [ ! -d "${SITEROOT}" ]
then
echo
echo "error: site root directory missing"
echo " SITEROOT == '${SITEROOT}'"
echo
exit 2
elif [ -z "${SITE}" ]
then
echo
echo "error: site must be defined"
echo
exit 2
elif [ -z "${THEME}" ]
then
echo
echo "error: theme must be defined"
echo
exit 2
fi
############################################################################
#
if [ "${doCreate}" == "YES" ]
then
echo
echo " info: attempting to create new site"
echo " SITEROOT == '${SITEROOT}'"
echo " SITE == '${SITE}'"
echo " THEMESDIR == '${THEMESDIR}'"
echo " THEME == '${THEMES}'"
if [ -d "${SITE}" ]
then
echo
echo "error: unable to create site (already exists)"
echo
exit 2
fi
if [ ! -d "${SITEROOT}${THEMESDIR}" ]
then
echo
echo "error: unable to locate themes directory"
echo " SITEROOT == '${SITEROOT}'"
echo " THEMESDIR == '${THEMESDIR}'"
echo
exit 2
elif [ ! -d "${SITEROOT}${THEMESDIR}${THEME}" ]
then
echo
echo "error: unable to locate theme"
echo " SITEROOT == '${SITEROOT}'"
echo " THEMESDIR == '${THEMESDIR}'"
echo " THEME == '${THEMES}'"
echo
exit 2
fi
hugo new site "${SITEROOT}${SITE}"
rv=$?
if [ "${rv}" != 0 ]
then
echo
echo "error: unable to create site"
echo
echo
exit $rv
fi
# hugo will not create the themes directory by default
if [ ! -d "${SITEROOT}${SITE}/themes/" ]
then
mkdir "${SITEROOT}${SITE}/themes/" || exit 2
fi
cd "${SITEROOT}" || exit 2
(cd "${THEMESDIR}" && tar cf - "${THEME}" ) | ( cd "${SITE}/themes" && tar xf - )
if [ ! -d "${SITEROOT}${THEMESDIR}${THEME}" ]
then
echo
echo "error: unable to create theme in site"
echo " SITEROOT == '${SITEROOT}'"
echo " SITE == '${SITE}'"
echo " THEMESDIR == '${THEMESDIR}'"
echo " THEME == '${THEMES}'"
echo
exit 2
fi
cd "${SITE}" || exit 2
# create a configuration file that uses the theme
if [ -f "themes/${THEME}/config.toml" ]
then
cp -p "themes/${THEME}/config.toml" "config.${THEME}.toml"
else
echo 'MetaDataFormat = "toml"' > "config.${THEME}.toml"
echo "theme = \"${THEME}\"" >> "config.${THEME}.toml"
cat config.toml >> "config.${THEME}.toml"
fi
echo " info: created site - please edit config.${THEME}.toml"
echo " SITEPATH == '${PWD}'"
echo
exit 0
fi
############################################################################
#
if [ ! -d "${SITEROOT}${SITE}" ]
then
echo
echo "error: site directory missing"
echo " SITEROOT == '${SITEROOT}'"
echo " SITE == '${SITE}'"
echo
exit 2
fi
cd "${SITEROOT}${SITE}" || exit 2
############################################################################
# inject an opinion here on what the configuration file name should be.
# then verify that the theme in the file matches our environment.
#
CONFIG="config.${THEME}.toml"
echo " CONFIG == '${CONFIG}'"
if [ ! -f "${CONFIG}" ]
then
echo
echo "error: missing configuration file"
echo " SITEROOT == '${SITEROOT}'"
echo " SITE == '${SITE}'"
echo " THEME == '${THEME}'"
echo " CONFIG == '${CONFIG}'"
echo
exit 2
fi
themeInFile=$( sed 's/[ ]//g' "${CONFIG}" | awk -F'"' '$1 == "theme=" { print $2; exit; }' )
if [ "${themeInFile}" != "${THEME}" ]
then
echo
echo "error: theme in configuration file does not match THEME in script"
echo " THEME == '${THEME}'"
echo " themeInFile == '${themeInFile}'"
echo
exit 2
fi
############################################################################
#
if [ "${doClean}" == "YES" ]
then
if [ ! -d "public" ]
then
echo
echo "error: no public directory to clean"
echo
exit 2
fi
echo " warn: removing existing public directory contents..."
rm -rf public/* # */
fi
############################################################################
#
if [ "${doBuild}" == "YES" ]
then
$HUGO --config=$CONFIG --buildDrafts --buildFuture --verbose
rv=$?
if [ "${rv}" != 0 ]
then
echo " STATUS == '${rv}'"
echo
exit $rv
fi
fi
############################################################################
#
if [ "${doPost}" == "YES" ]
then
$HUGO new "post/${POST}.md"
exit $?
fi
############################################################################
#
if [ "${doServer}" == "YES" ]
then
echo $HUGO server --config=$CONFIG ${doWatch} --buildDrafts --buildFuture --verbose server
$HUGO server --config=$CONFIG ${doWatch} --buildDrafts --buildFuture --verbose server
exit 0
fi
############################################################################
#
if [ -z "${1}" ]
then
echo
exit 0
fi
############################################################################
#
$PWD
echo $HUGO --config=$CONFIG "$@"
$HUGO --config=$CONFIG "$@"
rv=$?
############################################################################
#
echo " STATUS == '${rv}'"
echo
exit $rv