added UA header, and Authorization header based on DPASTE_API_TOKEN env var
1 files changed, 19 insertions(+), 10 deletions(-)

M dpaste.sh
M dpaste.sh +19 -10
@@ 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 @@ while getopts hs:t:e: opt; do
 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