Last active
July 15, 2024 18:42
-
-
Save knzai/75702a336a25646e6c0039f96d5732b9 to your computer and use it in GitHub Desktop.
Revisions
-
knzai revised this gist
Jul 15, 2024 . 1 changed file with 27 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,31 @@ set -Eeo pipefail #============================ # CONSTANTS & DEFAULTS #============================ #constants readonly VERSION_NUM='1.2.0' readonly SCRIPT='ggdrive.sh' #in case terminal doesn't support colors - readonly after setup_colors runs RESET='' RED='' GREEN='' BLUE='' PURPLE='' CYAN='' YELLOW='' #environment variables #G_API_KEY #IFS # shell built-in Internal Field Separator #defaults #unset csv_file export=false g_api='https://www.googleapis.com/drive/v2' tempfile='tempfile' user_agent='gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9' #============================ # USAGE & METADATA #============================ usage() { cat <<-EOF ${CYAN}Name:${RESET} $(script_name) ($VERSION_NUM) ${CYAN}Description:${RESET} Download google drive file exports, eg gdocs -> pdf ${CYAN}Usage:${RESET} ${RED}<G_API_KEY=XXXX>${RESET} ./$(script_name) ${YELLOW}[-h, --help] [-v] [-e] [-g ${RED}<g_api>${YELLOW}] [-t ${RED}<tempfile>${YELLOW}] [-u ${RED}<user_agent>${YELLOW}] [csv_file]${RESET} @@ -71,12 +88,12 @@ usage() { } version() { msg "$(script_name) $VERSION_NUM" if [ ! $# -eq 0 ]; then cat <<-EOF ${CYAN}Authour & Copyright:${RESET} ${BLUE}version${RESET} $(script_name) $VERSION_NUM ${BLUE}author${RESET} Kenzi Connor ${BLUE}copyright${RESET} Copyright (c) Public Domain ${BLUE}license${RESET} Public Domain via Unlicense (see footer) @@ -100,14 +117,13 @@ version() { die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } script_name() { echo "${GREEN}$SCRIPT${RESET}"; } setup_colors() { if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then RESET=$(tput setaf 7) RED=$(tput setaf 1) GREEN=$(tput setaf 2) BLUE=$(tput setaf 4) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) fi readonly RESET RED GREEN BLUE PURPLE CYAN YELLOW } #============================ @@ -116,12 +132,6 @@ setup_colors() { parse_params() { if [ $# -eq 0 ]; then usage && exit; fi; #flags and options while getopts eg:t:u:hv-: OPT; do # support long options: https://stackoverflow.com/a/28466267/519360 @@ -151,7 +161,7 @@ parse_params() { } #============================ # ALIASES AND FUNCTIONS #============================ get_gdrive_files() { if [ ! -f $1 ]; then die "$1 file not found"; fi -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,23 +57,22 @@ usage() { ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} - script_name harcoded for ease of copying from gist. Update if needed [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && bash ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ && [[ -s \$ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw \\ > \$ggdrivetemp && bash \$ggdrivetemp \$csv_file EOF shift ;; esac done } version() { msg "$(script_name) $version_num" if [ ! $# -eq 0 ]; then cat <<-EOF ${CYAN}Authour & Copyright:${RESET} -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,7 +57,7 @@ usage() { ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it - $(script_name) harcoded for ease of copying from gist (remember to update) ${RESET} [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && chmod +x ggdrive.sh && ./ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,12 +58,12 @@ usage() { curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} - script_name harcoded for ease of copying from gist. Update if needed [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && chmod +x ggdrive.sh && ./ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ && [[ -s \$ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw \\ > \$ggdrivetemp && chmod +x $ggdrivetemp && .\\\$ggdrivetemp \$csv_file EOF shift ;; esac -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,8 +57,8 @@ usage() { ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} - script_name harcoded for ease of copying from gist. Update if needed [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && bash ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,7 +57,7 @@ usage() { ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} - replace \$(script_name) if copying direct from source/gist [ -f $(script_name) ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > $(script_name) && bash ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} -
knzai revised this gist
Jul 15, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ usage() { vars) cat <<-EOF ${CYAN}Environoment variables:${RESET} ${RED}G_API_KEY${RESET} REQUIRED Google Cloud API Key with access to Google Drive API ${CYAN}Arguments:${RESET} ${YELLOW}[csv_file]${RESET} Path/to/file.csv. Leave blank for NOOP. -
knzai revised this gist
Jul 14, 2024 . 1 changed file with 5 additions and 180 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,113 +1,6 @@ #!/usr/bin/env bash set -Eeo pipefail #============================ # USAGE & METADATA #============================ @@ -211,7 +104,11 @@ msg() { echo >&2 -e "${1-}"; } script_name() { echo "${GREEN}$script_name${RESET}"; } setup_colors() { if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then RESET=$(tput setaf 7) RED=$(tput setaf 1) GREEN=$(tput setaf 2) ORANGE='' BLUE=$(tput setaf 4) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) else NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW='' fi } #============================ @@ -301,78 +198,6 @@ parse_params "$@" main #=============================================================================== # LICENSE: Public Domain via Unlicense # -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 223 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -105,39 +105,230 @@ setup_colors; #============================ # PARSE PARAMETERS #============================ #!/usr/bin/env bash set -Eeo pipefail #============================ # USAGE & METADATA #============================ version_num='1.2.0' script_name='ggdrive.sh' usage() { cat <<-EOF ${CYAN}Name:${RESET} $(script_name) ($version_num) ${CYAN}Description:${RESET} Download google drive file exports, eg gdocs -> pdf ${CYAN}Usage:${RESET} ${RED}<G_API_KEY=XXXX>${RESET} ./$(script_name) ${YELLOW}[-h, --help] [-v] [-e] [-g ${RED}<g_api>${YELLOW}] [-t ${RED}<tempfile>${YELLOW}] [-u ${RED}<user_agent>${YELLOW}] [csv_file]${RESET} EOF for i in "$@"; do case $i in vars) cat <<-EOF ${CYAN}Environoment variables:${RESET} ${RED}G_API_KEY${RESET} REQUIRED Google Cloud API Key with access to Google Drive API ${CYAN}Arguments:${RESET} ${YELLOW}[csv_file]${RESET} Path/to/file.csv. Leave blank for NOOP. ${CYAN}Flags:${RESET} ${YELLOW}[-e, --export]${RESET} Export functions. Use w/o [csv_file] for custom handling ${YELLOW}[-v, --version] ${RESET} Output version info. Long form provides more metadata ${YELLOW}[-h, --help]${RESET} Output help. Long form provides more info, examples ${CYAN}Options${RESET} ${BLUE}#Specify endpoint of the google drive api${RESET} ${YELLOW}[-g, --g_api] ${RED}<arg>${RESET} {'https://www.googleapis.com/drive/v2'} ${BLUE}#If you want to be particular about the temporary file${RESET} ${YELLOW}[-t, --tempfile] ${RED}<arg>${RESET} {tempfile} ${BLUE}#I don't think Google actually cares, but hey${RESET} ${YELLOW}[-u, --user_agent] ${RED}<arg>${RESET} {'gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9'} EOF shift ;; eg) cat <<-EOF ${CYAN}Examples${RESET} ${BLUE}# Standard usage${RESET} ./$(script_name) path/to/csv ${BLUE}# Do nothing except export functions for your own use${RESET} ./$(script_name) -e ${BLUE}# Specify a different version of the google drive api${RESET} ./$(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv ${CYAN}Curl examples${RESET} ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} [ -f $(script_name) ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > $(script_name) && bash ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ && [[ -s \$ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw \\ > \$ggdrivetemp && bash \$ggdrivetemp \$csv_file EOF shift ;; esac done } version() { if [ $# -eq 0 ]; then msg "$(script_name) $version_num"; else cat <<-EOF ${CYAN}Authour & Copyright:${RESET} ${BLUE}version${RESET} $(script_name) $version_num ${BLUE}author${RESET} Kenzi Connor ${BLUE}copyright${RESET} Copyright (c) Public Domain ${BLUE}license${RESET} Public Domain via Unlicense (see footer) ${BLUE}site${RESET} knz.ai/ggdrive ${BLUE}source${RESET} gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9 ${CYAN}Version history:${RESET} ${BLUE}2024/07/10${RESET} : 0.0.1 : Script creation ${BLUE}2024/07/11${RESET} : 0.0.2 : Cleaned up, comment, add help and usage ${BLUE}2024/07/12${RESET} : 0.0.3 : Simplify usage() to make curling easier ${BLUE}2024/07/12${RESET} : 1.0.0 : BREAKING Rename env var for clarity ${BLUE}2024/07/12${RESET} : 1.1.0 : Changed --help and --version usage ${BLUE}2024/07/13${RESET} : 1.2.0 : More usage() improvements EOF fi } #============================ # MESSAGING & COLORS #============================ die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } script_name() { echo "${GREEN}$script_name${RESET}"; } setup_colors() { RESET=$(tput setaf 7) RED=$(tput setaf 1) GREEN=$(tput setaf 2) ORANGE='' BLUE=$(tput setaf 4) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) } #============================ # PARSE PARAMETERS #============================ parse_params() { if [ $# -eq 0 ]; then usage && exit; fi; #defaults export=false g_api='https://www.googleapis.com/drive/v2' tempfile='tempfile' user_agent='gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9' #flags and options while getopts eg:t:u:hv-: OPT; do # support long options: https://stackoverflow.com/a/28466267/519360 if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG OPT="${OPTARG%%=*}" # extract long option name OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty) OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` fi case "$OPT" in e|export ) export=true;; g|g_api ) g_api="$OPTARG";; t|tempfile ) tempfile="$OPTARG";; u|user_agent) user_agent="$OPTARG";; h ) usage 'vars' ;; help ) usage 'vars' 'eg' ;; v ) version ;; version ) version 'full' ;; * ) exit 2;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list #positional args csv_file=$1 return 0 } #============================ # ALIAS AND FUNCTIONS #============================ get_gdrive_files() { if [ ! -f $1 ]; then die "$1 file not found"; fi oldifs=$IFS IFS=',' while read gd_id mime dest min_size do gdrive_export $gd_id $mime replace_if_valid $dest $min_size done < $1 IFS=$oldifs } gdrive_export() { #$1: google_file_id #$2: mime_type wget -nv -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$G_API_KEY" } replace_if_valid() { #$1: dest #$2: min_size if [ $(du -k $tempfile | cut -f1) -gt $2 ]; then mv $tempfile "$1" else die "Problem fetching file" fi } #============================ # MAIN #============================ main() { if [ $csv_file ]; then get_gdrive_files $csv_file elif $export; then export -f get_gdrive_files export -f gdrive_export export -f replace_if_valid fi } setup_colors parse_params "$@" main #=============================================================================== # LICENSE: Public Domain via Unlicense # # This is free and unencumbered software released into the public domain. # # Anyone is free to copy, modify, publish, use, compile, sell, or # distribute this software, either in source code form or as a compiled # binary, for any purpose, commercial or non-commercial, and by any # means. # # In jurisdictions that recognize copyright laws, the author or authors # of this software dedicate any and all copyright interest in the # software to the public domain. We make this dedication for the benefit # of the public at large and to the detriment of our heirs and # successors. We intend this dedication to be an overt act of # relinquishment in perpetuity of all present and future rights to this # software under copyright law. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # For more information, please refer to <http://unlicense.org/> #============================ # ALIAS AND FUNCTIONS -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 67 additions and 65 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,67 +4,65 @@ set -Eeo pipefail die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } #============================ # USAGE & METADATA version_num='1.2.0' script_name() { echo "${GREEN}ggdrive.sh${RESET}"; } usage() { cat <<-EOF ${CYAN}Name:${RESET} $(script_name) ($version_num) ${CYAN}Description:${RESET} Download google drive file exports, eg gdocs -> pdf ${CYAN}Usage:${RESET} ${RED}<G_API_KEY=XXXX>${RESET} ./$(script_name) ${YELLOW}[-h, --help] [-v] [-e] [-g ${RED}<g_api>${YELLOW}] [-t ${RED}<tempfile>${YELLOW}] [-u ${RED}<user_agent>${YELLOW}] [csv_file]${RESET} EOF for i in "$@"; do case $i in vars) cat <<-EOF ${CYAN}Environoment variables:${RESET} ${RED}G_API_KEY${RESET} REQUIRED Google Cloud API Key with access to Google Drive API ${CYAN}Arguments:${RESET} ${YELLOW}[csv_file]${RESET} Path/to/file.csv. Leave blank for NOOP. ${CYAN}Flags:${RESET} ${YELLOW}[-e, --export]${RESET} Export functions. Use w/o [csv_file] for custom handling ${YELLOW}[-v, --version] ${RESET} Output version info. Long form provides more metadata ${YELLOW}[-h, --help]${RESET} Output help. Long form provides more info, examples ${CYAN}Options${RESET} ${BLUE}#Specify endpoint of the google drive api${RESET} ${YELLOW}[-g, --g_api] ${RED}<arg>${RESET} {'https://www.googleapis.com/drive/v2'} ${BLUE}#If you want to be particular about the temporary file${RESET} ${YELLOW}[-t, --tempfile] ${RED}<arg>${RESET} {tempfile} ${BLUE}#I don't think Google actually cares, but hey${RESET} ${YELLOW}[-u, --user_agent] ${RED}<arg>${RESET} {'gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9'} EOF shift ;; eg) cat <<-EOF ${CYAN}Examples${RESET} ${BLUE}# Standard usage${RESET} ./$(script_name) path/to/csv ${BLUE}# Do nothing except export functions for your own use${RESET} ./$(script_name) -e ${BLUE}# Specify a different version of the google drive api${RESET} ./$(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv ${CYAN}Curl examples${RESET} ${BLUE}#standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv ${BLUE}#only download if it doesn't exist, then run it${RESET} [ -f $(script_name) ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > $(script_name) && bash ggdrive.sh gdrive_files.csv ${BLUE}#my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front${RESET} csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ && [[ -s \$ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw \\ > \$ggdrivetemp && bash \$ggdrivetemp \$csv_file @@ -74,29 +72,35 @@ usage() { done } version() { if [ $# -eq 0 ]; then echo "$(script_name) $version_num"; else cat <<-EOF ${CYAN}Authour & Copyright:${RESET} ${BLUE}version${RESET} $(script_name) $version_num ${BLUE}author${RESET} Kenzi Connor ${BLUE}copyright${RESET} Copyright (c) Public Domain ${BLUE}license${RESET} Public Domain via Unlicense (see footer) ${BLUE}site${RESET} knz.ai/ggdrive ${BLUE}source${RESET} gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9 ${CYAN}Version history:${RESET} ${BLUE}2024/07/10${RESET} : 0.0.1 : Script creation ${BLUE}2024/07/11${RESET} : 0.0.2 : Cleaned up, comment, add help and usage ${BLUE}2024/07/12${RESET} : 0.0.3 : Simplify usage() to make curling easier ${BLUE}2024/07/12${RESET} : 1.0.0 : BREAKING Rename env var for clarity ${BLUE}2024/07/12${RESET} : 1.1.0 : Changed --help and --version usage ${BLUE}2024/07/13${RESET} : 1.2.0 : More usage() improvements EOF fi } setup_colors() { RESET=$(tput setaf 7) RED=$(tput setaf 1) GREEN=$(tput setaf 2) ORANGE='' BLUE=$(tput setaf 4) PURPLE='' CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) } setup_colors; #============================ # PARSE PARAMETERS @@ -123,11 +127,11 @@ while getopts eg:t:u:hv-: OPT; do t|tempfile ) tempfile="$OPTARG";; u|user_agent) user_agent="$OPTARG";; h ) usage 'vars' ;; help ) usage 'vars' 'eg' ;; v ) version ;; version ) version 'full' ;; * ) exit 2;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list @@ -175,8 +179,6 @@ elif $export; then export -f get_gdrive_files export -f gdrive_export export -f replace_if_valid fi -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 69 additions and 68 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env bash set -Eeo pipefail die() { echo "$*" >&2; exit 2; } @@ -10,67 +10,68 @@ script_name='ggdrive.sh' version_num='1.1.0' usage() { cat <<-EOF Usage: <G_API_KEY=XXXX> $script_name [-h, --help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF for i in "$@"; do case $i in desc) cat <<-EOF Description: Download google drive file exports, eg gdocs -> pdf EOF shift ;; vars) cat <<-EOF Environoment variables: G_API_KEY REQUIRED Google Cloud API Key with access to Google Drive API Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. Flags: -e, --export Export functions. Use w/o [csv_file] for custom handling -v, --version Output version info. Long form provides more metadata -h, --help Output help. Long form provides more info, examples Options #Specify endpoint of the google drive api -g, --g_api <arg> {'https://www.googleapis.com/drive/v2'} #If you want to be particular about the temporary file -t, --tempfile <arg> {tempfile} #I don't think Google actually cares, but hey -u, --user_agent <arg> {'gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9'} EOF shift ;; eg) cat <<-EOF Examples # Standard usage $script_namepath/to/csv # Do nothing except export functions for your own use $script_name -e # Specify a different version of the google drive api $script_name -g https://www.googleapis.com/drive/v3 path/to/csv Curl examples #standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv #only download if it doesn't exist, then run it [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && bash ggdrive.sh gdrive_files.csv #my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front csv_file=gdrive_files.csv && [[ -f \$ggdrivetemp || ggdrivetemp=\$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] \\ && [[ -s \$ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw \\ > \$ggdrivetemp && bash \$ggdrivetemp \$csv_file EOF shift ;; esac done } version() { echo "$script_name $version_num"; } @@ -106,7 +107,7 @@ if [ $# -eq 0 ]; then usage && exit; fi; export=false g_api='https://www.googleapis.com/drive/v2' tempfile='tempfile' user_agent='gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9' #flags and options while getopts eg:t:u:hv-: OPT; do @@ -122,8 +123,8 @@ while getopts eg:t:u:hv-: OPT; do t|tempfile ) tempfile="$OPTARG";; u|user_agent) user_agent="$OPTARG";; h ) usage 'desc' 'vars' ; exit 0;; help ) usage 'desc' 'vars' 'eg'; exit 0;; v ) version; exit 0;; version ) version_full; exit 0;; * ) exit 2;; @@ -171,9 +172,9 @@ replace_if_valid() { if [ $csv_file ]; then get_gdrive_files $csv_file elif $export; then export -f get_gdrive_files export -f gdrive_export export -f replace_if_valid else die 'NOOP: neither --export nor <csvfile> passed in' fi -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,11 @@ script_name='ggdrive.sh' version_num='1.1.0' usage() { case $1 in '') cat <<EOF Usage: <G_API_KEY=XXXX> $script_name [-h, --help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF esac } usage_full() { -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -169,9 +169,9 @@ replace_if_valid() { if [ $csv_file ]; then get_gdrive_files $csv_file elif $export; then export -f get_gdrive_files export -f gdrive_export export -f replace_if_valid else die 'NOOP: neither --export nor <csvfile> passed in' fi -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 61 additions and 43 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,17 @@ #!/bin/bash set -Eeo pipefail die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } #============================ # USAGE & MESSAGING #============================ script_name='ggdrive.sh' version_num='1.1.0' usage() { cat <<EOF Usage: <G_API_KEY=XXXX> $script_name [-h, --help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF } @@ -27,28 +28,37 @@ Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. Flags: -e, --export Export functions. Use w/o [csv_file] for custom handling -v, --version Output version info. Long form provides more metadata -h, --help Output help. Long form provides more info, examples Options #Specify endpoint of the google drive api -g, --g_api <arg> {'https://www.googleapis.com/drive/v2'} #If you want to be particular about the temporary file -t, --tempfile <arg> {tempfile} #I don't think Google actually cares, but hey -u, --user_agent <arg> {'github.com/knzai'} EOF } usage_expanded() { usage_full cat <<EOF Examples # Standard usage $script_namepath/to/csv # Do nothing except export functions for your own use $script_name -e # Specify a different version of the google drive api $script_name -g https://www.googleapis.com/drive/v3 path/to/csv Curl examples #standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv @@ -59,7 +69,30 @@ Curl csv_file=gdrive_files.csv && [[ -f $ggdrivetemp || ggdrivetemp=$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] && [[ -s $ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > $ggdrivetemp && bash $ggdrivetemp $csv_file EOF } version() { echo "$script_name $version_num"; } version_full() { version cat <<EOF Authour & Copyright version ggdrive.sh 0.0.3 author Kenzi Connor copyright Copyright (c) Public Domain license Public Domain via Unlicense (see footer) site knz.ai/ggdrive source gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9 Version history: 2024/07/10 : 0.0.1 : Script creation 2024/07/11 : 0.0.2 : Cleaned up, comment, add help and usage 2024/07/21 : 0.0.3 : Simplify usage() to make curling easier 2024/07/12 : 1.0.0 : BREAKING Rename env var for clarity 2024/07/12 : 1.1.0 : Changed --help and --version usage EOF } #============================ @@ -82,14 +115,16 @@ while getopts eg:t:u:hv-: OPT; do OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` fi case "$OPT" in e|export ) export=true;; g|g_api ) g_api="$OPTARG";; t|tempfile ) tempfile="$OPTARG";; u|user_agent) user_agent="$OPTARG";; h ) usage_full; exit 0;; help ) usage_expanded; exit 0;; v ) version; exit 0;; version ) version_full; exit 0;; * ) exit 2;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list @@ -142,23 +177,6 @@ else fi #=============================================================================== # LICENSE: Public Domain via Unlicense # -
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,7 @@ usage_full() { usage cat <<EOF Description: Download google drive file exports, eg gdocs -> pdf Environoment variables: G_API_KEY REQUIRED Google Cloud API Key with access to Google Drive API -
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . No changes.There are no files selected for viewing
-
knzai revised this gist
Jul 13, 2024 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -56,6 +56,9 @@ Curl #only download if it doesn't exist, then run it [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && bash ggdrive.sh gdrive_files.csv #my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front csv_file=gdrive_files.csv && [[ -f $ggdrivetemp || ggdrivetemp=$(mktemp -t ggdriveXXXXXXXXXX.sh) ]] && [[ -s $ggdrivetemp ]] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > $ggdrivetemp && bash $ggdrivetemp $csv_file EOF exit 0 } -
knzai revised this gist
Jul 13, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -112,7 +112,7 @@ get_gdrive_files() { gdrive_export() { #$1: google_file_id #$2: mime_type wget -nv -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$G_API_KEY" } replace_if_valid() { #$1: dest -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,8 +50,12 @@ Examples $(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv Curl #standard run directly from the pipe curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv #only download if it doesn't exist, then run it [ -f ggdrive.sh ] || curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw > ggdrive.sh && bash ggdrive.sh gdrive_files.csv EOF exit 0 } @@ -145,6 +149,7 @@ fi # license Public Domain via Unlicense # site knz.ai/ggdrive # source gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9 # #=============================================================================== # HISTORY # 2024/07/10 : 0.0.1 : Script creation -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ Description: Download google drive file exports, eg gdocs -> pdf Environoment variables: G_API_KEY REQUIRED Google Cloud API Key with access to Google Drive API Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ msg() { echo >&2 -e "${1-}"; } #============================ usage() { cat <<EOF Usage: <G_API_KEY=XXXX> $(script_name) [-h/--help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF } @@ -22,7 +22,7 @@ Description: Download google drive file exports, eg gdocs -> pdf Environoment variables: G_API_KEY REQUIRED Google Cloud API Key with access to Google Drive API Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. @@ -108,7 +108,7 @@ get_gdrive_files() { gdrive_export() { #$1: google_file_id #$2: mime_type wget -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$G_API_KEY" } replace_if_valid() { #$1: dest @@ -144,12 +144,13 @@ fi # copyright Copyright (c) Public Domain # license Public Domain via Unlicense # site knz.ai/ggdrive # source gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9 #=============================================================================== # HISTORY # 2024/07/10 : 0.0.1 : Script creation # 2024/07/11 : 0.0.2 : Cleaned up, comment, add help and usage # 2024/07/21 : 0.0.3 : Simplify usage() to make curling easier # 2024/07/12 : 1.0.0 : BREAKING Rename env var for clarity # #=============================================================================== # LICENSE: Public Domain via Unlicense -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 42 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,11 +6,11 @@ version() { echo "$(script_name)0.0.3" && exit 0; } die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } #============================ # USAGE HELP & MESSAGING #============================ usage() { cat <<EOF Usage: <GAPI_KEY=XXXX> $(script_name) [-h/--help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF } @@ -28,8 +28,8 @@ Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. Flags: -e, --export export functions. Use w/o [csv_file] for custom handling -g, --g_api Specify endpoint of the google drive api {'https://www.googleapis.com/drive/v2'} -t, --tempfile If you want to be particular about the tempfile {tempfile} @@ -57,45 +57,42 @@ EOF } #============================ # PARSE PARAMETERS #============================ if [ $# -eq 0 ]; then usage && exit; fi; #defaults export=false g_api='https://www.googleapis.com/drive/v2' tempfile='tempfile' user_agent='github.com/knzai' #flags and options while getopts eg:t:u:hv-: OPT; do # support long options: https://stackoverflow.com/a/28466267/519360 if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG OPT="${OPTARG%%=*}" # extract long option name OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty) OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` fi case "$OPT" in e|export ) export=true ;; g|g_api ) g_api="$OPTARG" ;; t|tempfile ) tempfile="$OPTARG" ;; u|user_agent) user_agent="$OPTARG" ;; h|help ) usage_full; ;; v|version ) version; ;; * ) exit 2 ;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list #positional args csv_file=$1 #============================ # ALIAS AND FUNCTIONS #============================ get_gdrive_files() { if [ ! -f $1 ]; then die "$1 file not found"; fi @@ -111,44 +108,49 @@ get_gdrive_files() { gdrive_export() { #$1: google_file_id #$2: mime_type wget -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$GAPI_KEY" } replace_if_valid() { #$1: dest #$2: min_size if [ $(du -k $tempfile | cut -f1) -gt $2 ]; then mv $tempfile "$1" else echo "Problem fetching file" exit 1 fi } #============================ # MAIN SCRIPT #============================ if [ $csv_file ]; then get_gdrive_files $csv_file elif $export; then export -f get_gdrive_files export -f gdrive_export export -f replace_if_valid else die 'NOOP: neither --export nor <csvfile> passed in' fi #=============================================================================== # AUTHOR & COPYRIGHT # version ggdrive.sh 0.0.3 # author Kenzi Connor # copyright Copyright (c) Public Domain # license Public Domain via Unlicense # site knz.ai/ggdrive # #=============================================================================== # HISTORY # 2024/07/10 : 0.0.1 : Script creation # 2024/07/11 : 0.0.2 : Cleaned up, comment, add help and usage # 2024/07/11 : 0.0.3 : Simplify usage() to make curling easier # #=============================================================================== # LICENSE: Public Domain via Unlicense # -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,6 +49,9 @@ Examples # Specify a different version of the google drive api $(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv Curl curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv EOF exit 0 } -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 38 additions and 37 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,16 @@ #!/bin/bash set -Eeo pipefail script_name() { echo 'ggdrive.sh'; } version() { echo "$(script_name)0.0.3" && exit 0; } die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } #============================ # USAGE HELP & MESSAGING #============================ usage() { cat <<EOF Usage: <GAPI_KEY=XXXX> $(script_name) [-h/--help] [-v] [-e] [-g <gapi_url>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF } @@ -43,21 +41,22 @@ Flags: Examples # Standard usage $(script_name)path/to/csv # Do nothing except export functions for your own use $(script_name) -e # Specify a different version of the google drive api $(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv EOF exit 0 } #============================ # PARSE PARAMETERS #============================ if [ $# -eq 0 ]; then usage && exit; fi; #defaults EXPORT=false @@ -81,6 +80,7 @@ while getopts eg:t:u:hv-: OPT; do h|help ) usage_full; ;; v|version) version; ;; *) die;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list @@ -90,49 +90,50 @@ csv_file=$1 #final parameter checks if [ $# -eq 0 ] && ! $EXPORT; then die 'Flags passed, but neither -e nor <csvfile> so still a total a NOOP'; fi #============================ # ALIAS AND FUNCTIONS #============================ get_gdrive_files() { if [ ! -f $1 ]; then die "$1 file not found"; fi oldifs=$IFS IFS=',' while read gd_id mime dest min_size do gdrive_export $gd_id $mime replace_if_valid $dest $min_size done < $1 IFS=$oldifs } gdrive_export() { #$1: google_file_id #$2: mime_type wget -O $TEMPFILE --user-agent=$USER_AGENT "$GAPI_URL/files/$1/export?mimeType=$2&key=$GAPI_KEY" } replace_if_valid() { #$1: dest #$2: min_size if [ $(du -k $TEMPFILE | cut -f1) -gt $2 ]; then mv $TEMPFILE "$1" else echo "Problem fetching file" exit 1 fi } #============================ # MAIN SCRIPT #============================ if [ $csv_file ]; then get_gdrive_files $csv_file fi if $EXPORT; then # export -f get_gdrive_files # export -f gdrive_export # export -f replace_if_valid echo "hah" fi -
knzai revised this gist
Jul 12, 2024 . 1 changed file with 87 additions and 111 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,119 +1,97 @@ #!/bin/bash set -e #since I call this from a GH Action better to always know if it'll fail #============================ # USAGE HELP & MESSAGING #============================ die() { echo "$*" >&2; exit 2; } msg() { echo >&2 -e "${1-}"; } SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}") version() { echo "${SCRIPT_NAME} 0.0.3" && exit 0;} usage() { cat <<EOF Usage: <GAPI_KEY=XXXX> ${SCRIPT_NAME} [-h/--help] [-v] [-e] [-g <gapi_url>] [-t <tempfile>] [-u <user_agent>] [csv_file] EOF } usage_full() { usage cat <<EOF Description: Download google drive file exports, eg gdocs -> pdf Environoment variables: GAPI_KEY REQUIRED Google Cloud API Key with access to Google Drive API Arguments: csv_file Path/to/file.csv. Leave blank for NOOP. Flags: -e, --export Export functions. Use w/o [csv_file] for custom handling -g, --gapi_url Specify endpoint of the google drive api {'https://www.googleapis.com/drive/v2'} -t, --tempfile If you want to be particular about the tempfile {tempfile} -u, --user_agent I don't think Google actually cares, but hey {'github.com/knzai'} -h, --help Print this help -v, --version Print script information Examples # Standard usage ${SCRIPT_NAME} path/to/csv # Do nothing except export functions for your own use ${SCRIPT_NAME} -e # Specify a different version of the google drive api ${SCRIPT_NAME} -g https://www.googleapis.com/drive/v3 path/to/csv EOF exit 0 } #============================ # PARSE PARAMETERS #============================ if [ $# -eq 0 ]; then usage && exit 0; fi; #defaults EXPORT=false GAPI_URL='https://www.googleapis.com/drive/v2' TEMPFILE='tempfile' USER_AGENT='github.com/knzai' #flags and options while getopts eg:t:u:hv-: OPT; do # support long options: https://stackoverflow.com/a/28466267/519360 if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG OPT="${OPTARG%%=*}" # extract long option name OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty) OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` fi case "$OPT" in e|export ) EXPORT=true ;; g|gapi_url ) GAPI_URL="$OPTARG" ;; t|tempfile ) TEMPFILE="$OPTARG" ;; u|user_agent) USER_AGENT="$OPTARG" ;; h|help ) usage_full; ;; v|version) version; ;; esac done shift $((OPTIND-1)) # remove parsed options and args from $@ list #positional args csv_file=$1 #final parameter checks if [ $# -eq 0 ] && ! $EXPORT; then die 'Flags passed, but neither -e nor <csvfile> so still a total a NOOP'; fi if [ ! -f $csv_file ]; then die "$1 file not found"; fi #============================ # ALIAS AND FUNCTIONS #============================ @@ -147,14 +125,9 @@ replace_if_valid() { # MAIN SCRIPT #============================ if $csv_file; then get_gdrive_files $csv_file end if $EXPORT; then export -f get_gdrive_files @@ -163,8 +136,15 @@ if $EXPORT; then fi #=============================================================================== #- AUTHOR & COPYRIGHT #- version ggdrive.sh 0.0.3 #- author Kenzi Connor #- copyright Copyright (c) Public Domain #- license Public Domain via Unlicense #- site knz.ai/ggdrive #- #=============================================================================== # LICENSE: Public Domain via Unlicense # @@ -191,8 +171,4 @@ fi # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # For more information, please refer to <http://unlicense.org/>
NewerOlder