From 2d8b033c3e033f5edb847a87540720d4c3d97511 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Mon, 17 Aug 2026 21:31:12 +0200 Subject: [PATCH] Extend gd functionality --- zsh/aliases.zsh | 102 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 12 deletions(-) diff --git a/zsh/aliases.zsh b/zsh/aliases.zsh index 217b8bd..f638d87 100644 --- a/zsh/aliases.zsh +++ b/zsh/aliases.zsh @@ -43,20 +43,98 @@ _gdcheck() { } } -# gdpush pushes a file to Google Drive -gdpush() { - _gdcheck || return - zstd -6 -T0 -c -- "$1" | gpg -c -z 0 -o - | rclone rcat "gdrive:backups/${1:t}.zst.gpg" +gd-auth() { + rclone lsd gdrive: >/dev/null 2>&1 || { + print -u2 "Google Drive auth failed; try: rclone config reconnect gdrive:" + return 1 + } } -# gdls lists files in Google Drive -gdls() { - _gdcheck || return - rclone lsf --files-only gdrive:backups/ | sed 's/\.zst\.gpg$//' +gd-check() { + local missing=() + + for cmd in rclone zstd gpg; do + command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd") + done + + (( ${#missing[@]} == 0 )) || { + print -u2 "Missing dependencies: ${missing[*]}" + return 1 + } + + gd-auth } -# gdpull pulls a file from Google Drive -gdpull() { - _gdcheck || return - rclone cat "gdrive:backups/${1:t}.zst.gpg" | gpg -d | zstd -d -c +# gd-push pushes a file to Google Drive with compression and password encryption +gd-push() { + gd-check || return + zstd -6 -T0 -c -- "$1" \ + | gpg -c -z 0 -o - \ + | rclone rcat "gdrive:backups/${1:t}.zst.gpg" +} + +# gd-push pulls a file from Google Drive, password decrypts and decompresses it. +# gd-push outputs to stdout +gd-pull() { + gd-check || return + rclone cat "gdrive:backups/${1:t}.zst.gpg" \ + | gpg -d \ + | zstd -d -c +} + +# gd-ls lists all the files available on Google Drive +gd-ls() { + gd-check || return + rclone lsf --files-only gdrive:backups/ \ + | sed -n 's/\.zst\.gpg$//p' +} + +# gd-rm removes a file from Google Drive +gd-rm() { + gd-check || return + rclone deletefile "gdrive:backups/${1:t}.zst.gpg" +} + +# dispatcher for gd functions +gd() { + local command="$1" + shift 2>/dev/null || true + + case "$command" in + push) + gd-push "$@" + ;; + pull) + gd-pull "$@" + ;; + ls) + gd-ls "$@" + ;; + rm) + gd-rm "$@" + ;; + check) + gd-check "$@" + ;; + auth) + gd-auth "$@" + ;; + "") + print -u2 "usage: gd [args]" + print -u2 "" + print -u2 "commands:" + print -u2 " push Compress, encrypt, and upload a file" + print -u2 " pull Download, decrypt, and decompress to stdout" + print -u2 " ls List available files" + print -u2 " rm Remove a file from Google Drive" + print -u2 " check Check dependencies and authentication" + print -u2 " auth Check Google Drive authentication" + return 2 + ;; + *) + print -u2 "gd: unknown command: $command" + print -u2 "usage: gd {push|pull|ls|rm|check|auth} ..." + return 2 + ;; + esac }