From f1352b4f0ac85cf5e70e9cae61adc105ddaa5a33 Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Sun, 15 Sep 2024 11:45:20 -0600 Subject: [PATCH] Bash: restic_env function --- bash/.bash_functions | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/bash/.bash_functions b/bash/.bash_functions index 84d3694..a8923c3 100644 --- a/bash/.bash_functions +++ b/bash/.bash_functions @@ -381,4 +381,51 @@ _please() { } complete -F _please please +# Restic environments =============================================== +# Only load these for root +if [[ "$EUID" -eq 0 ]]; then + _restic_env() { + local curr=${COMP_WORDS[COMP_CWORD]} + local words=$(jq -r '.[].name' /etc/restic/config.json) + COMPREPLY=( $(compgen -W "$words" -- "$curr") ) + } + + restic_env() { + local name="${1}" + local config_path="${2:-/etc/restic/config.json}" + + if [[ -z "$name" ]]; then + echo 'USAGE: restic_env [config_path]' + return 1 + fi + + local env_json="$(jq -r --arg name "$name" '.[] | select(.name == $name)' "$config_path")" + + if [[ -z "$env_json" ]]; then + echo "Could not find environment name: '$name'" + return 1 + fi + + local repository=$(echo "$env_json" | jq -r '.env.repository') + local password=$(echo "$env_json" | jq -r '.env.password // empty') + local password_file=$(echo "$env_json" | jq -r '.env.password_file // empty') + + unset -v RESTIC_REPOSITORY RESTIC_PASSWORD RESTIC_PASSWORD_FILE + export RESTIC_REPOSITORY="$repository" + + if [[ -n "$password" ]]; then + export RESTIC_PASSWORD="$password" + elif [[ -n "$password_file" ]]; then + export RESTIC_PASSWORD_FILE="$password_file" + else + echo 'Failed to find password or password_file' + return 1 + fi + + echo "Switched to restic respository: $repository" + } + + complete -F _restic_env restic_env +fi + # vim: set ft=bash ff=unix: