This morning I saw a post about how to create a Mastodon status update on the command line using curl. Kinda neat right?!

It got me thinking that it could be fun to add a little bit of script around it that allowed you to create the post in your configured $EDITOR, kind of like how git will open your editor of choice when you make a commit.

I don’t foresee wanting to read and reply to posts in the terminal (I would miss the images in the fediverse too much). But similar to my approach to jounaling I thought it might be useful to be able to quickly jot down a post, without getting sucked into reading my timeline, etc.

Here’s what it looks like in action:

To try it out put this little script in your PATH (I called mine “post”) and then set the environment variables (EDITOR, MASTODON_POST_HOST, MASTODON_POST_TOKEN) for it to use:

#!/bin/bash

# Set these environment variables and you can create a (text-only) post using
# your favorite command line text editor.
#
# - EDITOR: e.g. vim, emacs, etc
# - MASTODON_POST_HOST: the hostname for our Mastodon account, e.g. chaos.social
# - MASTODON_POST_TOKEN: an app access key with write:statuses permission
#
# See: https://gist.github.com/edsu/aa6f70bb20127b1e18e05dff5e470022

if [[ -z "${EDITOR}" ]]; then
  echo "⚠️  Please set EDITOR to something (e.g. vim, emacs) in your environment"
  exit
fi

if [[ -z "${MASTODON_POST_HOST}" ]]; then
  echo "⚠️  Please set MASTODON_POST_HOST in your environment"
  exit
fi

if [[ -z "${MASTODON_POST_TOKEN}" ]]; then
  echo "⚠️  Please set MASTODON_POST_TOKEN in your environment, get one by creating an app at https://$MASTODON_POST_HOST/settings/applications"
  exit
fi

post_dir=`mktemp -dt "mastodon-post.XXXXXXXXXX"`
post_file="$post_dir/post.txt"

$EDITOR $post_file

if [[ ! -f $post_file ]]; then
  echo "🛑 cancelled posting..."
  exit
fi

curl --silent --header "Authorization: Bearer ${MASTODON_POST_TOKEN}" https://${MASTODON_POST_HOST}/api/v1/statuses --form "status=<${post_file}" > /dev/null

if [[ $? -eq 0 ]]; then
  echo "🦣 post sent!"
else
  echo "😟 post failed" 
fi

It’s also available in this gist where it might see some updates. It’s probably complicated enough to warrant being written in $another_programming_language, but I know there are other fully functional terminal apps like toot and tut for anything more complicated than sending off a quick post.