validate datasets exist before running

- this allows for early exit as well as better error messaging
This commit is contained in:
Dave Eddy 2022-06-12 15:05:07 -04:00
parent 701762f1ec
commit 89b9c8a416
2 changed files with 41 additions and 6 deletions

View File

@ -6,11 +6,18 @@ Not Yet Released
(nothing yet) (nothing yet)
`v1.5.0`
--------
- Sanity check datasets existence before running
- `-q` will hide warnings for datesets not existing
- Based on ([#9](https://github.com/bahamas10/zfs-prune-snapshots/pull/9))
`v1.4.1` `v1.4.1`
-------- --------
- Allow snapshot datasets to contain spaces - Allow snapshot datasets to contain spaces
([#18](https://github.com/bahamas10/zfs-prune-snapshots/pull/18) ([#18](https://github.com/bahamas10/zfs-prune-snapshots/pull/18))
`v1.4.0` `v1.4.0`
-------- --------

View File

@ -69,7 +69,7 @@ EOF
} }
debug() { debug() {
((verbosity >= 1)) && echo "$@" >&2 ((verbosity >= 1)) && echo '>' "$@" >&2
return 0 return 0
} }
@ -174,6 +174,7 @@ verbosity=0
prefix= prefix=
suffix= suffix=
invert=false invert=false
quiet=false
while getopts 'hniqlRp:s:vV' option; do while getopts 'hniqlRp:s:vV' option; do
case "$option" in case "$option" in
h) usage; exit 0;; h) usage; exit 0;;
@ -182,7 +183,7 @@ while getopts 'hniqlRp:s:vV' option; do
l) listonly=true;; l) listonly=true;;
p) prefix=$OPTARG;; p) prefix=$OPTARG;;
s) suffix=$OPTARG;; s) suffix=$OPTARG;;
q) exec 1>/dev/null;; q) quiet=true; exec 1>/dev/null;;
R) recursive=true;; R) recursive=true;;
v) ((verbosity++));; v) ((verbosity++));;
V) echo "$VERSION"; exit 0;; V) echo "$VERSION"; exit 0;;
@ -221,13 +222,10 @@ fi
shift shift
pools=("$@")
destroyargs=() destroyargs=()
code=0 code=0
totalused=0 totalused=0
numsnapshots=0 numsnapshots=0
humanpools=${pools[*]}
humanpools=${humanpools:-<all>}
if $recursive; then if $recursive; then
destroyargs+=('-R') destroyargs+=('-R')
@ -243,6 +241,36 @@ if ! command -v zfs &>/dev/null; then
exit 1 exit 1
fi fi
# validate pools given, print warnings if not in quiet mode
pools=()
for arg in "$@"; do
debug "checking '$arg' exists"
error=$(zfs list "$arg" 2>&1 1>/dev/null)
code=$?
debug "zfs list '$arg' -> exited $code"
if ((code == 0)) && [[ -z $error ]]; then
debug "adding dataset '$arg'"
pools+=("$arg")
else
msg="dataset '$arg' invalid: $error"
debug "$msg"
if ! $quiet; then
echo "$msg" >&2
fi
fi
done
# it is an error if arguments were given but all datasets were invalidated
if [[ -n $1 && -z ${pools[0]} ]]; then
echo 'no valid dataset names provided' >&2
exit 1
fi
humanpools=${pools[*]}
humanpools=${humanpools:-<all>}
# first pass of the pools (to calculate totals and filter unwanted datasets # first pass of the pools (to calculate totals and filter unwanted datasets
lines=() lines=()
while read -r line; do while read -r line; do