summaryrefslogtreecommitdiff
path: root/btrfs-completion
blob: ae683f4ecf6194ad71c585ce370cd2bf402e3151 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# original by Alfredo Esteban <aedelatorre at xxxxxxxxx>
# (http://www.spinics.net/lists/linux-btrfs/msg15899.html)
# edited by Joseph Wang <joequant at gmail.com>
# (http://lists.alioth.debian.org/pipermail/bash-completion-devel/2013-June/004868.html)
# edited by John C F <john.ch.fr at gmail.com> on 2015-02-02

_btrfs_devs()
{
	local DEVS
	DEVS=''; while read dev; do DEVS+="$dev "; done < <(lsblk -pnro name)
	COMPREPLY+=( $( compgen -W "$DEVS" -- "$cur" ) )
}

_btrfs_mnts()
{
	local MNTS
	MNTS=''
	while read mnt; do MNTS+="$mnt "
	done < <(mount -t btrfs | awk '{print $3}')
	COMPREPLY+=( $( compgen -W "$MNTS" -- "$cur" ) )
}

_btrfs()
{
	local cur prev words cword
	_init_completion || return

	COMPREPLY=()

	local cmd=${words[1]}

	commands='subvolume filesystem balance device scrub check rescue restore inspect-internal property send receive quota qgroup replace help version'
	commands_subvolume='create delete list snapshot find-new get-default set-default show sync'
	commands_filesystem='defragment sync resize show df du label usage'
	commands_balance='start pause cancel resume status'
	commands_device='scan add delete remove ready stats usage'
	commands_scrub='start cancel resume status'
	commands_rescue='chunk-recover super-recover zero-log'
	commands_inspect_internal='inode-resolve logical-resolve subvolid-resolve rootid min-dev-size dump-tree dump-super tree-stats'
	commands_property='get set list'
	commands_quota='enable disable rescan'
	commands_qgroup='assign remove create destroy show limit'
	commands_replace='start status cancel'

	if [[ "$cur" == -* && $cword -le 3 && "$cmd" != "help" ]]; then
		COMPREPLY=( $( compgen -W '--help' -- "$cur" ) )
		return 0
	fi

	if [[ $cword -eq 1 ]]; then
		COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
		return 0
	elif [[ $cword -eq 2 ]]; then
		case $cmd in
			subvolume)
				opts="$commands_subvolume"
				;;
			filesystem)
				opts="$commands_filesystem"
				;;
			balance)
				opts="$commands_balance"
				;;
			device)
				opts="$commands_device"
				;;
			scrub)
				opts="$commands_scrub"
				;;
			check)
				_btrfs_devs
				return 0
				;;
			rescue)
				opts="$commands_rescue"
				;;
			restore)
				_btrfs_devs
				return 0
				;;
			inspect-internal)
				opts="$commands_inspect_internal"
				;;
			property)
				opts="$commands_property"
				;;
			send|receive)
				_filedir -d
				return 0
				;;
			quota)
				opts="$commands_quota"
				;;
			qgroup)
				opts="$commands_qgroup"
				;;
			replace)
				opts="$commands_replace"
				;;
			help)
				opts="--full"
				;;
			version)
				return 0
				;;
		esac
		COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
		return 0
	elif [[ $cword -eq 3 ]]; then
		case $cmd in
			filesystem)
				case $prev in
					defragment)
						_filedir
						return 0
						;;
					df|usage)
						_btrfs_mnts
						return 0
						;;
					label)
						_btrfs_mnts
						_btrfs_devs
						return 0
						;;
				esac
				;;
			device|rescue)
				_btrfs_devs
				return 0
				;;
			inspect-internal)
				case $prev in
					min-dev-size)
						_btrfs_mnts
						return 0
						;;
					rootid)
						_filedir
						return 0
						;;
				esac
				;;
			receive)
				case $prev in
					-f)
						_filedir
						return 0
						;;
				esac
				;;
			replace)
				case $prev in
					status|cancel)
						_btrfs_mnts
						return 0
						;;
					start)
						_btrfs_devs
						return 0
						;;
				esac
				;;
			subvolume)
				case $prev in
					list)
						_btrfs_mnts
						return 0
						;;
				esac
				;;
		esac
	fi

	_filedir -d
	return 0
}

complete -F _btrfs btrfs