-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtrctl.bash-completion
More file actions
142 lines (126 loc) · 3.55 KB
/
Copy pathbtrctl.bash-completion
File metadata and controls
142 lines (126 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__btrctl_trunk_hosts() {
local btrctl_path
btrctl_path=$(type -P btrctl || true)
[ -n "$btrctl_path" ] || return
if [ "$(id -u)" -eq 0 ]; then
command "$btrctl_path" trunk hosts 2>/dev/null || true
elif [ -n "${BTRCTL_CONF:-}" ]; then
command sudo -n BTRCTL_CONF="$BTRCTL_CONF" -- "$btrctl_path" trunk hosts 2>/dev/null || true
else
command sudo -n -- "$btrctl_path" trunk hosts 2>/dev/null || true
fi
}
__btrctl() {
local cur word i first_arg kind expect="" options="" set_candidates=""
local host_seen=0 tag_seen=0 positional_count=0
cur="${COMP_WORDS[COMP_CWORD]}"
local commands="snapshot list latest rm tag trunk config help -h --help"
local trunk_commands="id backup hosts list rm"
COMPREPLY=()
if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
return
fi
if [ "${COMP_WORDS[1]}" = "trunk" ] && [ "$COMP_CWORD" -eq 2 ]; then
COMPREPLY=($(compgen -W "$trunk_commands" -- "$cur"))
return
fi
if [ "${COMP_WORDS[1]}" = "trunk" ] && [ "${COMP_WORDS[2]}" = "list" ] &&
[ "$COMP_CWORD" -eq 3 ]; then
[[ "$cur" = -* ]] && return
COMPREPLY=($(compgen -W "$(__btrctl_trunk_hosts)" -- "$cur"))
return
fi
if [ "${COMP_WORDS[1]}" = "trunk" ] && [ "${COMP_WORDS[2]}" = "rm" ] &&
[ "$COMP_CWORD" -eq 3 ]; then
[[ "$cur" = -* ]] && return
COMPREPLY=($(compgen -W "$(__btrctl_trunk_hosts | awk '{ print $0 "/" }')" -- "$cur"))
compopt -o nospace
return
fi
case "${COMP_WORDS[1]}" in
list|latest)
first_arg=2
kind="host-only"
;;
snapshot)
first_arg=2
kind="snapshot"
;;
rm)
first_arg=2
kind="rm"
;;
tag)
first_arg=2
kind="tag"
;;
trunk)
[ "${COMP_WORDS[2]}" = "backup" ] || return
first_arg=3
kind="trunk-backup"
;;
*) return ;;
esac
for ((i = first_arg; i < COMP_CWORD; i++)); do
word="${COMP_WORDS[i]}"
if [ -n "$expect" ]; then
case "$expect" in
host) host_seen=1 ;;
tag) tag_seen=1 ;;
esac
expect=""
continue
fi
case "$word" in
--host) expect="host" ;;
--tag)
[ "$kind" = "snapshot" ] || return
expect="tag"
;;
-*) return ;;
*)
case "$kind" in
host-only|snapshot) return ;;
esac
positional_count=$((positional_count + 1))
;;
esac
done
case "$expect" in
host)
COMPREPLY=($(compgen -W "$(__btrctl_trunk_hosts)" -- "$cur"))
return
;;
tag) return ;;
esac
case "$kind" in
host-only)
[ "$host_seen" -eq 0 ] && options="--host"
;;
snapshot)
[ "$host_seen" -eq 0 ] && options="--host"
[ "$tag_seen" -eq 0 ] && options="${options:+${options} }--tag"
;;
rm)
[ "$positional_count" -le 1 ] || return
[ "$host_seen" -eq 0 ] && options="--host"
if [ "$host_seen" -eq 0 ] && [ "$positional_count" -eq 0 ]; then
set_candidates=$(command btrctl list 2>/dev/null | awk 'NR > 1 { print $2 }')
fi
;;
tag)
[ "$positional_count" -le 2 ] || return
[ "$host_seen" -eq 0 ] && options="--host"
;;
trunk-backup)
[ "$positional_count" -le 1 ] || return
[ "$host_seen" -eq 0 ] && options="--host"
if [ "$host_seen" -eq 0 ] && [ "$positional_count" -eq 0 ]; then
set_candidates=$(command btrctl list 2>/dev/null | awk 'NR > 1 { print $2 }')
fi
;;
esac
COMPREPLY=($(compgen -W "${options}${options:+ }${set_candidates}" -- "$cur"))
}
complete -F __btrctl btrctl