Skip to content

Commit ff9efe9

Browse files
morph027oetiker
authored andcommitted
started basic bash completion (oetiker#242)
* started basic bash completion * extend bash completion for list
1 parent 7b2fec2 commit ff9efe9

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

bash_completion.d/znapzendzetup

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2016, Stefan Heitmüller <[email protected]>
4+
5+
# Permission is hereby granted, free of charge, to any person
6+
# obtaining a copy of this software and associated documentation
7+
# files (the "Software"), to deal in the Software without
8+
# restriction, including without limitation the rights to use,
9+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the
11+
# Software is furnished to do so, subject to the following
12+
# conditions:
13+
14+
# The above copyright notice and this permission notice shall be
15+
# included in all copies or substantial portions of the Software.
16+
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
# OTHER DEALINGS IN THE SOFTWARE.
25+
26+
if [[ -w /dev/zfs ]]; then
27+
__ZFS_CMD="zfs"
28+
__ZNAPZENDZETUP_CMD="znapzendzetup"
29+
else
30+
__ZFS_CMD="sudo zfs"
31+
__ZNAPZENDZETUP_CMD="sudo znapzendzetup"
32+
fi
33+
34+
__znapzendzetup_get_commands()
35+
{
36+
$__ZNAPZENDZETUP_CMD | awk '/^\s{8}[a-z]/ {print $1}' | cut -f1 -d '|' | uniq | sort
37+
}
38+
39+
__znapzendzetup_list_datasets()
40+
{
41+
$__ZNAPZENDZETUP_CMD list 2>&1 | grep -E '^*** backup plan: .* ***$' | awk '{print $(NF-1)}' | uniq | sort
42+
}
43+
44+
__zfs_list_datasets()
45+
{
46+
$__ZFS_CMD list -H -o name -t filesystem,volume
47+
}
48+
49+
__znapzendzetup_complete()
50+
{
51+
local cur prev cmd cmds
52+
COMPREPLY=()
53+
# Don't split on colon
54+
_get_comp_words_by_ref -n : -c cur -p prev -w COMP_WORDS -i COMP_CWORD
55+
cmd="${COMP_WORDS[1]}"
56+
57+
if [[ ${prev##*/} == znapzendzetup ]]
58+
then
59+
cmds=$(__znapzendzetup_get_commands)
60+
COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
61+
return 0
62+
fi
63+
64+
case "${cmd}" in
65+
create|delete|edit|enable|disable|export|list)
66+
COMPREPLY=($(compgen -W "$(__znapzendzetup_list_datasets)" -- "$cur"))
67+
;;
68+
import)
69+
COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
70+
;;
71+
esac
72+
73+
__ltrim_colon_completions "$cur"
74+
return 0
75+
}
76+
77+
complete -F __znapzendzetup_complete znapzendzetup

0 commit comments

Comments
 (0)