components/zsh/Completion/Unix/Type/_zfs_dataset
branchs11-update
changeset 3021 b9308977c65b
parent 3020 a14aae53a1c7
child 3023 90190872ae3e
equal deleted inserted replaced
3020:a14aae53a1c7 3021:b9308977c65b
     1 #autoload
       
     2 
       
     3 local -a type expl_type_arr rsrc rdst paths_allowed
       
     4 local -a typearg datasetlist expl mlist
       
     5 local expl_type
       
     6 
       
     7 # -e takes an argument which is passed as as the "descr" argument to _wanted
       
     8 # -p indicates that filesystem paths, not just dataset names, are allowed
       
     9 # -r1 indicates that we're completing the source of a rename
       
    10 # -r2 indicates that we're completing the destination of a rename
       
    11 # -t takes arguments (what kinds of datasets) and can appear multiple times
       
    12 zparseopts -D -E e:=expl_type_arr p=paths_allowed r1=rsrc r2=rdst t+:=type
       
    13 
       
    14 [[ -n $type[(r)fs] ]]    && typearg=( filesystem )
       
    15 [[ -n $type[(r)vol] ]]   && typearg=( $typearg volume )
       
    16 [[ -n $type[(r)snap] ]]  && typearg=( $typearg snapshot )
       
    17 [[ -n $type[(r)share] ]]  && typearg=( $typearg share )
       
    18 if [[ -n $typearg ]]; then
       
    19 	typearg=( -t ${(j:,:)typearg} )
       
    20 # We know we're in zfs list if paths_allowed is non-empty.
       
    21 elif [[ -n $opt_args[-t] && ${#paths_allowed} -gt 0 ]]; then
       
    22 	typearg=( -t $opt_args[-t] )
       
    23 fi
       
    24 
       
    25 if [[ ${#paths_allowed} -gt 0 && $PREFIX == /* ]]; then
       
    26 	_path_files
       
    27 	return
       
    28 fi
       
    29 
       
    30 if [[ ${#rsrc} -gt 0 ]]; then
       
    31 	# With the -r option to zfs rename, we can only rename snapshots.  With the
       
    32 	# -p option, we can only rename filesystems and volumes.
       
    33 	if [[ -n $words[(r)-r] ]]; then
       
    34 		typearg=( -t snapshot )
       
    35 	elif [[ -n $words[(r)-p] ]]; then
       
    36 		typearg=( -t filesystem,volume )
       
    37 	else
       
    38 		typearg=( -t filesystem,share,snapshot,volume )
       
    39 	fi
       
    40 fi
       
    41 
       
    42 if [[ ${#rdst} -gt 0 ]]; then
       
    43 	if [[ ${words[CURRENT - 1]} == *@* ]]; then
       
    44 		# If we're renaming snapshots, there's nothing to complete, so
       
    45 		# we simply give instructions.  (In non-recursive cases, we
       
    46 		# could put the name of the snapshotted dataset first, but why
       
    47 		# bother with the long form?)
       
    48 		_message -e 'snapshot name (beginning with "@")'
       
    49 		return
       
    50 	else
       
    51 		# The parent dataset must be a filesystem, and can't rename
       
    52 		# a dataset into another pool.  Plus we hardcode the expl.
       
    53 		typearg=( -t fs -r ${${words[CURRENT - 1]}%%/*} )
       
    54 		expl_type_arr=( -e "parent dataset" )
       
    55 	fi
       
    56 fi
       
    57 
       
    58 if [[ -n $type[(r)clone] ]]; then
       
    59 	datasetlist=( ${="$(zfs list -H -o name,origin -t fs | awk "\$2 != \"-\" {print \$1}")":#no cloned filesystems available} )
       
    60 else
       
    61 	datasetlist=( ${="$(zfs list -H -o name $typearg)":#no datasets available} )
       
    62 fi
       
    63 
       
    64 expl_type=${typearg[2,-1]//,/\/}
       
    65 if [[ -n $type[(r)mtpt] ]]; then
       
    66 	mlist=( ${="$(zfs list -H -o mountpoint $typearg)":#no mountpoints available} )
       
    67 	datasetlist=( $datasetlist $mlist )
       
    68 	expl_type="$expl_type/mountpoint"
       
    69 fi
       
    70 
       
    71 if [[ -n $expl_type_arr[2] ]]; then
       
    72 	expl_type=$expl_type_arr[2]
       
    73 fi
       
    74 
       
    75 _wanted dataset expl "$expl_type" _multi_parts "$@" -q / datasetlist