Skip to content

Instantly share code, notes, and snippets.

@knzai
Last active July 15, 2024 18:42
Show Gist options
  • Select an option

  • Save knzai/75702a336a25646e6c0039f96d5732b9 to your computer and use it in GitHub Desktop.

Select an option

Save knzai/75702a336a25646e6c0039f96d5732b9 to your computer and use it in GitHub Desktop.

Revisions

  1. knzai revised this gist Jul 15, 2024. 1 changed file with 27 additions and 17 deletions.
    44 changes: 27 additions & 17 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,31 @@
    set -Eeo pipefail

    #============================
    # USAGE & METADATA
    # CONSTANTS & DEFAULTS
    #============================
    version_num='1.2.0'
    script_name='ggdrive.sh'
    #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}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"
    msg "$(script_name) $VERSION_NUM"
    if [ ! $# -eq 0 ]; then
    cat <<-EOF
    ${CYAN}Authour & Copyright:${RESET}
    ${BLUE}version${RESET} $(script_name) $version_num
    ${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_name${RESET}"; }
    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) ORANGE='' BLUE=$(tput setaf 4) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3)
    else
    NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
    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;

    #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
    @@ -151,7 +161,7 @@ parse_params() {
    }

    #============================
    # ALIAS AND FUNCTIONS
    # ALIASES AND FUNCTIONS
    #============================
    get_gdrive_files() {
    if [ ! -f $1 ]; then die "$1 file not found"; fi
  2. knzai revised this gist Jul 15, 2024. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions ggdrive.sh
    Original 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 - $(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}#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 && chmod +x $ggdrivetemp && .\\\$ggdrivetemp \$csv_file
    > \$ggdrivetemp && bash \$ggdrivetemp \$csv_file
    EOF
    shift ;;
    esac
    done
    }

    version() {
    if [ $# -eq 0 ]; then
    msg "$(script_name) $version_num";
    else
    msg "$(script_name) $version_num"
    if [ ! $# -eq 0 ]; then
    cat <<-EOF
    ${CYAN}Authour & Copyright:${RESET}
  3. knzai revised this gist Jul 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ggdrive.sh
    Original 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} - script_name harcoded for ease of copying from gist. Update if needed
    ${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}
  4. knzai revised this gist Jul 15, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ggdrive.sh
    Original 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 && bash ggdrive.sh gdrive_files.csv
    [ -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 && bash \$ggdrivetemp \$csv_file
    > \$ggdrivetemp && chmod +x $ggdrivetemp && .\\\$ggdrivetemp \$csv_file
    EOF
    shift ;;
    esac
  5. knzai revised this gist Jul 15, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ggdrive.sh
    Original 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} - 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}#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) ]] \\
  6. knzai revised this gist Jul 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ggdrive.sh
    Original 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}
    ${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}
  7. knzai revised this gist Jul 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ggdrive.sh
    Original 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
    ${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.
  8. knzai revised this gist Jul 14, 2024. 1 changed file with 5 additions and 180 deletions.
    185 changes: 5 additions & 180 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -1,113 +1,6 @@
    #!/usr/bin/env bash
    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
    EOF
    shift ;;
    esac
    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
    #============================
    #!/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() {
    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)
    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
    #
    # 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
    #============================
    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
    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
    fi


    #===============================================================================
    # LICENSE: Public Domain via Unlicense
    #
  9. knzai revised this gist Jul 13, 2024. 1 changed file with 223 additions and 32 deletions.
    255 changes: 223 additions & 32 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -105,39 +105,230 @@ setup_colors;
    #============================
    # PARSE PARAMETERS
    #============================
    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 `=`
    #!/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
    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
    }

    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
  10. knzai revised this gist Jul 13, 2024. 1 changed file with 67 additions and 65 deletions.
    132 changes: 67 additions & 65 deletions ggdrive.sh
    Original 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 & MESSAGING
    #============================
    script_name='ggdrive.sh'
    version_num='1.1.0'
    # USAGE & METADATA
    version_num='1.2.0'

    script_name() { echo "${GREEN}ggdrive.sh${RESET}"; }

    usage() {
    cat <<-EOF
    Usage: <G_API_KEY=XXXX> $script_name [-h, --help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file]
    ${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
    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
    ${CYAN}Environoment variables:${RESET}
    ${RED}G_API_KEY${RESET} REQUIRED Google Cloud API Key with access to Google Drive API
    Arguments:
    csv_file Path/to/file.csv. Leave blank for NOOP.
    ${CYAN}Arguments:${RESET}
    ${YELLOW}[csv_file]${RESET} 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
    ${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
    Options
    #Specify endpoint of the google drive api
    -g, --g_api <arg> {'https://www.googleapis.com/drive/v2'}
    ${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'}
    #If you want to be particular about the temporary file
    -t, --tempfile <arg> {tempfile}
    ${BLUE}#If you want to be particular about the temporary file${RESET}
    ${YELLOW}[-t, --tempfile] ${RED}<arg>${RESET} {tempfile}
    #I don't think Google actually cares, but hey
    -u, --user_agent <arg> {'gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9'}
    ${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
    Examples
    # Standard usage
    $script_namepath/to/csv
    ${CYAN}Examples${RESET}
    ${BLUE}# Standard usage${RESET}
    ./$(script_name) path/to/csv
    # Do nothing except export functions for your own use
    $script_name -e
    ${BLUE}# Do nothing except export functions for your own use${RESET}
    ./$(script_name) -e
    # Specify a different version of the google drive api
    $script_name -g https://www.googleapis.com/drive/v3 path/to/csv
    ${BLUE}# Specify a different version of the google drive api${RESET}
    ./$(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv
    Curl examples
    #standard run directly from the pipe
    ${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
    #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
    ${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
    #my fave bootstrapping: if there isn't a tempfile, download to one and run; easy csv_file path at front
    ${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() { 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
    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
    }

    EOF
    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 'desc' 'vars' ; exit 0;;
    help ) usage 'desc' 'vars' 'eg'; exit 0;;
    v ) version; exit 0;;
    version ) version_full; exit 0;;
    * ) exit 2;;
    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
    else
    die 'NOOP: neither --export nor <csvfile> passed in'
    fi


  11. knzai revised this gist Jul 13, 2024. 1 changed file with 69 additions and 68 deletions.
    137 changes: 69 additions & 68 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/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() {
    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() {
    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
    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
    #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
    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='github.com/knzai'
    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_full; exit 0;;
    help ) usage_expanded; exit 0;;
    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
    export -f get_gdrive_files
    export -f gdrive_export
    export -f replace_if_valid
    else
    die 'NOOP: neither --export nor <csvfile> passed in'
    fi
  12. knzai revised this gist Jul 13, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,11 @@ script_name='ggdrive.sh'
    version_num='1.1.0'

    usage() {
    cat <<EOF
    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() {
  13. knzai revised this gist Jul 13, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ggdrive.sh
    Original 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
    export -f get_gdrive_files
    export -f gdrive_export
    export -f replace_if_valid
    else
    die 'NOOP: neither --export nor <csvfile> passed in'
    fi
  14. knzai revised this gist Jul 13, 2024. 1 changed file with 61 additions and 43 deletions.
    104 changes: 61 additions & 43 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@
    #!/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 & 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]
    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
    -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}
    -u, --user_agent I don't think Google actually cares, but hey
    {'github.com/knzai'}
    -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}
    -h, --help Print this help
    -v, --version Print script information
    #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_name)path/to/csv
    $script_namepath/to/csv
    # Do nothing except export functions for your own use
    $(script_name) -e
    $script_name -e
    # Specify a different version of the google drive api
    $(script_name) -g https://www.googleapis.com/drive/v3 path/to/csv
    $script_name -g https://www.googleapis.com/drive/v3 path/to/csv
    Curl
    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
    exit 0
    }

    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|help ) usage_full; ;;
    v|version ) version; ;;
    * ) exit 2 ;;
    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



    #===============================================================================
    # 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
    # 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
    #
  15. knzai revised this gist Jul 13, 2024. No changes.
  16. knzai revised this gist Jul 13, 2024. No changes.
  17. knzai revised this gist Jul 13, 2024. No changes.
  18. knzai revised this gist Jul 13, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ggdrive.sh
    Original 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
    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
  19. knzai revised this gist Jul 13, 2024. No changes.
  20. knzai revised this gist Jul 13, 2024. No changes.
  21. knzai revised this gist Jul 13, 2024. No changes.
  22. knzai revised this gist Jul 13, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ggdrive.sh
    Original 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
    }
  23. knzai revised this gist Jul 13, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ggdrive.sh
    Original 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 -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$G_API_KEY"
    wget -nv -O $tempfile --user-agent=$user_agent "$g_api/files/$1/export?mimeType=$2&key=$G_API_KEY"
    }
    replace_if_valid() {
    #$1: dest
  24. knzai revised this gist Jul 12, 2024. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions ggdrive.sh
    Original 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
  25. knzai revised this gist Jul 12, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ggdrive.sh
    Original 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
    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.
  26. knzai revised this gist Jul 12, 2024. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ msg() { echo >&2 -e "${1-}"; }
    #============================
    usage() {
    cat <<EOF
    Usage: <GAPI_KEY=XXXX> $(script_name) [-h/--help] [-v] [-e] [-g <g_api>] [-t <tempfile>] [-u <user_agent>] [csv_file]
    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:
    GAPI_KEY REQUIRED Google Cloud API Key with access to Google Drive API
    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=$GAPI_KEY"
    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/11 : 0.0.3 : Simplify usage() to make curling easier
    # 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
  27. knzai revised this gist Jul 12, 2024. 1 changed file with 42 additions and 40 deletions.
    82 changes: 42 additions & 40 deletions ggdrive.sh
    Original 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 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]
    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, --gapi_url Specify endpoint of the google drive api
    -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
    # PARSE PARAMETERS
    #============================
    if [ $# -eq 0 ]; then usage && exit; fi;

    #defaults
    EXPORT=false
    GAPI_URL='https://www.googleapis.com/drive/v2'
    TEMPFILE='tempfile'
    USER_AGENT='github.com/knzai'
    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
    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; ;;
    *) die;;
    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

    #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
    # 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 "$GAPI_URL/files/$1/export?mimeType=$2&key=$GAPI_KEY"
    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"
    if [ $(du -k $tempfile | cut -f1) -gt $2 ]; then
    mv $tempfile "$1"
    else
    echo "Problem fetching file"
    exit 1
    fi
    }

    #============================
    # MAIN SCRIPT
    # 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"
    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
    #-
    # 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
    #
  28. knzai revised this gist Jul 12, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ggdrive.sh
    Original 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
    }
  29. knzai revised this gist Jul 12, 2024. 1 changed file with 38 additions and 37 deletions.
    75 changes: 38 additions & 37 deletions ggdrive.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,16 @@
    #!/bin/bash
    set -e #since I call this from a GH Action better to always know if it'll fail
    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
    #============================
    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]
    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
    $(script_name)path/to/csv
    # Do nothing except export functions for your own use
    ${SCRIPT_NAME} -e
    $(script_name) -e
    # Specify a different version of the google drive api
    ${SCRIPT_NAME} -g https://www.googleapis.com/drive/v3 path/to/csv
    $(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;
    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
    if [ ! -f $csv_file ]; then die "$1 file not found"; fi

    #============================
    # ALIAS AND FUNCTIONS
    #============================
    get_gdrive_files() {
    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
    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"
    #$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
    #$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
    if [ $csv_file ]; then
    get_gdrive_files $csv_file
    end
    fi

    if $EXPORT; then
    export -f get_gdrive_files
    export -f gdrive_export
    export -f replace_if_valid
    # export -f get_gdrive_files
    # export -f gdrive_export
    # export -f replace_if_valid
    echo "hah"
    fi


  30. knzai revised this gist Jul 12, 2024. 1 changed file with 87 additions and 111 deletions.
    198 changes: 87 additions & 111 deletions ggdrive.sh
    Original 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

    #===============================================================================
    # HEADER
    #===============================================================================
    #% SYNOPSIS
    #+ GAPI_KEY=**** ${SCRIPT_NAME} [-egtuhv] <csvfile> #-h or --help for more details
    #%
    #% DESCRIPTION
    #% Download google drive file exports, eg gdocs -> pdf
    #%
    #% ARGUMENTS
    #% $1 <csvfile> Leave blank for noop (add -e for lib usage)
    #% Row format: gdrive_file_id,mime_type,dest,min_size\n
    #%
    #% ENV VARIABLES
    #% GAPI_KEY Google Cloud API Key with access to Google Drive API
    #%
    #% OPTIONS
    #% -e, --export Export the functions for your own use
    #% -g, --gapi_url Specify different endpoint of the google drive api
    #% -t, --tempfile If you want to be particular about the tempfile
    #% -u, --user_agent I don't think Google actually cares, but hey
    #%
    #% -h, --help Print this help
    #% -v, --version Print script information
    #%
    #% CURL USAGE - if it gets popular rate limiting, but that's not a concern now
    #% CSVFILE=gdrive_files.csv && GGTEMP=$(mktemp -t ggdriveXXXXXXXXXX.sh)\
    #% && curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw\
    #% > $GGTEMP && bash $GGTEMP $CSVFILE && echo $GGTEMP && rm $GGTEMP
    #%
    #% 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
    #%
    #===============================================================================
    #- IMPLEMENTATION
    #- version ${SCRIPT_NAME} (knz.ai) 0.0.2
    #- author Kenzi Connor
    #- copyright Copyright (c) Public Domain
    #- license Public Domain via Unlicense (see footer)
    #- site knz.ai/ggdrive
    #- source https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9
    #-
    #===============================================================================
    # ATTRIBUTIONS
    # Template for this header parsing
    # Michel VONGVILAY (https://www.uxora.com)
    # https://www.uxora.com/unix/shell-script/18-shell-script-template
    #
    #===============================================================================
    # HISTORY
    # 2024/07/10 : 0.0.1 : knzai : Script creation
    # 2024/07/11 : 0.0.2 : knzai : Cleaned up, comment, add help and usage
    #
    #===============================================================================
    # END_OF_HEADER
    #===============================================================================

    #============================
    # USAGE AND HELP OUPUT
    # USAGE HELP & MESSAGING
    #============================
    usage() { printf "Usage: "; scriptinfo usg ; }
    usagefull() { scriptinfo ful ; }
    scriptinfo() { headFilter="^#-"
    [[ "$1" = "usg" ]] && headFilter="^#+"
    [[ "$1" = "ful" ]] && headFilter="^#[%+]"
    [[ "$1" = "ver" ]] && headFilter="^#-"
    head -${SCRIPT_HEADSIZE:-99} ${0} | grep -e "${headFilter}" | sed -e "s/${headFilter}//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g"; }
    SCRIPT_NAME="$(basename ${0})" # scriptname without path
    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 ARGUMENTS
    # PARSE PARAMETERS
    #============================
    die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
    needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; }
    if [ $# -eq 0 ]; then
    usage
    exit 0
    fi
    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 ) needs_arg; GAPI_URL="$OPTARG" ;;
    t|tempfile ) needs_arg; TEMPFILE="$OPTARG" ;;
    u|user_agent ) needs_arg; USER_AGENT="$OPTARG" ;;

    h|help ) usagefull; exit 0 ;;
    v|version ) scriptinfo; exit 0 ;;
    \? ) exit 2 ;;
    * ) die "Illegal option --$OPT" ;;
    esac
    # 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 [ $# -ne 0 ]; then
    if [ ! -f $1 ]; then
    echo "$1 file not found"; exit 99;
    fi
    get_gdrive_files $1
    elif ! $EXPORT; then
    echo 'Flags passed, but neither -e nor <csvfile> so a NOOP'
    fi
    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



    #===============================================================================
    # FOOTER
    #- 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/>
    #
    #===============================================================================
    # END_OF_FOOTER
    #===============================================================================
    # For more information, please refer to <http://unlicense.org/>