Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: complete snapshot ranges for zfs destroy
- X-seq: zsh-workers 53395
- From: Oliver Kiddle <opk@xxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: PATCH: complete snapshot ranges for zfs destroy
- Date: Fri, 28 Feb 2025 14:42:08 +0100
- Archived-at: <https://zsh.org/workers/53395>
- List-id: <zsh-workers.zsh.org>
zfs destroy allows a list or range of snapshots to be specified. In the
man page the usage is shown as volume@snap[%snap[,snap[%snap]]]
This is useful with dry-run and verbose flags to see how much space can
be freed.
This handles this in the completion.
Oliver
diff --git a/Completion/Unix/Command/_zfs b/Completion/Unix/Command/_zfs
index 1afbf273c..850dfe52b 100644
--- a/Completion/Unix/Command/_zfs
+++ b/Completion/Unix/Command/_zfs
@@ -434,7 +434,7 @@ case $service:$words[1] in
'-R[recursively destroy all dependents]' \
'(-f)-d[delete or mark deferred]' \
'(-d)-f[force unmounts]' \
- ':dataset:_zfs_dataset -t fs -t vol ${=${opt_args[(i)-f]:--t snap}:/-f/} ${=${opt_args[(i)-*]:--t bookmark}:/-?/}'
+ ':dataset:_zfs_dataset -t fs -t vol ${=${opt_args[(i)-f]:--t snap -t range}:/-f/} ${=${opt_args[(i)-*]:--t bookmark}:/-?/}'
;;
zfs:snap(|shot))
diff --git a/Completion/Unix/Type/_zfs_dataset b/Completion/Unix/Type/_zfs_dataset
index 7edcfd5d7..0b3876148 100644
--- a/Completion/Unix/Type/_zfs_dataset
+++ b/Completion/Unix/Type/_zfs_dataset
@@ -1,6 +1,6 @@
#autoload
-local -a type expl_type_arr rsrc rdst paths_allowed
+local -a type expl_type_arr rsrc rdst paths_allowed suf
local -a typearg datasetlist expl mlist
local expl_type
@@ -9,11 +9,16 @@ local expl_type
# -r1 indicates that we're completing the source of a rename
# -r2 indicates that we're completing the destination of a rename
# -t takes arguments (what kinds of datasets) and can appear multiple times
+# in addition to the usual types
+# clone - cloned file systems
+# mtpt - add mountpoints
+# range - allow a range of snapshots
zparseopts -D -E e:=expl_type_arr p=paths_allowed r1=rsrc r2=rdst t+:=type
+suf=( -r '\n\t\- @\\#' )
[[ -n $type[(r)fs] ]] && typearg=( filesystem )
[[ -n $type[(r)vol] ]] && typearg+=( volume )
-[[ -n $type[(r)snap] ]] && typearg+=( snapshot )
+[[ -n $type[(r)snap] || -prefix *@ ]] && typearg+=( snapshot )
[[ -n $type[(r)share] && $implementation = solaris ]] && typearg+=( share )
[[ -n $type[(r)bookmark] && $implementation = openzfs ]] &&
typearg+=( bookmark )
@@ -62,7 +67,26 @@ fi
if [[ -n $type[(r)clone] ]]; then
datasetlist=( ${(f)"$(zfs list -H -o name,origin -t filesystem 2>/dev/null | awk -F$'\t' "\$2 != \"-\" {print \$1}")":#no cloned filesystems available} )
else
- datasetlist=( ${(f)"$(zfs list -H -o name $typearg 2>/dev/null)":#no datasets available} )
+ [[ -n $type[(r)range] && $implementation = openzfs ]] &&
+ typearg+=( -s creation ) suf=( -S% -r '\n\t\- ,' )
+ datasetlist=( ${(f)"$(zfs list -H -o name $typearg 2>/dev/null)":#no datasets available} )
+fi
+
+if [[ -n $type[(r)range] && -prefix *[%,] && $implementation = openzfs ]]; then
+ if [[ -n ${datasetlist[(r)${PREFIX%@*}@*]} ]]; then
+ datasetlist=( ${${(M)datasetlist:#${PREFIX%@*}@*}##*@} )
+ compset -P '*[@,]'
+ # if we can find the starting snapshot filter to later ones
+ [[ -prefix *% ]] && (( ${#datasetlist[(r)${PREFIX%\%*},-1]} > 2 )) &&
+ datasetlist=( ${${datasetlist[(r)${PREFIX%\%*},-1]}[2,-1]} )
+ expl_type=""
+ compset -P '*%' && suf=( -qS, ) expl_type="end "
+ _description snapshots expl "${expl_type}snapshot"
+ compadd $suf "$expl[@]" -a datasetlist
+ else
+ _message -e snapshots 'snapshot'
+ fi
+ return
fi
expl_type=${typearg[2,-1]//,/\/}
@@ -77,4 +101,4 @@ if [[ -n $expl_type_arr[2] ]]; then
fi
_description datasets expl "$expl_type"
-_multi_parts "$@" "$expl[@]" -q / datasetlist
+_multi_parts $suf "$@" "$expl[@]" -q / datasetlist
Messages sorted by:
Reverse Date,
Date,
Thread,
Author