#!/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 < $(script_name) [-h/--help] [-v] [-e] [-g ] [-t ] [-u ] [csv_file] EOF } usage_full() { usage cat < 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, --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'} -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 Curl curl -s -L https://gist.github.com/knzai/75702a336a25646e6c0039f96d5732b9/raw | bash -s -- gdrive_files.csv EOF exit 0 } #============================ # 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 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 "$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 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 # # 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