#!/bin/sh # swx: share files and text from the terminal via swx.sh # # install (read this file first, always): # curl -o swx https://swx.sh/cli && less swx && chmod +x swx && mv swx ~/bin/ # # usage: # swx put [-e minutes, 1 to 60] [-p password] [-b] [-d tries] [-w url] # swx text "some text" [same flags] # swx get [-p password] [-o outfile] # swx del [delete-token] # # env: # SWX_TOKEN upload token (required for put/text, and for del without a token) # SWX_URL base url, defaults to https://swx.sh set -eu BASE="${SWX_URL:-https://swx.sh}" die() { printf '%s\n' "$1" >&2; exit 1; } command -v curl >/dev/null 2>&1 || die "swx needs curl" cmd="${1:-help}" [ $# -gt 0 ] && shift EXPIRY="" PW="" BURN="" DESTROY="" WEBHOOK="" OUT="" collect_flags() { while [ $# -gt 0 ]; do case "$1" in -e) EXPIRY="$2"; shift 2 ;; -p) PW="$2"; shift 2 ;; -b) BURN=1; shift ;; -d) DESTROY="$2"; shift 2 ;; -w) WEBHOOK="$2"; shift 2 ;; -o) OUT="$2"; shift 2 ;; *) die "unknown flag: $1" ;; esac done } do_upload() { # $1 = filename, stdin = bytes [ -n "${SWX_TOKEN:-}" ] || die "set SWX_TOKEN to upload" set -- -H "authorization: Bearer $SWX_TOKEN" -H "x-filename: $1" [ -n "$EXPIRY" ] && set -- "$@" -H "x-expiry: $EXPIRY" [ -n "$PW" ] && set -- "$@" -H "x-password: $PW" [ -n "$BURN" ] && set -- "$@" -H "x-burn: 1" [ -n "$DESTROY" ] && set -- "$@" -H "x-destroy-after-fails: $DESTROY" [ -n "$WEBHOOK" ] && set -- "$@" -H "x-webhook-url: $WEBHOOK" curl -sS "$@" --data-binary @- "$BASE/api/up" } case "$cmd" in put) [ $# -ge 1 ] || die "usage: swx put [flags]" FILE="$1"; shift [ -f "$FILE" ] || die "no such file: $FILE" collect_flags "$@" do_upload "$(basename "$FILE")" < "$FILE" ;; text) [ $# -ge 1 ] || die 'usage: swx text "something" [flags]' TEXT="$1"; shift collect_flags "$@" printf '%s' "$TEXT" | do_upload "paste.txt" ;; get) [ $# -ge 1 ] || die "usage: swx get [-p password] [-o outfile]" TARGET="$1"; shift collect_flags "$@" case "$TARGET" in http*) URL="$TARGET" ;; *) URL="$BASE/f/$TARGET" ;; esac set -- -sS -f [ -n "$PW" ] && set -- "$@" -H "x-pw: $PW" if [ -n "$OUT" ]; then curl "$@" -o "$OUT" "$URL" && printf 'saved %s\n' "$OUT" else curl "$@" -OJ "$URL" fi ;; del) [ $# -ge 1 ] || die "usage: swx del [delete-token]" ID="$1"; TOKEN="${2:-}" set -- -sS -X DELETE if [ -n "$TOKEN" ]; then set -- "$@" -H "x-delete-token: $TOKEN" else [ -n "${SWX_TOKEN:-}" ] || die "pass the delete token or set SWX_TOKEN" set -- "$@" -H "authorization: Bearer $SWX_TOKEN" fi curl "$@" "$BASE/api/v1/shares/$ID" printf '\n' ;; help|*) sed -n '2,19p' "$0" | sed 's/^# \{0,1\}//' ;; esac