index.md (2282B)
1 --- 2 title: "2024, Day 29: First draft of a weeknote script" 3 description: "I wrote a crufty script to automate weeknote creation" 4 date: 2024-12-29T15:45:44-08:00 5 draft: false 6 --- 7 8 I finished [the Bash script I started]({{< relref 9 "/december-adventure/2024-28/index.md" >}}) to help automate the creation of 10 [the weeknotes I intent to post]({{< relref 11 "/december-adventure/2024-25/index.md" >}}). It works for me, for now, but 12 before I paste the code I want to call out the two biggest issues: 13 14 First, it uses the non-POSIX `--date` flag to `DATE(1)`. I don't know how to 15 make this work on any sort of BSD or macOS and I don't know if I ever will. 16 17 More glaringly, messing with Git branches in a specific working tree is 18 a really nasty hack. It would be way better to have this operate on its own 19 clone of my website repo but I haven't set that up. My script launches an 20 editor and if I get distracted and do something else to my website while 21 that's open (more likely than you think), I could easily mess everything up. 22 23 It would be better to have more of this handled by a [Hugo 24 archetype](https://gohugo.io/content-management/archetypes/) but TBH 25 I haven't even given that option much attention. I just wanted to get 26 something together before the end of today and I did. 27 28 ```bash 29 #!/usr/bin/env bash 30 # Create or edit next Monday's weeknote 31 32 set -eu 33 34 HUGODIR=<path-to-hugo-root> 35 POSTFILE=index.md 36 WEEKSTR=$(date +"%G-W%V" --date="07:59 next Mon") 37 POSTDIR=$(echo "content/posts/weeknotes/${WEEKSTR}" | tr A-Z a-z) 38 39 pushd "$HUGODIR" 40 GITCURRBRANCH=$(git branch --show-current) 41 42 if ! grep -q "$WEEKSTR" <(git branch --list) 43 then 44 git branch "$WEEKSTR" main 45 fi 46 git switch "$WEEKSTR" 47 48 if [[ ! -d "$POSTDIR" ]] 49 then 50 mkdir -p "$POSTDIR" 51 fi 52 pushd "$POSTDIR" 53 54 if [[ ! -f "$POSTFILE" ]] 55 then 56 # Fill in the frontmatter 57 cat << HERE > "$POSTFILE" 58 --- 59 title: "Weeknote for $WEEKSTR" 60 description: "Things I learned or read in the last week" 61 date: $(date -Iseconds --date="07:59 next Mon") 62 draft: true 63 --- 64 HERE 65 66 COMMITMSG="Add weeknote for $WEEKSTR" 67 else 68 COMMITMSG="Update weeknote for $WEEKSTR" 69 fi 70 71 # Edit the weeknote 72 $EDITOR "$POSTFILE" 73 74 # Commit the weeknote 75 git add "$POSTFILE" 76 git commit -m "$COMMITMSG" "$POSTFILE" 77 78 # Clean up 79 git switch "$GITCURRBRANCH" 80 popd 81 popd 82 ```