mirror of
https://github.com/notwa/rc
synced 2024-11-04 22:49:03 -08:00
compute number of failures instead of keeping track
This commit is contained in:
parent
545ab4bb5d
commit
99b2991838
1 changed files with 13 additions and 14 deletions
27
sh/oxo
27
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
|
||||
|
|
Loading…
Reference in a new issue