DEV Community

Cover image for πŸ“Ÿ Updates to my autosched script to schedule posts with Hugo
Ryan Collins
Ryan Collins

Posted on • Originally published at gozgeek.com on

πŸ“Ÿ Updates to my autosched script to schedule posts with Hugo

A couple of weeks ago I posted how I schedule posts with Hugo. At the time, I had two scripts. The first one automatically selected the next day to schedule, while the other script let you pick the day. I’ve now combined the two. Here you go, the finished autosched:

#!/bin/bash

# Set these to where you want the posts to go and where you
# want to archive the posts
postsdir="${HOME}/Web/gozgeek.com/content/posts/$(date +"%Y")"
archive="${HOME}/notes/Archive/$(date +"%Y")"

RANDOM=$(date +%s%N | cut -b10-19)
h=$(( $RANDOM % 8 + 8 ))
m=$(( $RANDOM % 59 + 0 ))

# Only one argument was passed, the script assumes it's a filename
if ["$#" -eq 1]; then
    d=$(find ${postsdir} | xargs -I {} basename "{}" | sort -r | head -1)
    day=$(date -d "${d:0:10} + 1 day" +"%Y-%m-%d")
    filename="${1}"
# Two arguments - first is the date in YYYY-MM-DD format, the second
# is the filename
elif ["$#" -eq 2]; then
    day="${1}"
    filename="${2}"
else
    echo "Incorrect number of arguments"
fi

if [[${filename:0:1} == "2"]]; then
    outfilename="${day}-${filename:11}"
else
    outfilename="${day}-${filename}"
fi

schedule="${day}T$(printf "%02d" ${h}):$(printf "%02d" ${m}):00-04:00"

#echo "$postsdir $outfilename $schedule" 
# Put the file in the right place
cp "${filename}" "${postsdir}/${outfilename}"

# Add new schedule date and time
sed -i "s/^Date: .*$/date: ${schedule}/" "${postsdir}/${outfilename}"

# Remove Status
sed -i "/^Status: .*$/d" "${postsdir}/${outfilename}"

# Archive post
# My drafts are in a git repo.
git add "${filename}"
git mv "${filename}" "${archive}/${outfilename}"

echo "${filename} scheduled for ${day}!"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)