From 905af590ac0ccce728d2fb127e25bbde4fb6cc02 Mon Sep 17 00:00:00 2001 From: sefidel Date: Thu, 27 Jul 2023 18:05:29 +0900 Subject: feat(home/base): add airport script --- home/profiles/base/default.nix | 1 + home/profiles/base/scripts/airport.nix | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 home/profiles/base/scripts/airport.nix diff --git a/home/profiles/base/default.nix b/home/profiles/base/default.nix index df2e672..43b56dd 100644 --- a/home/profiles/base/default.nix +++ b/home/profiles/base/default.nix @@ -53,6 +53,7 @@ in pkgs.gcc (pkgs.writeShellScriptBin "0x0" (import ./scripts/0x0.nix)) + (pkgs.writeShellScriptBin "airport" (import ./scripts/airport.nix)) ]; diff --git a/home/profiles/base/scripts/airport.nix b/home/profiles/base/scripts/airport.nix new file mode 100755 index 0000000..f123557 --- /dev/null +++ b/home/profiles/base/scripts/airport.nix @@ -0,0 +1,90 @@ +'' + # airport.sh - shared file store + # + # # Environment variables + # `AIRPORT_HOST` - host to rsync to + # `AIRPORT_USER` - username to connect with + # `AIRPORT_DIR` - defaults to `~/airport`, directory to use + + set -euo pipefail + + die () { + echo >&2 "$@" + exit 1 + } + + confirm() { + read -r -p "''${1:-Proceed?} [y/N] " response + case "$response" in + [yY][eE][sS]|[yY]) + true + ;; + *) + false + ;; + esac + } + + delay() { + DELAY_SECONDS=''${2:-5} + echo "''${1:-This script will perform some potentionally destructive actions.}" + echo "Waiting $DELAY_SECONDS second(s) before proceeding..." + sleep $DELAY_SECONDS + } + + bin_exists() { + [ -z $1 ] && die "FATAL: called bin_exists() without arguments (expected 1)" + if command -v $1 &> /dev/null + then + true + else + false + fi + } + + [ -f $HOME/.airport-env ] && source $HOME/.airport-env + + HOST=''${AIRPORT_HOST:?"AIRPORT_HOST needs to be set"} + USER=''${AIRPORT_USER:?"AIRPORT_USER needs to be set"} + DIR=''${AIRPORT_DIR:-"$HOME/airport"} + + [ "$#" -eq 0 ] && die "FATAL: expected exactly 1 argument, $# provided" + + if [ ! -d $DIR ] + then + confirm "$DIR doesn't exist, create and proceed?" || die "FATAL: $DIR doesn't exist" + mkdir -p $DIR + fi + + bin_exists "rsync" || die "FATAL: couldn't find rsync" + + case "$1" in + send) + delay "Airport will run 'rsync -au' to $DIR to SEND" + rsync -au --stats --info=progress2 --mkpath $DIR $USER@$HOST:airport + ;; + + recv) + delay "Airport will run 'rsync -au' to $DIR to RECV" + rsync -au --stats --info=progress2 $USER@$HOST:airport $DIR + ;; + + configure) + delay "Will write configuration to $HOME/.airport-env" + cat >$HOME/.airport-env <