Skip to content

Instantly share code, notes, and snippets.

@pateketrueke
Last active March 26, 2017 03:42
Show Gist options
  • Save pateketrueke/c5a498a6f1320562b22de5f1a0bde533 to your computer and use it in GitHub Desktop.
Save pateketrueke/c5a498a6f1320562b22de5f1a0bde533 to your computer and use it in GitHub Desktop.
#!/bin/bash
SRC="$HOME/sites"
DEST="/etc/caddy"
setup() {
SITE_CONF="$(cat $SRC/$1/Caddyfile)"
SITE_HOST="$(cat $SRC/$1/HOSTS)"
SITE_CONF="$(echo "$SITE_CONF" | sed "s/localhost/$SITE_HOST/g" )"
SITE_CONF="${SITE_CONF/root www/root $DEST/www/$1}"
echo "$SITE_CONF" | sudo tee "$DEST/vhosts/$1" > /dev/null
sudo cp -R "$SRC/$1/www" "$DEST/www/$1"
}
list() {
echo "Available sites:"
for s in `ls $SRC`;
do
SITE="$SRC/$s"
CONF="$SITE/Caddyfile"
if [ -f $CONF ]; then
echo " - $s"
fi
done;
}
all() {
echo "Enabled sites:"
for s in `ls $DEST/vhosts`;
do
echo " - $s"
done;
}
case $1 in
flush)
if [ ! -z "$(command -v systemctl)" ]; then
sudo systemctl restart caddy.service
fi
;;
log)
if [ ! -z "$(command -v journalctl)" ]; then
sudo journalctl -f -u caddy.service
fi
;;
ls)
list
all
;;
on)
if [ -z "$2" ]; then
echo "Missing SITE_NAME"
echo
list
exit 1
fi
setup $2
echo "Site $2 enabled"
;;
off)
if [ -z "$2" ]; then
echo "Missing SITE_NAME"
echo
list
exit 1
fi
if [ ! -f "$DEST/vhosts/$2" ]; then
echo "Site $2 does not exists!"
exit 1
fi
sudo unlink "$DEST/vhosts/$2"
sudo rm -rf "$DEST/www/$2"
echo "Site $2 disabled"
;;
*)
echo "Usage: "
echo " sites on SITE"
echo " sites off SITE"
echo " sites ls"
echo " sites log"
echo " sites flush"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment