Add a recursive option to consumedir

This commit is contained in:
Eike Kettner 2020-06-27 01:14:34 +02:00
parent 9d1717f0e0
commit d13e0a4370

View File

@ -20,8 +20,8 @@ if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
exit 1 exit 1
fi fi
OPTIONS=omhdp:v OPTIONS=omhdp:vr
LONGOPTS=once,distinct,help,delete,path:,verbose LONGOPTS=once,distinct,help,delete,path:,verbose,recursive,dry
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
@ -34,7 +34,7 @@ fi
eval set -- "$PARSED" eval set -- "$PARSED"
declare -a watchdir declare -a watchdir
help=n verbose=n delete=n once=n distinct=n help=n verbose=n delete=n once=n distinct=n recursive=n dryrun=n
while true; do while true; do
case "$1" in case "$1" in
-h|--help) -h|--help)
@ -61,6 +61,14 @@ while true; do
distinct=y distinct=y
shift shift
;; ;;
-r|--recursive)
recursive=y
shift
;;
--dry)
dryrun=y
shift
;;
--) --)
shift shift
break break
@ -85,6 +93,8 @@ showUsage() {
echo " -h | --help Prints this help text. (value: $help)" echo " -h | --help Prints this help text. (value: $help)"
echo " -m | --distinct Optional. Upload only if the file doesn't already exist. (value: $distinct)" echo " -m | --distinct Optional. Upload only if the file doesn't already exist. (value: $distinct)"
echo " -o | --once Instead of watching, upload all files in that dir. (value: $once)" echo " -o | --once Instead of watching, upload all files in that dir. (value: $once)"
echo " -r | --recursive Traverse the directory(ies) recursively (value: $recursive)"
echo " --dry Do a 'dry run', not uploading anything only printing to stdout (value: $dryrun)"
echo "" echo ""
echo "Arguments:" echo "Arguments:"
echo " A list of URLs to upload the files to." echo " A list of URLs to upload the files to."
@ -126,20 +136,24 @@ info() {
} }
upload() { upload() {
tf=$($MKTEMP_CMD) rc=0 if [ "$dryrun" = "y" ]; then
$CURL_CMD -# -o "$tf" --stderr "$tf" -w "%{http_code}" -XPOST -F file=@"$1" "$2" | (2>&1 1>/dev/null grep 200) info "Not uploading (dry-run) $1 to $2"
rc=$(expr $rc + $?)
cat $tf | (2>&1 1>/dev/null grep '{"success":true')
rc=$(expr $rc + $?)
if [ $rc -ne 0 ]; then
info "Upload failed. Exit code: $rc"
cat "$tf"
echo ""
rm "$tf"
return $rc
else else
rm "$tf" tf=$($MKTEMP_CMD) rc=0
return 0 $CURL_CMD -# -o "$tf" --stderr "$tf" -w "%{http_code}" -XPOST -F file=@"$1" "$2" | (2>&1 1>/dev/null grep 200)
rc=$(expr $rc + $?)
cat $tf | (2>&1 1>/dev/null grep '{"success":true')
rc=$(expr $rc + $?)
if [ $rc -ne 0 ]; then
info "Upload failed. Exit code: $rc"
cat "$tf"
echo ""
rm "$tf"
return $rc
else
rm "$tf"
return 0
fi
fi fi
} }
@ -195,13 +209,21 @@ process() {
if [ "$once" = "y" ]; then if [ "$once" = "y" ]; then
info "Uploading all files in '$watchdir'." info "Uploading all files in '$watchdir'."
MD="-maxdepth 1"
if [ "$recursive" = "y" ]; then
MD=""
fi
for dir in "${watchdir[@]}"; do for dir in "${watchdir[@]}"; do
for file in "$dir"/*; do find "$dir" $MD -type f -print0 | while IFS= read -d '' -r file; do
process "$file" process "$file"
done done
done done
else else
$INOTIFY_CMD -m "${watchdir[@]}" -e close_write -e moved_to | REC=""
if [ "$recursive" = "y" ]; then
REC="-r"
fi
$INOTIFY_CMD $REC -m "${watchdir[@]}" -e close_write -e moved_to |
while read path action file; do while read path action file; do
trace "The file '$file' appeared in directory '$path' via '$action'" trace "The file '$file' appeared in directory '$path' via '$action'"
sleep 1 sleep 1