Script: Publish Hugo Sites

Published 05-05-2018 03:11:46

Raw

#!/bin/bash
# Bound to Mod+Shift+Backspace
# Publish a Hugo site to a remote server
# User-defined variables are set in
# ~/.config/hugosites.conf file
Say() { echo "$1" "$2" ; notify-send "$1" "$2" ; echo "$1" "$2" >> $LogFile ;}
ConfigFile="/home/$USER/.config/hugosites.conf"
ConfigFiletmp='/tmp/hugosites.conf'
if egrep -q -v '^#|^[^ ]*=[^;]*' "$ConfigFile"
then
	Say "🗞️ Config file is unclean, cleaning it..."
	egrep '^#|^[^ ]*=[^;&]*'  "$ConfigFile" > "$ConfigFiletmp"
	cp "$ConfigFiletmp" "$ConfigFile"
fi
source "$ConfigFile"
runprefix=
# Set menu properties
mymenu=$(setmenu -l)
amitty=$(tty)
case $amitty in
	*tty1|"not a tty") runprefix="$TERMINAL -e" ;;
esac
mkdir -p $PathToLogs/push
LogFile="$PathToLogs/push/$(date +%b%d)/$(date +%H%M).$Chosen.log"
Choices="theatomheart.net\n$SiteName1\n$SiteName2\n$SiteName3\nConfigure"

# # --- Functions
ClearExit() { clear && exit ;}
Configure() { $runprefix $EDITOR ~/.config/hugosites.conf && ClearExit ;}
GetConfirmation() {
	clear
	Confirm=$(echo -e "Yes\nNo" | $mymenu "Publish $Chosen?") || ClearExit
	[[ $Confirm = "No" ]] && ClearExit
	clear ;}
CompileSite() {
	Say "🗞️ Compiling website." &&
	rm -rf $PathToHugo/$Chosen/public/ &&
	date >> $LogFile &&
	cd $PathToHugo/$Chosen &&
	hugo >> $LogFile ;}
MountServer() {
	sshfs $Username@$ServerHost:../../var/www/$Chosen $PathToHugo/.push/$Chosen &&
	Say "🔗 Connected to $ServerHost." ;}
SyncFiles() {
	Say "🗞️ Publishing web data."
	rsync -rtpvus --progress --delete $PathToHugo/$Chosen/public/ $PathToHugo/.push/$Chosen >> $LogFile ;}
Chosen=$(echo -e "$Choices" | $mymenu "Publish site:") || ClearExit
if [ $Chosen = "theatomheart.net" ] ; then ~/Projects/theatomheart.net
elif [ $Chosen = "Configure" ] ; then Configure
else
	mkdir -p $PathToLogs/push/$(date +%b%d)/
	GetConfirmation &&
	CompileSite || Say "⛔ There was a problem compiling $Chosen"
	MountServer || Say "⛔ There was a problem connecting to $ServerHost."
	SyncFiles || Say "⛔ There was a problem pushing files to $ServerHost."
	fusermount -u $PathToHugo/.push/$Chosen
	rm -rf $PathToHugo/$Chosen/public/
	clear
	Say "👍️ Finished publishing $Chosen." &&
	mpv $HOME/.scripts/notify.mp3 &>/dev/null
fi