# HG changeset patch # User Paul Bissex # Date 1611107564 18000 # Tue Jan 19 20:52:44 2021 -0500 # Node ID 4f9cbc400ee4e544e2cfe53554acad353ba3c411 # Parent e85880c1ca7cc8897b672a2fef7266606f84da32 added UA header, and Authorization header based on DPASTE_API_TOKEN env var diff --git a/dpaste.sh b/dpaste.sh --- a/dpaste.sh +++ b/dpaste.sh @@ -1,18 +1,20 @@ #!/bin/sh +# getopts code adapted from http://mywiki.wooledge.org/BashFAQ/035#getopts -# Usage info show_help() { cat << HELP -Usage: $(basename $0) [-h] [-s SYNTAX] [-t TITLE] [-e EXPIRY] +Usage: $(basename "$0") [-h] [-s SYNTAX] [-t TITLE] [-e EXPIRY] Send stdin to dpaste.com, optionally setting syntax/title/expiry. Resulting item will be opened in your browser. + +If DPASTE_API_TOKEN is set in the environment, it will be used +for authentication. HELP } -# Process the options -# (getopts code adapted from http://mywiki.wooledge.org/BashFAQ/035#getopts) -params="" -# Trailing colon on option name means it expects a value +# Set User-Agent header, per dpaste.com TOS +params="-A 'dpaste.sh'" + while getopts hs:t:e: opt; do case $opt in h) @@ -33,14 +35,21 @@ done shift "$((OPTIND-1))" -echo "Paste your content now, ^D to submit.\n---" +echo "Paste your content now, ^D to submit." -# make the API call -url=$(curl -s -F "content=<-" $params http://dpaste.com/api/v2/) +# Make the API call, passing stdin to 'content' param +if [ -z $DPASTE_API_TOKEN ] +then + url=$(curl -s -F "content=<-" $params http://dpaste.com/api/v2/) +else + echo "(Using DPASTE_API_TOKEN from environment)" + url=$(curl -s -H "Authorization: Bearer $DPASTE_API_TOKEN" -F "content=<-" $params https://dpaste.com/api/v2/) +fi + echo "---" echo $url -# open in browser +# Open in browser if [ -x "$(command -v xdg-open)" ]; then xdg-open $url