Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active January 3, 2023 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edsu/aa6f70bb20127b1e18e05dff5e470022 to your computer and use it in GitHub Desktop.
Save edsu/aa6f70bb20127b1e18e05dff5e470022 to your computer and use it in GitHub Desktop.
#!/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.md"
$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment