From 15c85716b2c22cd7aac616d1b46756a5736b6d52 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Sun, 9 Apr 2023 15:44:31 -0400 Subject: Create a bash script to publish gemlogs This script and configuration enables users to simply publish a gemtext file and modify their index file with the link. The readme.txt goes into how this will work. Error codes are also noted. The basic usage is built around how my capsule is laid out - so I have an index file at: gemini://senders.io/gemlog/ that uses a descending post order. This is useful for folks so the most recent post is at the top. But index files can be disabled - but if you're doing that then you're basically just using scp :) which I'd say write your own bash script for! --- config.sh | 12 +++++ gempost.sh | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.txt | 79 +++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 config.sh create mode 100755 gempost.sh create mode 100644 readme.txt diff --git a/config.sh b/config.sh new file mode 100644 index 0000000..faefbb1 --- /dev/null +++ b/config.sh @@ -0,0 +1,12 @@ +GEMPOST_SSH_HOST= +GEMPOST_REMOTE_PATH= + +# Optional configs +# GEMPOST_REMOTE_POST_PROCESS= + +# 0 disabled | 1 enabled +GEMPOST_INDEX_ON=0 +GEMPOST_REMOTE_INDEX_FILE= +GEMPOST_INDEX_LINK_PREFIX= +## This will be defaulted to ~/.local/share/gempost/index-previous-entry.txt +# GEMPOST_PREVIOUS_TITLE_FILE= diff --git a/gempost.sh b/gempost.sh new file mode 100755 index 0000000..1183da9 --- /dev/null +++ b/gempost.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +set -e +HELP=" +Usage: ./gempost.sh +Example: + ./gempost.sh Docs/gemlog/hello-world.gmi +See gempost(1) for details" +DETAILS=" +To use gempost you need a few things: + +1. ssh setup +2. a configuration with SSH setup: + GEMPOST_SSH_HOST=user@gemserver.space + GEMPOST_REMOTE_PATH=/var/gemlog/ + GEMPOST_INDEX_ON=1 + GEMPOST_INDEX_FILE=/var/gemlog/index.gmi + GEMPOST_PREVIOUS_FILE=index-previous-entry.txt + This config will default into ~/.config/gempost/ + But can be overwritten with the environment variable GEMPOST_HOME + If index is enabled it will default to the file shown above in your GEMPOST_HOME +3. sed +" +# Invalid arguments - Missing file to post +ERR_MISSING_TARGET_FILE=2 +# Supplied file is not found +ERR_TARGET_FILE_NOT_FOUND=3 +# No configuration file in expected locations +ERR_CONFIG_FILE_NOT_FOUND=4 +# GEMPOST_INDEX_ON enabled but the previous title file supplied is empty +ERR_PREV_TITLE_EMPTY=5 +# GEMPOST_INDEX_ON enabled but no previous title file found in expected locations +ERR_PREV_TITLE_FILE_NOT_FOUND=6 + +function init_config() { + if [[ -n $GEMPOST_HOME ]]; then + if [[ -f $GEMPOST_HOME/config.sh ]]; then + source $GEMPOST_HOME/config.sh + return; + fi + fi + if [[ -n $XDG_CONFIG_HOME ]]; then + if [[ -f $XDG_CONFIG_HOME/gempost/config.sh ]]; then + source $XDG_CONFIG_HOME/gempost/config.sh + return; + fi + fi + if [[ -f $HOME/.config/gempost/config.sh ]]; then + source $HOME/.config/gempost/config.sh + return + fi + echo "Unable to find gempost.conf - please see help for details..." >&2 + exit $ERR_CONFIG_FILE_NOT_FOUND; +} + +init_prev_title() { + if [[ -f $GEMPOST_PREVIOUS_TITLE_FILE ]]; then + PREV_TITLE=$(cat ${GEMPOST_PREVIOUS_TITLE_FILE}) + if [[ -n ${PREV_TITLE} ]]; then + PREV_TITLE_FILE=$GEMPOST_PREVIOUS_TITLE_FILE + return; + else + echo "GEMPOST_INDEX_ON is enabled but $GEMPOST_PREVIOUS_TITLE_FILE is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, subsequent posts will properly manage this for you." >&2 + exit $ERR_PREV_TITLE_EMPTY; + fi + fi + if [[ -f $XDG_DATA_HOME/gempost/previous-index-title.txt ]]; then + PREV_TITLE=$(cat $XDG_DATA_HOME/gempost/previous-index-title.txt) + if [[ -n $PREV_FILE ]]; then + PREV_TITLE_FILE=$XDG_DATA_HOME/gempost/previous-index-title.txt + return; + else + echo "GEMPOST_INDEX_ON is enabled but $XDG_DATA_HOME/gempost/previous-index-title.txt is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, sunsequent posts will properly manage this for you." >&2 + exit $ERR_PREV_TITLE_EMPTY; + fi + fi + if [[ -f $HOME/.local/share/gempost/previous-index-title.txt ]]; then + PREV_TITLE=$(cat $HOME/.local/share/gempost/previous-index-title.txt) + if [[ -n $PREV_TITLE ]]; then + PREV_TITLE_FILE=$HOME/.local/share/gempost/previous-index-title.txt + return; + else + echo "GEMPOST_INDEX_ON is enabled but $HOME/.local/share/gempost/previous-index-title.txt is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, sunsequent posts will properly manage this for you." >&2 + exit $ERR_PREV_TITLE_EMPTY; + fi + fi + echo "GEMPOST_INDEX_ON but no previous index title file found. Please see help for details..." >&2 + exit $ERR_PREV_TITLE_FILE_NOT_FOUND; +} + + +function main() { + # Read parameters + if [[ $# -eq 0 ]]; then + echo "Missing arguments + ${HELP}" >&2 + exit $ERR_MISSING_ARGUMENTS; + fi + + # init target file + TARGET_FILE=$1 + if [[ ! -f $TARGET_FILE ]]; then + echo "TARGET_FILE $TARGET_FILE not found" >&2 + exit $ERR_TARGET_FILE_NOT_FOUND + fi + + + init_config; + if [[ $GEMPOST_INDEX_ON -gt 0 ]]; then + # populates $INDEX_TITLE + init_prev_title; + if [[ $# -eq 2 ]]; then + LINK_DESC=$2 + fi + fi + + # Grab the name of the file + TARGET_FILE_NAME=$(basename $TARGET_FILE) + # Send the file to the remote server + echo "Posting file: ${TARGET_FILE_NAME}..." + scp $TARGET_FILE "${GEMPOST_SSH_HOST}:${GEMPOST_REMOTE_PATH}/${TARGET_FILE_NAME}" + echo "File posted" + if [[ $GEMPOST_INDEX_ON -gt 0 ]]; then + echo "Updating index file" + + if [[ -n $LINK_DESC ]]; then + NEW_INDEX_LINE="=> ${GEMPOST_INDEX_LINK_PREFIX}/${TARGET_FILE_NAME} ${LINK_DESC}" + else + NEW_INDEX_LINE="=> ${GEMPOST_INDEX_LINK_PREFIX}/${TARGET_FILE_NAME}" + fi + echo "Modifying remote index file..." + ssh $GEMPOST_SSH_HOST \ + "sed -i '/${PREV_TITLE}/i ${NEW_INDEX_LINE}' ${GEMPOST_REMOTE_INDEX_FILE}" + echo ${TARGET_FILE_NAME} > ${PREV_TITLE_FILE} + echo "Index file updated!" + fi + + if [[ -n $GEMPOST_REMOTE_POST_PROCESS ]]; then + ssh $GEMPOST_SSH_HOST \ + "${GEMPOST_REMOTE_POST_PROCESS}" + echo "Done running remote post process" + fi +} + +main "$@"; +echo "Completed successfully" + diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..f8fd8af --- /dev/null +++ b/readme.txt @@ -0,0 +1,79 @@ +gempost.sh +========== + +gempost.sh is a simple bash script for "publishing" gemini posts. + +usage +----- + +To publish a singel file you can simply run: + ./gempost.sh + +If you're using the index file mode you can optionally add a link description with: + ./gempost.sh + +It will based on your configuration: + +1. upload the file to your server +2. modify an indexfile if configured +3. run any remote post processing (like update your rss feed!) + +config +------ + +There is a "complete" sample config.sh file in the repository. +It will look for configs in: + 1. GEMPOST_CONFIG_HOME + 2. $XDG_CONFIG_HOME/gempost/config.sh + 3. $HOME/.config/gempost/config.sh + +Then it will simply source the files making the values available. + +The available configurations are: + + - GEMPOST_SSH_HOST + - the ssh host you wish to publish to - this is used as the entrypoint for both: scp and ssh + - scp $FILE $GEMPOST_SSH_HOST:$GEMPOST_REMOTE_PATH/$FILE_NAME + - ssh $GEMPOST_SSH_HOST "${GEMPOST_REMOTE_POST_PROCESS}" + - GEMPOST_REMOTE_PATH + - shown above - this will be where we publish the file to + - **NOTE** All paths get "realized" locally - so ~ or $HOME will be your LOCAL HOME FOLDER - use absolute paths to be safe! + - GEMPOST_REMOTE_POST_PROCESS + - This optional config is a remote ssh command + - GEMPOST_INDEX_ON + - 0 for disabled, 1 for enabled + - This enables the index update logic + - GEMPOST_REMOTE_INDEX_FILE + - The absolute path to the remote index file you wish to update + - GEMPOST_INDEX_LINK_PREFIX + - This will be the actual link prefix so "/gemlog" for example + - then we'll append ${GEMPOST_INDEX_LINK_PREFIX}/${FILE_NAME} + - GEMPOST_PREVIOUS_TITLE_FILE + - In order to properly prepend our link to the index file we need a target location we use the previous post as a target + +The previous title file is stored in one of: + 1. GEMPOST_PREVIOUS_TITLE_FILE + 2. $XDG_DATA_HOME/gempost/previous-index-title.txt + 3. $HOME/.local/share/gempost/previous-index-title.txt + +After the index file is update it'll put the current file name into this file to be used as the next marker. + +errors +------ + +There are a few errors that can occur (mostly around setups) - We don't trap remote process errors or scp/ssh errors - those will exit and usuall print out an error. + +But we have a few error codes in the file: + + # Invalid arguments - Missing file to post + ERR_MISSING_TARGET_FILE=2 + # Supplied file is not found + ERR_TARGET_FILE_NOT_FOUND=3 + # No configuration file in expected locations + ERR_CONFIG_FILE_NOT_FOUND=4 + # GEMPOST_INDEX_ON enabled but the previous title file supplied is empty + ERR_PREV_TITLE_EMPTY=5 + # GEMPOST_INDEX_ON enabled but no previous title file found in expected locations + ERR_PREV_TITLE_FILE_NOT_FOUND=6 + +An error will be written out for any of these, but if you're scripting around it these will be the error codes. -- cgit v1.2.3-54-g00ecf