From 73202f5e3b21fd4cd8fab25ef23d9b8fddd0ec999d9fc62be3d5e0de662d3f0e Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Sat, 22 Aug 2026 07:36:30 +0200 Subject: [PATCH] extract shared snapshot and archive selection logic --- pbc | 95 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/pbc b/pbc index 34d4404..cc70793 100755 --- a/pbc +++ b/pbc @@ -180,6 +180,30 @@ function may_select() { echo "$value" } +function select_group() { + local groups + readarray -t groups < <(get_group_names) + (( ${#groups[@]} == 0 )) && return 2 + + PS3="Snapshot group [1-${#groups[@]}]: " + GROUP=$(may_select "$1" "${groups[@]}") || { echo "Error: invalid group" >&2; return 1; } +} + +function select_snapshot_and_archive() { + local snapshots archives + readarray -t snapshots < <(get_snapshot_names) + (( ${#snapshots[@]} == 0 )) && return 2 + + PS3="Snapshot [1-${#snapshots[@]}]: " + SNAPSHOT=$(may_select "$1" "${snapshots[@]}") || { echo "Error: invalid snapshot" >&2; return 1; } + + readarray -t archives < <(get_snapshot_files "$SNAPSHOT") + (( ${#archives[@]} == 0 )) && return 3 + + PS3="Archive [1-${#archives[@]}]: " + ARCHIVE=$(may_select "$2" "${archives[@]}") || { echo "Error: invalid archive" >&2; return 1; } +} + SCRIPT_PATH=$(realpath -s "$0") SCRIPT_DIR=$(dirname "$(realpath "$0")") @@ -297,61 +321,38 @@ case "$action" in pbc-list "${args[@]}" || exit $? ;; snapshot-list) - readarray -t groups < <(get_group_names) - (( ${#groups[@]} == 0 )) && echo "no snapshot groups found" && exit 0 - - group=${args[0]} - args=("${args[@]:1}") - - PS3="Snapshot group [1-${#groups[@]}]: " - group=$(may_select "$group" "${groups[@]}") || { echo "Error: invalid group" >&2; exit 1; } + select_group "${args[0]}" + case $? in + 0) ;; + 2) echo "no snapshot groups found"; exit 0 ;; + *) exit 1 ;; + esac echo - pbc-snapshot-list "$group" "${args[@]}" || exit $? + pbc-snapshot-list "$GROUP" "${args[@]:1}" || exit $? ;; catalog-shell) - readarray -t snapshots < <(get_snapshot_names) - (( ${#snapshots[@]} == 0 )) && echo "no snapshots found" && exit 0 - - snapshot=${args[0]} - args=("${args[@]:1}") - - PS3="Snapshot [1-${#snapshots[@]}]: " - snapshot=$(may_select "$snapshot" "${snapshots[@]}") || { echo "Error: invalid snapshot" >&2; exit 1; } - - readarray -t archives < <(get_snapshot_files "$snapshot") - (( ${#archives[@]} == 0 )) && echo "no archives found" && exit 0 - - archive=${args[0]} - args=("${args[@]:1}") - - PS3="Archive [1-${#archives[@]}]: " - archive=$(may_select "$archive" "${archives[@]}") || { echo "Error: invalid archive" >&2; exit 1; } + select_snapshot_and_archive "${args[0]}" "${args[1]}" + case $? in + 0) ;; + 2) echo "no snapshots found"; exit 0 ;; + 3) echo "no archives found"; exit 0 ;; + *) exit 1 ;; + esac echo - pbc-catalog-shell "$snapshot" "$archive" || exit $? + pbc-catalog-shell "$SNAPSHOT" "$ARCHIVE" || exit $? ;; mount) - readarray -t snapshots < <(get_snapshot_names) - (( ${#snapshots[@]} == 0 )) && echo "no snapshots found" && exit 0 + select_snapshot_and_archive "${args[0]}" "${args[1]}" + case $? in + 0) ;; + 2) echo "no snapshots found"; exit 0 ;; + 3) echo "no archives found"; exit 0 ;; + *) exit 1 ;; + esac - snapshot=${args[0]} - args=("${args[@]:1}") - - PS3="Snapshot [1-${#snapshots[@]}]: " - snapshot=$(may_select "$snapshot" "${snapshots[@]}") || { echo "Error: invalid snapshot" >&2; exit 1; } - - readarray -t archives < <(get_snapshot_files "$snapshot") - (( ${#archives[@]} == 0 )) && echo "no archives found" && exit 0 - - archive=${args[0]} - args=("${args[@]:1}") - - PS3="Archive [1-${#archives[@]}]: " - archive=$(may_select "$archive" "${archives[@]}") || { echo "Error: invalid archive" >&2; exit 1; } - - target=${args[0]} - args=("${args[@]:1}") + target=${args[2]} if [ -z "$target" ]; then while [ -z "$target" ]; do @@ -367,7 +368,7 @@ case "$action" in fi echo - pbc-mount "$snapshot" "$archive" "$target" || exit $? + pbc-mount "$SNAPSHOT" "$ARCHIVE" "$target" || exit $? ;; install) (( EUID != 0 )) && echo "Error: installation only works as root!" >&2 && exit 1