btrfs-progs: update btrfs-completion
[platform/upstream/btrfs-progs.git] / btrfs-completion
1 # original by Alfredo Esteban <aedelatorre at xxxxxxxxx>
2 # (http://www.spinics.net/lists/linux-btrfs/msg15899.html)
3 # edited by Joseph Wang <joequant at gmail.com>
4 # (http://lists.alioth.debian.org/pipermail/bash-completion-devel/2013-June/004868.html)
5 # edited by John C F <john.ch.fr at gmail.com> on 2015-02-02
6
7 _btrfs_devs()
8 {
9     local DEVS
10     DEVS=''; while read dev; do DEVS+="$dev "; done < <(lsblk -pnro name)
11         COMPREPLY+=( $( compgen -W "$DEVS" -- "$cur" ) )
12 }
13
14 _btrfs_mnts()
15 {
16         local MNTS
17         MNTS=''
18         while read mnt; do MNTS+="$mnt "
19         done < <(mount -t btrfs | awk '{print $3}')
20         COMPREPLY+=( $( compgen -W "$MNTS" -- "$cur" ) )
21 }
22
23 _btrfs()
24 {
25         local cur prev words cword
26     _init_completion || return
27
28     COMPREPLY=()
29
30         local cmd=${words[1]}
31
32     commands='subvolume filesystem balance device scrub check rescue restore inspect-internal property send receive quota qgroup replace help version'
33     commands_subvolume='create delete list snapshot find-new get-default set-default show sync'
34     commands_filesystem='defragment sync resize show df du label usage'
35     commands_balance='start pause cancel resume status'
36     commands_device='scan add delete remove ready stats usage'
37     commands_scrub='start cancel resume status'
38     commands_rescue='chunk-recover super-recover zero-log'
39     commands_inspect_internal='inode-resolve logical-resolve subvolid-resolve rootid min-dev-size dump-tree dump-super tree-stats'
40     commands_property='get set list'
41     commands_quota='enable disable rescan'
42     commands_qgroup='assign remove create destroy show limit'
43     commands_replace='start status cancel'
44
45         if [[ "$cur" == -* && $cword -le 3 && "$cmd" != "help" ]]; then
46                 COMPREPLY=( $( compgen -W '--help' -- "$cur" ) )
47                 return 0
48         fi
49
50     if [[ $cword -eq 1 ]]; then
51         COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
52         return 0
53     elif [[ $cword -eq 2 ]]; then
54         case $cmd in
55             subvolume)
56                 opts="$commands_subvolume"
57                 ;;
58             filesystem)
59                 opts="$commands_filesystem"
60                 ;;
61             balance)
62                 opts="$commands_balance"
63                 ;;
64             device)
65                 opts="$commands_device"
66                 ;;
67             scrub)
68                 opts="$commands_scrub"
69                 ;;
70             check)
71                 _btrfs_devs
72                                 return 0
73                 ;;
74             rescue)
75                 opts="$commands_rescue"
76                 ;;
77             restore)
78                 _btrfs_devs
79                                 return 0
80                 ;;
81             inspect-internal)
82                 opts="$commands_inspect_internal"
83                 ;;
84             property)
85                 opts="$commands_property"
86                 ;;
87             send|receive)
88                 _filedir -d
89                 return 0
90                 ;;
91             quota)
92                 opts="$commands_quota"
93                 ;;
94             qgroup)
95                 opts="$commands_qgroup"
96                 ;;
97             replace)
98                 opts="$commands_replace"
99                 ;;
100             help)
101                 opts="--full"
102                 ;;
103             version)
104                 return 0
105                 ;;
106         esac
107         COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
108         return 0
109     elif [[ $cword -eq 3 ]]; then
110         case $cmd in
111             filesystem)
112                                 case $prev in
113                                         defragment)
114                                                 _filedir
115                                                 return 0
116                                                 ;;
117                                         df|usage)
118                                                 _btrfs_mnts
119                                                 return 0
120                                                 ;;
121                                         label)
122                                                 _btrfs_mnts
123                                                 _btrfs_devs
124                                                 return 0
125                                                 ;;
126                                 esac
127                 ;;
128             device|rescue)
129                                 _btrfs_devs
130                 return 0
131                 ;;
132                         inspect-internal)
133                                 case $prev in
134                                         min-dev-size)
135                                                 _btrfs_mnts
136                                                 return 0
137                                                 ;;
138                                         rootid)
139                                                 _filedir
140                                                 return 0
141                                                 ;;
142                                 esac
143                                 ;;
144                         receive)
145                                 case $prev in
146                                         -f)
147                                                 _filedir
148                                                 return 0
149                                                 ;;
150                                 esac
151                                 ;;
152             replace)
153                 case $prev in
154                     status|cancel)
155                                                 _btrfs_mnts
156                         return 0
157                         ;;
158                     start)
159                                                 _btrfs_devs
160                         return 0
161                         ;;
162                 esac
163                 ;;
164                         subvolume)
165                                 case $prev in
166                                         list)
167                                                 _btrfs_mnts
168                                                 return 0
169                                                 ;;
170                                 esac
171                                 ;;
172         esac
173     fi
174
175     _filedir -d
176     return 0
177 }
178
179 complete -F _btrfs btrfs