#!/bin/sh # lbin - CLI client for bin.liminal.cafe # Friendly temporary file hosting service set -e LBIN_URL="${LBIN_URL:-https://bin.liminal.cafe}" LBIN_AUTH="${LBIN_AUTH:-}" DEBUG=0 usage() { cat <<'EOF' lbin - CLI client for bin.liminal.cafe USAGE: lbin Upload file directly lbin Upload with specific mode command | lbin [options] Upload from stdin COMMANDS: f, file Upload a file and get a link o, oneshot Upload a file with one-time use link u, url Generate a shortened URL ou, oneshot_url Generate a one-time use shortened URL r, remote Paste file from remote URL - Read from stdin (default: file) OPTIONS: -h, --help Show this help message -o, --oneshot When reading stdin, use oneshot mode -d, --debug Show curl command and full response EXAMPLES: lbin example.txt lbin f example.txt lbin o secret.txt lbin u https://example.com/very/long/path echo "hello" | lbin cat script.sh | lbin -o ENVIRONMENT: LBIN_URL Override base URL (default: https://bin.liminal.cafe) LBIN_AUTH Authorization token (if required) TAB COMPLETION: Source the completion file for bash/zsh tab completion: . lbin-completion.bash # for bash . lbin-completion.zsh # for zsh ~byakuren ^ ^ =(o.o)= EOF } die() { printf 'lbin: %s\n' "$1" >&2 exit 1 } do_upload() { field="$1" value="$2" curl_args="-sS" if [ "$DEBUG" = 1 ]; then curl_args="-v" fi if [ -n "$LBIN_AUTH" ]; then if [ "$DEBUG" = 1 ]; then printf 'DEBUG: curl %s -F "%s=%s" -H "Authorization: %s" "%s"\n' \ "$curl_args" "$field" "$value" "$LBIN_AUTH" "$LBIN_URL" >&2 fi response=$(curl $curl_args -F "$field=$value" -H "Authorization: $LBIN_AUTH" "$LBIN_URL" 2>&1) else if [ "$DEBUG" = 1 ]; then printf 'DEBUG: curl %s -F "%s=%s" "%s"\n' \ "$curl_args" "$field" "$value" "$LBIN_URL" >&2 fi response=$(curl $curl_args -F "$field=$value" "$LBIN_URL" 2>&1) fi if [ "$DEBUG" = 1 ]; then printf 'DEBUG: response:\n%s\n' "$response" >&2 fi # Check for unauthorized response if [ "$response" = "unauthorized" ]; then die "unauthorized - set LBIN_AUTH environment variable with your token" fi printf '%s\n' "$response" } upload_file() { file="$1" mode="${2:-file}" [ -f "$file" ] || die "file not found: $file" [ -r "$file" ] || die "cannot read file: $file" do_upload "$mode" "@$file" } upload_stdin() { mode="${1:-file}" # Read stdin into a temp file with .txt extension for proper rendering tmp=$(mktemp -t lbin).txt trap 'rm -f "$tmp"' EXIT cat > "$tmp" do_upload "$mode" "@$tmp" } upload_url() { url="$1" mode="${2:-url}" case "$url" in http://*|https://*) do_upload "$mode" "$url" ;; *) die "invalid URL: $url (must start with http:// or https://)" ;; esac } # Parse global options first while [ $# -gt 0 ]; do case "$1" in -d|--debug) DEBUG=1 shift ;; *) break ;; esac done # Handle stdin if no args and stdin is not a tty if [ $# -eq 0 ]; then if [ -t 0 ]; then usage exit 0 else upload_stdin "file" exit 0 fi fi # Parse arguments case "$1" in -h|--help|help) usage exit 0 ;; -o|--oneshot) if [ -t 0 ]; then die "oneshot mode requires stdin input or a file argument" fi upload_stdin "oneshot" ;; f|file) [ -n "$2" ] || die "file command requires a filename" upload_file "$2" "file" ;; o|oneshot) [ -n "$2" ] || die "oneshot command requires a filename" upload_file "$2" "oneshot" ;; u|url) [ -n "$2" ] || die "url command requires a URL" upload_url "$2" "url" ;; ou|oneshot_url) [ -n "$2" ] || die "oneshot_url command requires a URL" upload_url "$2" "oneshot_url" ;; r|remote) [ -n "$2" ] || die "remote command requires a URL" upload_url "$2" "remote" ;; -) # Explicit stdin shift mode="file" if [ "$1" = "-o" ] || [ "$1" = "--oneshot" ]; then mode="oneshot" fi upload_stdin "$mode" ;; -*) die "unknown option: $1 (see --help)" ;; *) # If it's a file, upload it; otherwise show appropriate error if [ -e "$1" ]; then if [ -f "$1" ]; then upload_file "$1" "file" else die "not a regular file: $1" fi else die "file not found: $1" fi ;; esac