Bash: restic_env function
This commit is contained in:
parent
a89b864815
commit
f1352b4f0a
1 changed files with 47 additions and 0 deletions
|
@ -381,4 +381,51 @@ _please() {
|
||||||
}
|
}
|
||||||
complete -F _please 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 <env-name> [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:
|
# vim: set ft=bash ff=unix:
|
||||||
|
|
Loading…
Reference in a new issue