< dep.smol.pub / posts

A better smolpub.sh for MacOS (without readarray)

When I first tried setting up `smolpub.sh` I was receiving an error:

readarray: command not found

Instead of installing another dependency, I updated the script to be as follows:

#!/bin/bash

host=https://smol.pub

validate_post() {
    arr=()
    while IFS= read -r line; do
        arr+=("$line")
    done < "$1"
    if [[ "$(echo ${arr[0]} | cut -b-2)" != "# " ]] || [[ "${arr[1]}" != "" ]]
    then
    echo "First line needs to start with a title (# Hello World)"
    echo "Second line needs to be left blank."
    fi
}

validate_post $1

if [ $# -eq 1 ]
then
    status=$(curl -s -o /dev/null -w "%{http_code}"\
     --form-string title="$(head -1 $1 | cut -b3-)"\
     --form-string slug="$1"\
     --form-string content="$(tail -n +3 $1)"\
     -b smolpub="$(cat ~/.config/.smolpub)"\
     $host/posts/save)

    if [ $status -ne 302 ]
    then
    curl --form-string title="$(head -1 $1 | cut -b3-)"\
         --form-string slug="$1"\
         --form-string content="$(tail -n +3 $1)"\
         -b smolpub="$(cat ~/.config/.smolpub)"\
         $host/posts/$1/update
    fi
fi

This seemed to solve my problem and unlock publishing from the MacOS command line.

I then created a little shell script so that I could post to my blog with one line:

#!/usr/bin/env bash

# Check if two arguments are provided
if [ "$#" -ne 2 ]; then
    echo "Usage: blogpost.sh <slug> <body>"
    exit 1
fi

# Assign arguments to variables for better readability
slug=/Users/me/.scripts/$1
body=$2

# Create a new file with the provided slug
touch $slug

# Write the body into the file
echo -e "# $(date +'%Y/%m/%d @ %H:%M')\n\n$body" > $slug

# Publish the post
bash smolpub.sh $slug

# Remove the file
# rm /Users/me/.scripts/$slug

Usage:

blogpost.sh my-blog-post "This is a test"

Would create a post with the title being the current date and time, and the body being the string I've passed in.