dotfiles/scripts/hugoctl
2020-05-27 17:26:00 -04:00

110 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# hugoctl -- hugo wrapper script to help manage a hugo blog
# hugo variables (local machine)
scriptname=$(basename "$0")
blog_dir="$HOME/src/blog" # or the path to your blog
local_url="http://localhost:1313/posts/"
post_filename=""
postname=""
# deployment variables
set_names() {
local arg="$1"
if [[ ! "$arg" =~ .md$ ]]; then
postname="$arg"
else
postname="${arg%???}"
fi
post_filename="$postname.md"
}
serve_and_open() {
killall hugo > /dev/null 2>&1
hugo serve -s "$blog_dir" -D > /dev/null 2>&1 &
xdg-open "http://localhost:1313/posts/$postname"
echo "Don't forget to \"killall hugo\" when finished"
}
edit_post() {
serve_and_open
$EDITOR "$blog_dir/content/posts/$post_filename" && kill %1
}
new_post() {
hugo new -s "$blog_dir" "posts/$post_filename"
edit_post
}
deploy_blog() {
# TODO make this transparent but don't leak stuff
printf "${green}%s\n${reset}" "Deploying..."
}
print_help() {
printf "%s\n" "Usage: "
printf "%s\n\n" "$scriptname [args]"
printf "%s\n" "Options: "
printf "\t%s:\n\t\t%s\n\n" "new-post (aliases: newpost,np,new,n) [post-name]" "create a new post, open editor and live server"
printf "\t%s:\n\t\t%s\n\n" "edit-post (alises: edit,ep,e) [post-name]" "edit a post in the content directory"
printf "\t%s:\n\t\t%s\n\n" "list-post (aliases: list,lp,ls,l)" "list all posts in content directory"
printf "\t%s:\n\t\t%s\n\n" "list-drafts (alises: listdraft,ld)" "list all posts in content directory"
printf "\t%s:\n\t\t%s\n\n" "server (alises: serve,s)" "serve blog from your blog directory"
printf "\t%s:\n\t\t%s\n\n" "kill (alises: killlall,ka,k)" "kill all hugo processes"
printf "\t%s:\n\t\t%s\n\n" "deploy (aliases: dep,d)" "deploy to a remote server"
printf "\n"
}
if [[ "$#" -lt 1 ]]; then
print_help
exit 1
fi
while (($#)); do
case "$1" in
new-post|newpost|np|new|n)
shift
set_names "$1"
new_post "$1"
break
;;
edit-post|edit|ep|e)
shift
set_names "$1"
edit_post "$1"
break
;;
list-post|list|lp|l|ls)
ls -ltr "$blog_dir/content/posts"
break
;;
list-drafts|listdraft|ld)
hugo list drafts -s "$blog_dir"
break
;;
server|serve|s)
serve_and_open
break
;;
kill|killall|ka|k)
killall "hugo"
break
;;
deploy|dep|d)
deploy_blog
break
;;
help|-h|--help)
print_help
break
;;
*)
print_help
break
;;
esac
done