#!/bin/bash set -euo pipefail # sos upload # example: # sos upload tools.dropshell.app file:latest ./file.txt # this will upload the file to the server, and return the download URL function show_help() { cat << EOF sos is a script to upload files to a simple object storage server. Usage: sos upload Example: sos upload tools.dropshell.app file:latest ./file.txt This will upload the file to the server, and return the download URL. Environment variables: SOS_WRITE_TOKEN: The write token to use for the upload. If not set, the script will look in $HOME/.config/sos/write_token.txt. EOF exit 0 } function die() { echo "FATAL:" echo "$@" exit 1 } function datetime() { date -u +"%Y.%m%d.%H%M" } function upload() { if [ "$#" -ne 3 ]; then echo "Usage: sos upload " exit 1 fi server=$1 label=$2 file=$3 [ -f "$file" ] || die "File not found: $file" # upload the file TARGET_URL="https://$server/upload" echo "Uploading $file to $TARGET_URL" DATETIME=$(datetime) # if the label doesn't have a tag, add :lastest [[ "$label" =~ : ]] || label="$label:latest" label_base=$(echo "$label" | cut -d':' -f1) LABELTAGS=( "$label" "$label_base:$DATETIME" "$label_base:latest" ) # deduplicate the labeltags mapfile -t LABELTAGS < <(printf "%s\n" "${LABELTAGS[@]}" | sort -u) LABELTAGS_JSON=$(printf '"%s",' "${LABELTAGS[@]}") LABELTAGS_JSON="[${LABELTAGS_JSON%,}]" METADATA_JSON=$(cat < $FILENAME" echo "Alternative: https://$server/$HASH > $FILENAME" } # if no arguments, show help [ "$#" -gt 0 ] || show_help CMD="$1" shift if [ "$CMD" == "upload" ]; then upload "$@" exit $? fi die "Unknown command: $CMD"