From 99b2991838b3d5b3618fea5e2d098ec2f109e39f Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 16 Sep 2021 22:48:20 -0700 Subject: [PATCH] compute number of failures instead of keeping track --- sh/oxo | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/sh/oxo b/sh/oxo index 90b40d9..6226898 100755 --- a/sh/oxo +++ b/sh/oxo @@ -35,31 +35,31 @@ oxo() { ### @- ### oxo: successfully uploaded 2 files ### oxo: failed to upload 2 files ### ``` - local succs=0 - local fails=0 - local f= s= local look='\033[1m%s\033[0m\n' local bad='\033[31m%s\033[0m\n' local warn='\033[1;33m%s\033[0m\n' local err='\033[1;31m%s\033[0m\n' + local hits=0 total=0 + local f= s= + if [ $# -eq 0 ]; then - curl -F'file=@-' https://0x0.st && : $((succs+=1)) || : $((fails+=1)) + curl -F'file=@-' https://0x0.st && : $((hits+=1)) || true + total=1 elif [ $# -gt 255 ]; then printf "$err" "oxo: too many arguments (max: 255)" >&2 return 255 + else + total=$# fi for f; do if [ ! -e "$f" ]; then - : $((fails+=1)) printf "$bad" "oxo: no such file: $f" >&2 elif [ -d "$f" ]; then - : $((fails+=1)) printf "$bad" "oxo: skipping directory: $f" >&2 elif [ ! -s "$f" ]; then - : $((fails+=1)) printf "$bad" "oxo: skipping empty file: $f" >&2 else local g="$f" @@ -70,7 +70,6 @@ oxo() { ### @- g=/tmp/oxo #printf "$warn" "oxo: special characters found: $f" >&2 if ! cp -p -- "$f" "$g"; then - : $((fails+=1)) printf "$bad" "oxo: failed to copy file: $f" >&2 continue fi @@ -79,7 +78,6 @@ oxo() { ### @- local kib="$(du -k -- "$f" | cut -f1)" if [ "$kib" -gt 524288 ]; then - : $((fails+=1)) printf "$bad" "oxo: file too large: $f" >&2 continue fi @@ -93,19 +91,20 @@ oxo() { ### @- local r=$(( (d*-335+16384)/32768+30 )) if ! curl -F"file=@\"$g\"" https://0x0.st; then - : $((fails+=1)) printf "$bad" "oxo: failed to upload file: $f" >&2 else - : $((succs+=1)) + : $((hits+=1)) printf "$look" "oxo: expires in $r days: $f" >&2 fi fi done - if [ $succs -gt 0 ]; then + local fails=$((total-hits)) + + if [ $hits -gt 0 ]; then [ $fails -eq 0 ] && fmt="$look" || fmt="$warn" - [ $succs -eq 1 ] && s= || s=s - printf "$fmt" "oxo: successfully uploaded $succs file$s" >&2 + [ $hits -eq 1 ] && s= || s=s + printf "$fmt" "oxo: successfully uploaded $hits file$s" >&2 fi if [ $fails -gt 0 ]; then