Skip to content

Commit

Permalink
[FEATURE] Support for sub-folders with --create-dir flag. | Resolve #155
Browse files Browse the repository at this point in the history
  • Loading branch information
Akianonymus committed Jul 14, 2022
1 parent 9000ac8 commit 968a27c
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 9 deletions.
9 changes: 9 additions & 0 deletions release/bash/gsync
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ return 0
_print_center_quiet(){
{ [ $# = 3 ]&&printf "%s\n" "$2";}||{ printf "%s%s\n" "$2" "$3";}
}
_split(){
set -f
old_ifs_split=$IFS
IFS=$2
set -- $1
printf '%s\n' "$@"
IFS=$old_ifs_split
set +f
}
_support_ansi_escapes(){
unset ansi_escapes
case "${TERM:-}" in
Expand Down
21 changes: 19 additions & 2 deletions release/bash/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ export DELETE_ACCOUNT_NAME="${2}" && _parser_shift
EOF
_parser_setup_flag "-c -C --create-dir" 1 required "foldername"
_parser_setup_flag_help \
"Option to create directory. Will print folder id. Can be used to provide input folder, see README."
"Option to create directory on drive. Will print folder id. If this option is used, then input files/folders are optional.
Also supports specifying sub folders, -c 'Folder1/folder2/test'.
Three folders will be created, test inside folder2, folder2 inside Folder1 and so on.
Input files and folders will be uploaded inside test folder."
_parser_setup_flag_preprocess 4<<'EOF'
unset FOLDERNAME
EOF
Expand Down Expand Up @@ -1201,6 +1205,15 @@ return 0
_print_center_quiet(){
{ [ $# = 3 ]&&printf "%s\n" "$2";}||{ printf "%s%s\n" "$2" "$3";}
}
_split(){
set -f
old_ifs_split=$IFS
IFS=$2
set -- $1
printf '%s\n' "$@"
IFS=$old_ifs_split
set +f
}
_support_ansi_escapes(){
unset ansi_escapes
case "${TERM:-}" in
Expand Down Expand Up @@ -1746,8 +1759,12 @@ if [ -z "$FOLDERNAME" ];then
WORKSPACE_FOLDER_ID="$ROOT_FOLDER"
WORKSPACE_FOLDER_NAME="$ROOT_FOLDER_NAME"
else
WORKSPACE_FOLDER_ID="$(_create_directory "$FOLDERNAME" "$ROOT_FOLDER")"||{ printf "%s\n" "$WORKSPACE_FOLDER_ID" 1>&2&&return 1;}
while read -r foldername <&4&&{ [ -n "$foldername" ]||continue;};do
WORKSPACE_FOLDER_ID="$(_create_directory "$foldername" "${WORKSPACE_FOLDER_ID:-$ROOT_FOLDER}")"||{ printf "%s\n" "$WORKSPACE_FOLDER_ID" 1>&2&&return 1;}
WORKSPACE_FOLDER_NAME="$(_drive_info "$WORKSPACE_FOLDER_ID" name|_json_value name 1 1)"||{ printf "%s\n" "$WORKSPACE_FOLDER_NAME" 1>&2&&return 1;}
done 4<<EOF
$(_split "$FOLDERNAME" "/")
EOF
fi
return 0
}
Expand Down
9 changes: 9 additions & 0 deletions release/sh/gsync
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ return 0
_print_center_quiet(){
{ [ $# = 3 ]&&printf "%s\n" "$2";}||{ printf "%s%s\n" "$2" "$3";}
}
_split(){
set -f
old_ifs_split=$IFS
IFS=$2
set -- $1
printf '%s\n' "$@"
IFS=$old_ifs_split
set +f
}
_support_ansi_escapes(){
unset ansi_escapes
case "${TERM:-}" in
Expand Down
21 changes: 19 additions & 2 deletions release/sh/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,11 @@ export DELETE_ACCOUNT_NAME="${2}" && _parser_shift
EOF
_parser_setup_flag "-c -C --create-dir" 1 required "foldername"
_parser_setup_flag_help \
"Option to create directory. Will print folder id. Can be used to provide input folder, see README."
"Option to create directory on drive. Will print folder id. If this option is used, then input files/folders are optional.
Also supports specifying sub folders, -c 'Folder1/folder2/test'.
Three folders will be created, test inside folder2, folder2 inside Folder1 and so on.
Input files and folders will be uploaded inside test folder."
_parser_setup_flag_preprocess 4<<'EOF'
unset FOLDERNAME
EOF
Expand Down Expand Up @@ -1174,6 +1178,15 @@ return 0
_print_center_quiet(){
{ [ $# = 3 ]&&printf "%s\n" "$2";}||{ printf "%s%s\n" "$2" "$3";}
}
_split(){
set -f
old_ifs_split=$IFS
IFS=$2
set -- $1
printf '%s\n' "$@"
IFS=$old_ifs_split
set +f
}
_support_ansi_escapes(){
unset ansi_escapes
case "${TERM:-}" in
Expand Down Expand Up @@ -1719,8 +1732,12 @@ if [ -z "$FOLDERNAME" ];then
WORKSPACE_FOLDER_ID="$ROOT_FOLDER"
WORKSPACE_FOLDER_NAME="$ROOT_FOLDER_NAME"
else
WORKSPACE_FOLDER_ID="$(_create_directory "$FOLDERNAME" "$ROOT_FOLDER")"||{ printf "%s\n" "$WORKSPACE_FOLDER_ID" 1>&2&&return 1;}
while read -r foldername <&4&&{ [ -n "$foldername" ]||continue;};do
WORKSPACE_FOLDER_ID="$(_create_directory "$foldername" "${WORKSPACE_FOLDER_ID:-$ROOT_FOLDER}")"||{ printf "%s\n" "$WORKSPACE_FOLDER_ID" 1>&2&&return 1;}
WORKSPACE_FOLDER_NAME="$(_drive_info "$WORKSPACE_FOLDER_ID" name|_json_value name 1 1)"||{ printf "%s\n" "$WORKSPACE_FOLDER_NAME" 1>&2&&return 1;}
done 4<<EOF
$(_split "$FOLDERNAME" "/")
EOF
fi
return 0
}
Expand Down
38 changes: 38 additions & 0 deletions src/common/common-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,44 @@ _print_center_quiet() {
{ printf "%s%s\n" "${2}" "${3}"; }
}

###################################################
# Split a string on a delimiter
# Arguments: 2
# ${1} = string
# ${2} = delimiter
# Print each list value on its own line.
# Reference: https://github.com/dylanaraps/pure-sh-bible#split-a-string-on-a-delimiter
###################################################
_split() {
# Disable globbing. This ensures that the word-splitting is safe.
set -f

# Store the current value of 'IFS' so we can restore it later.
# shellcheck disable=2250
old_ifs_split=$IFS

# Change the field separator to what we're
# splitting on.
IFS=$2

# Create an argument list splitting at each
# occurance of '$2'.
# This is safe to disable as it just warns against
# word-splitting which is the behavior we expect.
# shellcheck disable=2086
set -- $1

# Print each list value on its own line.
printf '%s\n' "$@"

# Restore the value of 'IFS'.
# shellcheck disable=2250
IFS=$old_ifs_split

# Re-enable globbing.
set +f
}

###################################################
# Check if script terminal supports ansi escapes
# Result: return 1 or 0
Expand Down
16 changes: 12 additions & 4 deletions src/common/upload-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,19 @@ _setup_workspace() {
WORKSPACE_FOLDER_ID="${ROOT_FOLDER}"
WORKSPACE_FOLDER_NAME="${ROOT_FOLDER_NAME}"
else
WORKSPACE_FOLDER_ID="$(_create_directory "${FOLDERNAME}" "${ROOT_FOLDER}")" ||
{ printf "%s\n" "${WORKSPACE_FOLDER_ID}" 1>&2 && return 1; }
WORKSPACE_FOLDER_NAME="$(_drive_info "${WORKSPACE_FOLDER_ID}" name | _json_value name 1 1)" ||
{ printf "%s\n" "${WORKSPACE_FOLDER_NAME}" 1>&2 && return 1; }
# split the string on / and use each value to create folder on drive
# it is safe to do as folder names can't contain /
while read -r foldername <&4 && { [ -n "${foldername}" ] || continue; }; do
# use WORKSPACE_FOLDER_ID folder id when available so the next folder is created inside the previous folder
WORKSPACE_FOLDER_ID="$(_create_directory "${foldername}" "${WORKSPACE_FOLDER_ID:-${ROOT_FOLDER}}")" ||
{ printf "%s\n" "${WORKSPACE_FOLDER_ID}" 1>&2 && return 1; }
WORKSPACE_FOLDER_NAME="$(_drive_info "${WORKSPACE_FOLDER_ID}" name | _json_value name 1 1)" ||
{ printf "%s\n" "${WORKSPACE_FOLDER_NAME}" 1>&2 && return 1; }
done 4<< EOF
$(_split "${FOLDERNAME}" "/")
EOF
fi

return 0
}

Expand Down
6 changes: 5 additions & 1 deletion src/common/upload-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ EOF

_parser_setup_flag "-c -C --create-dir" 1 required "foldername"
_parser_setup_flag_help \
"Option to create directory. Will print folder id. Can be used to provide input folder, see README."
"Option to create directory on drive. Will print folder id. If this option is used, then input files/folders are optional.
Also supports specifying sub folders, -c 'Folder1/folder2/test'.
Three folders will be created, test inside folder2, folder2 inside Folder1 and so on.
Input files and folders will be uploaded inside test folder."

_parser_setup_flag_preprocess 4<< 'EOF'
unset FOLDERNAME
Expand Down

0 comments on commit 968a27c

Please sign in to comment.