Skip to content

Commit 2be4503

Browse files
committed
feat(tusc): add --no-spin, --no-color and rename vars
1 parent a122d10 commit 2be4503

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

tusc.sh

100644100755
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare -A HEADERS # assoc headers of last request
1616
declare ISOK=0 # is last request ok?
1717

1818
# message helpers
19-
line() { echo -e "\e[${3:-0};$2m$1\e[0m"; }
19+
line() { [[ $NOCOLOR ]] && echo -e "$1" || echo -e "\e[${3:-0};$2m$1\e[0m"; }
2020
error() { line "$1" 31; if [[ ! ${2:-0} -eq 0 ]]; then exit $2; fi }
2121
ok() { line "${1:- Done}" 32; }
2222
info() { line "$1" 33; }
@@ -54,11 +54,14 @@ usage()
5454
$(info "-a --algo") $(comment "The algorigthm for key &/or checksum.")
5555
$(comment "(Eg: sha1, sha256)")
5656
$(info "-b --base-path") $(comment "The tus-server base path (Default: '/files/').")
57+
$(info "-C --no-color") $(comment "Donot color the output (Useful for parsing output).")
5758
$(info "-f --file") $(comment "The file to upload.")
5859
$(info "-h --help") $(comment "Show help information and usage.")
5960
$(info "-H --host") $(comment "The tus-server host where file is uploaded.")
6061
$(info "-L --locate") $(comment "Locate the uploaded file in tus-server.")
62+
$(info "-S --no-spin") $(comment "Donot show the spinner (Useful for parsing output).")
6163
$(info "-u --update") $(comment "Update tusc to latest version.")
64+
$(info " --version") $(comment "Print the current tusc version.")
6265
6366
$(ok Examples:)
6467
$TUSC --help # shows this help
@@ -98,9 +101,9 @@ filepart() # $1 = start_byte, $2 = byte_length, $3 = file
98101
# http request
99102
request()
100103
{
101-
echo > $HFILE
102-
[[ $DEBUG ]] && comment "> curl -sSLD $HFILE -H 'Tus-Resumable: 1.0.0' $1"
103-
BODY=$(bash -c "curl -sSLD $HFILE -H 'Tus-Resumable: 1.0.0' $1")
104+
echo > $HEADER
105+
[[ $DEBUG ]] && comment "> curl -sSLD $HEADER -H 'Tus-Resumable: 1.0.0' $1"
106+
BODY=$(bash -c "curl -sSLD $HEADER -H 'Tus-Resumable: 1.0.0' $1")
104107
HEADERS=()
105108

106109
while IFS=':' read key value; do
@@ -109,7 +112,7 @@ request()
109112
fi
110113
value="${value/ /}"
111114
HEADERS[$key]="${value%$'\r'}"
112-
done < <(cat "$HFILE")
115+
done < <(cat "$HEADER")
113116

114117
if [[ "${HEADERS[Status]}" == "20"* ]]; then ISOK=1; else ISOK=0; fi
115118

@@ -119,6 +122,7 @@ request()
119122
# show spinner and mark its pid
120123
spinner()
121124
{
125+
[[ $NOSPIN ]] && return 0
122126
do-spin &
123127
SPINID=$!
124128
disown
@@ -137,6 +141,7 @@ do-spin()
137141

138142
no-spinner()
139143
{
144+
[[ $NOSPIN ]] && return 0
140145
local PID=$SPINID
141146
SPINID=0
142147
[[ $PID -eq 0 ]] || kill $PID 2> /dev/null
@@ -146,14 +151,14 @@ no-spinner()
146151
on-exit()
147152
{
148153
no-spinner
149-
rm -f $FILE.part $HFILE0 $HFILE
154+
rm -f $FILE.part $HEADER0 $HEADER
150155
[[ $OFFSET ]] || return 0
151156

152157
OFFSET=${HEADERS[Upload-Offset]:-0} LEFTOVER=$((SIZE - OFFSET))
153158
if [[ $LEFTOVER -eq 0 ]]; then
154-
ok "Uploaded successfully!"
159+
ok "Uploaded successfully!"
155160
else
156-
error "Unfinished upload, please rerun the command to resume."
161+
error "Unfinished upload, please rerun the command to resume."
157162
fi
158163
info "URL: $TUSURL"
159164
}
@@ -163,10 +168,12 @@ while [[ $# -gt 0 ]]; do
163168
case "$1" in
164169
-a | --algo) SUMALGO="$2"; shift 2 ;;
165170
-b | --base-path) BASEPATH="$2"; shift 2 ;;
171+
-C | --no-color) NOCOLOR=1; shift ;;
166172
-f | --file) FILE="$2"; shift 2 ;;
167173
-h | --help | help) usage $1; exit 0 ;;
168174
-H | --host) HOST="$2"; shift 2 ;;
169175
-L | --locate) LOCATE=1; shift ;;
176+
-S | --no-spin) NOSPIN=1; shift ;;
170177
-u | --update) update; exit 0 ;;
171178
--version | version) version; exit 0 ;;
172179
*) if [[ $HOST ]]; then
@@ -185,15 +192,14 @@ trap on-exit EXIT
185192
SUMALGO=${SUMALGO:-sha1}
186193
[[ $SUMALGO == "sha"* ]] || error "--algo '$SUMALGO' not supported" 1
187194

188-
FILE=`realpath $FILE` NAME=`basename $FILE` SIZE=`stat -c %s $FILE` HFILE=`mktemp -t tus.XXXXXXXXXX`
189-
[[ $DEBUG ]] && info "Host: $HOST | Header: $HFILE\nFile: $NAME | Size: $SIZE"
195+
FILE=`realpath $FILE` NAME=`basename $FILE` SIZE=`stat -c %s $FILE` HEADER=`mktemp -t tus.XXXXXXXXXX`
190196

191197
# calc key &/or checksum
192-
[[ $DEBUG ]] && comment "Calculating key ..."
198+
[[ $DEBUG ]] && comment "> ${SUMALGO}sum $FILE"
193199
spinner && read -r KEY _ <<< `${SUMALGO}sum $FILE` && no-spinner
194200
195201
CHKSUM="$SUMALGO $(echo -n $KEY | base64 -w 0)"
196-
[[ $DEBUG ]] && info "Key: $KEY | Checksum: $CHKSUM"
202+
[[ $DEBUG ]] && info "HOST : $HOST\nHEADER: $HEADER\nFILE : $NAME\nSIZE : $SIZE\nKEY : $KEY\nCHKSUM: $CHKSUM"
197203
198204
# head request
199205
TUSURL=`tus-config ".[\"$KEY\"].location?"`
@@ -231,7 +237,7 @@ request "-H 'Content-Type: application/offset+octet-stream' \
231237
232238
# show spinner
233239
spinner
234-
HFILE0=$HFILE HFILE=`mktemp -t tus.XXXXXXXXXX`
240+
HEADER0=$HEADER HEADER=`mktemp -t tus.XXXXXXXXXX`
235241
while :; do
236242
[[ ${HEADERS[Upload-Offset]} -eq $SIZE ]] && exit
237243
request "--head $TUSURL" > /dev/null

0 commit comments

Comments
 (0)