143 lines
3.1 KiB
Bash
143 lines
3.1 KiB
Bash
#!/usr/bin/false
|
|
|
|
# LC_COLLATE=C: sort case-sensitive, i.e. README before aaa.py
|
|
alias ls='LC_COLLATE=C ls --color --group-directories-first'
|
|
alias ll='LC_COLLATE=C ls -la'
|
|
alias la='LC_COLLATE=C ls -A'
|
|
alias l='LC_COLLATE=C ls -CF'
|
|
|
|
alias xo='xdg-open'
|
|
|
|
alias pacman="sudo pacman"
|
|
|
|
alias history="history 0" # force zsh to show the complete history
|
|
|
|
# create directory and set it as cwd
|
|
alias take=_take
|
|
_take() {
|
|
if [ -z "$1" ]; then
|
|
printf 'usage: %s directory' $0 > /dev/stderr
|
|
return 1
|
|
fi
|
|
|
|
mkdir -p $1
|
|
cd $1
|
|
}
|
|
|
|
# gd: Check dependencies and auth
|
|
_gdcheck() {
|
|
# deps
|
|
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
|
|
}
|
|
|
|
# auth
|
|
rclone lsd gdrive: >/dev/null 2>&1 || {
|
|
print -u2 "Google Drive auth failed; try: rclone config reconnect gdrive:"
|
|
return 1
|
|
}
|
|
}
|
|
|
|
gd-auth() {
|
|
rclone lsd gdrive: >/dev/null 2>&1 || {
|
|
print -u2 "Google Drive auth failed; try: rclone config reconnect gdrive:"
|
|
return 1
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
# gd-push pushes a file to Google Drive with compression and password encryption
|
|
gd-push() {
|
|
gd-check || return
|
|
tar cf - "$1" \
|
|
| zstd -6 \
|
|
| gpg -c -z 0 -o - \
|
|
| rclone rcat "gdrive:rclone/${1:t}.tar.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:rclone/${1:t}.tar.zst.gpg" \
|
|
| gpg -d \
|
|
| zstd -d -c \
|
|
| tar x -
|
|
}
|
|
|
|
# gd-ls lists all the files available on Google Drive
|
|
gd-ls() {
|
|
gd-check || return
|
|
rclone lsf --files-only gdrive:rclone/ \
|
|
| sed -n 's/\.tar\.zst\.gpg$//p'
|
|
}
|
|
|
|
# gd-rm removes a file from Google Drive
|
|
gd-rm() {
|
|
gd-check || return
|
|
rclone deletefile "gdrive:rclone/${1:t}.tar.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 <command> [args]"
|
|
print -u2 ""
|
|
print -u2 "commands:"
|
|
print -u2 " push <path> Compress, encrypt, and upload a file"
|
|
print -u2 " pull <path> Download, decrypt, and decompress to stdout"
|
|
print -u2 " ls List available files"
|
|
print -u2 " rm <path> 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
|
|
}
|