www.eamoncaddigan.net

Content and configuration for https://www.eamoncaddigan.net
git clone https://git.eamoncaddigan.net/www.eamoncaddigan.net.git
Log | Files | Refs | Submodules | README

commit fadb3edc09050dee947a2c9b659e38008b449a0b
parent a1d26ecca1438525f377ba2d7e4d4374ea211cac
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sun, 29 Dec 2024 16:12:22 -0800

Update the December Adventure log for day 29

Diffstat:
Acontent/december-adventure/2024-29/index.md | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+), 0 deletions(-)

diff --git a/content/december-adventure/2024-29/index.md b/content/december-adventure/2024-29/index.md @@ -0,0 +1,82 @@ +--- +title: "2024, Day 29: First draft of a weeknote script" +description: "I wrote a crufty script to automate weeknote creation" +date: 2024-12-29T15:45:44-08:00 +draft: false +--- + +I finished [the Bash script I started]({{< relref +"/december-adventure/2024-28/index.md" >}}) to help automate the creation of +[the weeknotes I intent to post]({{< relref +"/december-adventure/2024-25/index.md" >}}). It works for me, for now, but +before I paste the code I want to call out the two biggest issues: + +First, it uses the non-POSIX `--date` flag to `DATE(1)`. I don't know how to +make this work on any sort of BSD or macOS and I don't know if I ever will. + +More glaringly, messing with Git branches in a specific working tree is +a really nasty hack. It would be way better to have this operate on its own +clone of my website repo but I haven't set that up. My script launches an +editor and if I get distracted and do something else to my website while +that's open (more likely than you think), I could easily mess everything up. + +It would be better to have more of this handled by a [Hugo +archetype](https://gohugo.io/content-management/archetypes/) but TBH +I haven't even given that option much attention. I just wanted to get +something together before the end of today and I did. + +```bash +#!/usr/bin/env bash +# Create or edit next Monday's weeknote + +set -eu + +HUGODIR=<path-to-hugo-root> +POSTFILE=index.md +WEEKSTR=$(date +"%G-W%V" --date="07:59 next Mon") +POSTDIR=$(echo "content/posts/weeknotes/${WEEKSTR}" | tr A-Z a-z) + +pushd "$HUGODIR" +GITCURRBRANCH=$(git branch --show-current) + +if ! grep -q "$WEEKSTR" <(git branch --list) +then + git branch "$WEEKSTR" main +fi +git switch "$WEEKSTR" + +if [[ ! -d "$POSTDIR" ]] +then + mkdir -p "$POSTDIR" +fi +pushd "$POSTDIR" + +if [[ ! -f "$POSTFILE" ]] +then + # Fill in the frontmatter + cat << HERE > "$POSTFILE" +--- +title: "Weeknote for $WEEKSTR" +description: "Things I learned or read in the last week" +date: $(date -Iseconds --date="07:59 next Mon") +draft: true +--- +HERE + + COMMITMSG="Add weeknote for $WEEKSTR" +else + COMMITMSG="Update weeknote for $WEEKSTR" +fi + +# Edit the weeknote +$EDITOR "$POSTFILE" + +# Commit the weeknote +git add "$POSTFILE" +git commit -m "$COMMITMSG" "$POSTFILE" + +# Clean up +git switch "$GITCURRBRANCH" +popd +popd +```