dotfiles

A steaming hot pile of sh...ell scripts and configuration files.
Log | Files | Refs

commit bf3e8ea43edce90ae3831bb6fd530f56ea6c2d11
parent 15ca93a889b0b59269925b50af884771452bd15b
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Wed,  6 Dec 2023 18:46:52 +0200

Add a -u / --url flag and fix some flag handling.

Diffstat:
Mbin/0x0.st | 60++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 44 insertions(+), 16 deletions(-)

diff --git a/bin/0x0.st b/bin/0x0.st @@ -9,24 +9,29 @@ fi help_message () { printf "Usage: -\t%s [--debug] -p / --primay -\t%s [--debug] -s / --secondary -\t%s [--debug] -b / --clipboard -\t%s [--debug] [FILENAME] +\t%s [--debug] -p, --primay +\t%s [--debug] -s, --secondary +\t%s [--debug] -b, --clipboard +\t%s [--debug] -u URL, --url URL +\t%s [--debug] FILENAME Flags: -p, --primary Operate on the PRIMARY selection. -s, --secondary Operate on the SECONDARY selection. -b, --clipboard Operate on the CLIPBOARD selection. + -u, --url Tell 0x0.st to mirror the file at the URL. --debug Show debug information and do not upload to the 0x0.st. " "$0" "$0" "$0" "$0" # XXX: That's a dumb looking way of providing # this one "$0" argument many times. } -# https://www.baeldung.com/linux/bash-parse-command-line-arguments -OUR_ARGUMENTS="$(getopt -o hpsb --long help,debug,primary,secondary,clipboard -- "$@" )" +print_provide_one_and_only_one_flag_message_flag () { + printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard, -u / --url flags.\n" +} +# https://www.baeldung.com/linux/bash-parse-command-line-arguments +OUR_ARGUMENTS="$(getopt -o hpsbu: --long help,debug,primary,secondary,clipboard,url: -- "$@" )" eval set -- "$OUR_ARGUMENTS" while [ -n "$*" ]; do case "$1" in @@ -39,41 +44,58 @@ while [ -n "$*" ]; do debug_flag=1 shift ;; - -p | primary) + -p | --primary) xsel_flag_primary="$1" - if [ -n "$xsel_flag_secondary" ] || [ -n "$xsel_flag_clipboard" ]; then - printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard flags.\n" + if [ -n "$xsel_flag_secondary" ] || [ -n "$xsel_flag_clipboard" ] || [ -n "$url_flag" ]; then + print_provide_one_and_only_one_flag_message + help_message exit 1 fi shift ;; -s | --secondary) xsel_flag_secondary="$1" - if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_clipboard" ]; then - printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard flags.\n" + if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_clipboard" ] || [ -n "$url_flag" ]; then + print_provide_one_and_only_one_flag_message + help_message exit 1 fi shift ;; - -b | clipboard) + -b | --clipboard) xsel_flag_clipboard="$1" - if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_secondary" ]; then - printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard flags.\n" + if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_secondary" ] || [ -n "$url_flag" ]; then + print_provide_one_and_only_one_flag_message + help_message exit 1 fi shift ;; + -u | --url) + url_flag="$1" + if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_secondary" ] || [ -n "$xsel_flag_clipboard" ]; then + print_provide_one_and_only_one_flag_message + help_message + exit 1 + fi + target_url="$2" + shift 2 + ;; --) shift filename="$1" - if [ -z "$filename$xsel_flag_primary$xsel_flag_secondary$xsel_flag_clipboard" ]; then - printf "Either provide a clipboard flag or a filename:\n\n" + if [ -z "$filename$xsel_flag_primary$xsel_flag_secondary$xsel_flag_clipboard$url_flag" ]; then + print_provide_one_and_only_one_flag_message help_message exit 1 fi xsel_flag="$xsel_flag_primary$xsel_flag_secondary$xsel_flag_clipboard" break ;; + *) + printf "Something is wrong." 1>&2 + exit 1 + ;; esac done @@ -96,6 +118,12 @@ if [ -n "$xsel_flag" ]; then else curl -F"file=@${temp_file}" 0x0.st fi +elif [ -n "$url_flag" ]; then + if [ -n "$debug_flag" ]; then + curl "$target_url" + else + curl -F"url=$target_url" 0x0.st + fi else if [ -n "$debug_flag" ]; then cat "$filename"