Skip to content

Commit 70759c7

Browse files
committed
ksau: Use getopts, following commit will make use of this
1 parent ab79652 commit 70759c7

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

ksau

+31-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ aqua=$(echo -en "\e[36m")
1111
lightgreen=$(echo -en "\e[92m")
1212
Normal='\033[0m'
1313
Black='\033[0;30m' # Black
14-
Red='\033[0;31m' # Red
14+
Red=$'\033[0;31m' # Red
1515
Green='\033[0;32m' # Green
1616
Yellow='\033[0;33m' # Yellow
1717
Blue='\033[0;34m' # Blue
@@ -205,6 +205,9 @@ function url_encode() {
205205

206206
# rclone_progress_bar -> progress_bar -> Print dowload link.
207207
upload() {
208+
if [[ $KSAUOPT_ADD_RAND_STR == true ]]; then
209+
add_random_string "$(basename "$file")"
210+
fi
208211
echo "Initializing process, might take up to 10 seconds..."
209212
rclone_progress_bar
210213
wait
@@ -221,12 +224,17 @@ upload() {
221224

222225
help() {
223226
echo "
224-
${aqua}Usage : ksau [OPTION]... [FILE]...${normal}
227+
${aqua}Usage : ksau [-r] [OPTION]... [FILE]...${normal}
228+
229+
${aqua}Options:${normal}
230+
-r Add random string to the end of filename when uploading
231+
232+
Note that all options must be passed ${Red}BEFORE${normal} other arguments.
225233
226234
${aqua}upload [FILE] [FOLDER]${normal} : Uploads the given file to the given folder
227235
on index.
228236
229-
${aqua}upload -r [FILE] [FOLDER]${normal} : Uploads the given file to the given folder
237+
${aqua}upload [FILE] [FOLDER]${normal} : Uploads the given file to the given folder
230238
but with new filename with random strings
231239
at last (before extension if present).
232240
@@ -287,18 +295,27 @@ version() {
287295
echo "${orange}${KSAU_VERSION_STRING} ${normal}"
288296
}
289297

298+
KSAUOPT_ADD_RAND_STR=false
299+
300+
while getopts "r" opt; do
301+
case $opt in
302+
r)
303+
KSAUOPT_ADD_RAND_STR=true
304+
;;
305+
?)
306+
# Getopts already print error for us so just exit
307+
exit 1
308+
;;
309+
esac
310+
done
311+
312+
# Remove all args parsed by getopts
313+
shift $((OPTIND - 1))
314+
290315
if [[ $1 == "upload" ]]; then
291-
if [[ $2 == "-r" ]]; then
292-
shift
293-
readonly file="$2"
294-
readonly u_folder="$3"
295-
add_random_string $(basename "$file")
296-
upload
297-
else
298-
readonly file="$2"
299-
readonly u_folder="$3"
300-
upload
301-
fi
316+
readonly file="$2"
317+
readonly u_folder="$3"
318+
upload
302319
elif [[ $1 == "setup" ]]; then
303320
setup
304321
elif [[ $1 == "dependencies" ]]; then

0 commit comments

Comments
 (0)