aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/bash_completion.d/zfs.in
blob: 8898fc735cd13d9437e610e03fd38aff7c8ff55a (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# Copyright (c) 2010-2016, Aneurin Price <aneurin.price@gmail.com>

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

__ZFS_CMD="@sbindir@/zfs"
__ZPOOL_CMD="@sbindir@/zpool"

# Disable bash's built-in hostname completion, as this makes it impossible to
# provide completions containing an @-sign, which is necessary for completing
# snapshot names. If bash_completion is in use, this will already be disabled
# and replaced with better completions anyway.
shopt -u hostcomplete

__zfs_get_commands()
{
    $__ZFS_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | cut -f1 -d '|' | uniq
}

__zfs_get_properties()
{
    $__ZFS_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all name space
}

__zfs_get_editable_properties()
{
    $__ZFS_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
}

__zfs_get_inheritable_properties()
{
    $__ZFS_CMD get 2>&1 | awk '$3 == "YES" {print $1}'
}

__zfs_list_datasets()
{
    $__ZFS_CMD list -H -o name -s name -t filesystem,volume "$@"
}

__zfs_list_filesystems()
{
    $__ZFS_CMD list -H -o name -s name -t filesystem
}

__zfs_match_snapshot()
{
    local base_dataset=${cur%@*}
    if [[ $base_dataset != $cur ]]
    then
        $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 $base_dataset
    else
        if [[ $cur != "" ]] && __zfs_list_datasets $cur &> /dev/null
        then
            $__ZFS_CMD list -H -o name -s name -t filesystem -r $cur | tail -n +2
            # We output the base dataset name even though we might be
            # completing a command that can only take a snapshot, because it
            # prevents bash from considering the completion finished when it
            # ends in the bare @.
            echo $cur
            echo $cur@
        else
            local datasets=$(__zfs_list_datasets)
            # As above
            echo $datasets
            if [[ "$cur" == */ ]]
            then
                # If the current command ends with a slash, then the only way
                # it can be completed with a single tab press (ie. in this pass)
                # is if it has exactly one child, so that's the only time we
                # need to offer a suggestion with an @ appended.
                local num_children
                # This is actually off by one as zfs list includes the named
                # dataset in addition to its children
                num_children=$(__zfs_list_datasets -d 1 ${cur%/} 2> /dev/null | wc -l)
                if [[ $num_children != 2 ]]
                then
                    return 0
                fi
            fi
            echo "$datasets" | awk '{print $1"@"}'
        fi
    fi
}

__zfs_match_snapshot_or_bookmark()
{
    local base_dataset=${cur%[#@]*}
    if [[ $base_dataset != $cur ]]
    then
        if [[ $cur == *@* ]]
        then
            $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 $base_dataset
        else
            $__ZFS_CMD list -H -o name -s name -t bookmark -d 1 $base_dataset
        fi
    else
        $__ZFS_CMD list -H -o name -s name -t filesystem,volume
        if [[ $cur != "" ]] && $__ZFS_CMD list -H -o name -s name -t filesystem,volume $cur &> /dev/null
        then
            echo $cur@
            echo $cur#
        fi
    fi
}

__zfs_match_multiple_snapshots()
{
    local existing_opts=$(expr "$cur" : '\(.*\)[%,]')
    if [[ $existing_opts ]]
    then
        local base_dataset=${cur%@*}
        if [[ $base_dataset != $cur ]]
        then
            local cur=${cur##*,}
            if [[ $cur =~ ^%|%.*% ]]
            then
                # correct range syntax is start%end
                return 1
            fi
            local range_start=$(expr "$cur" : '\(.*%\)')
            $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 $base_dataset | sed 's$.*@$'$range_start'$g'
        fi
    else
        __zfs_match_snapshot_or_bookmark
    fi
}

__zfs_list_volumes()
{
    $__ZFS_CMD list -H -o name -s name -t volume
}

__zfs_argument_chosen()
{
    local word property
    for word in $(seq $((COMP_CWORD-1)) -1 2)
    do
        local prev="${COMP_WORDS[$word]}"
        if [[ ${COMP_WORDS[$word-1]} != -[tos] ]]
        then
            if [[ "$prev" == [^,]*,* ]] || [[ "$prev" == *[@:\#]* ]]
            then
                return 0
            fi
            for property in $@
            do
                if [[ $prev == "$property"* ]]
                then
                    return 0
                fi
            done
        fi
    done
    return 1
}

__zfs_complete_ordered_arguments()
{
    local list1=$1
    local list2=$2
    local cur=$3
    local extra=$4
    if __zfs_argument_chosen $list1
    then
        COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
    else
        COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
    fi
}

__zfs_complete_multiple_options()
{
    local options=$1
    local cur=$2

    COMPREPLY=($(compgen -W "$options" -- "${cur##*,}"))
    local existing_opts=$(expr "$cur" : '\(.*,\)')
    if [[ $existing_opts ]]
    then
        COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
    fi
}

__zfs_complete_switch()
{
    local options=$1
    if [[ ${cur:0:1} == - ]]
    then
        COMPREPLY=($(compgen -W "-{$options}" -- "$cur"))
        return 0
    else
        return 1
    fi
}

__zfs_complete_nospace()
{
    # Google indicates that there may still be bash versions out there that
    # don't have compopt.
    if type compopt &> /dev/null
    then
        compopt -o nospace
    fi
}

__zfs_complete()
{
    local cur prev cmd cmds
    COMPREPLY=()
    if type _get_comp_words_by_ref &> /dev/null
    then
        # Don't split on colon
        _get_comp_words_by_ref -n : -c cur -p prev -w COMP_WORDS -i COMP_CWORD
    else
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
    fi
    cmd="${COMP_WORDS[1]}"

    if [[ ${prev##*/} == zfs ]]
    then
        cmds=$(__zfs_get_commands)
        COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
        return 0
    fi

    case "${cmd}" in
        bookmark)
            if __zfs_argument_chosen
            then
                COMPREPLY=($(compgen -W "${prev%@*}# ${prev/@/#}" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
            fi
            ;;
        clone)
            case "${prev}" in
                -o)
                    COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
                    __zfs_complete_nospace
                    ;;
                *)
                    if ! __zfs_complete_switch "o,p"
                    then
                        if __zfs_argument_chosen
                        then
                            COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                        fi
                    fi
                    ;;
            esac
            ;;
        get)
            case "${prev}" in
                -d)
                    COMPREPLY=($(compgen -W "" -- "$cur"))
                    ;;
                -t)
                    __zfs_complete_multiple_options "filesystem volume snapshot bookmark all" "$cur"
                    ;;
                -s)
                    __zfs_complete_multiple_options "local default inherited temporary received none" "$cur"
                    ;;
                -o)
                    __zfs_complete_multiple_options "name property value source received all" "$cur"
                    ;;
                *)
                    if ! __zfs_complete_switch "H,r,p,d,o,t,s"
                    then
                        if __zfs_argument_chosen $(__zfs_get_properties)
                        then
                            COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                        else
                            __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
                        fi
                    fi
                    ;;
            esac
            ;;
        inherit)
            if ! __zfs_complete_switch "r"
            then
                __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_match_snapshot)" $cur
            fi
            ;;
        list)
            case "${prev}" in
                -d)
                    COMPREPLY=($(compgen -W "" -- "$cur"))
                    ;;
                -t)
                    __zfs_complete_multiple_options "filesystem volume snapshot bookmark all" "$cur"
                    ;;
                -o)
                    __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
                    ;;
                -s|-S)
                    COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "$cur"))
                    ;;
                *)
                    if ! __zfs_complete_switch "H,r,d,o,t,s,S"
                    then
                        COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                    fi
                    ;;
            esac
            ;;
        promote)
            COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
            ;;
        rollback)
            if ! __zfs_complete_switch "r,R,f"
            then
                COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
            fi
            ;;
        send)
            if ! __zfs_complete_switch "D,n,P,p,R,v,e,L,i,I"
            then
                if __zfs_argument_chosen
                then
                    COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                else
                    if [[ $prev == -*i* ]]
                    then
                        COMPREPLY=($(compgen -W "$(__zfs_match_snapshot_or_bookmark)" -- "$cur"))
                    else
                        COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                    fi
                fi
            fi
            ;;
        snapshot)
            case "${prev}" in
                -o)
                    COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
                    __zfs_complete_nospace
                    ;;
                *)
                    if ! __zfs_complete_switch "o,r"
                    then
                        COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
                        __zfs_complete_nospace
                    fi
                    ;;
            esac
            ;;
        set)
            __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_match_snapshot)" $cur
            __zfs_complete_nospace
            ;;
        upgrade)
            case "${prev}" in
                -a|-V|-v)
                    COMPREPLY=($(compgen -W "" -- "$cur"))
                    ;;
                *)
                    if ! __zfs_complete_switch "a,V,v,r"
                    then
                        COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
                    fi
                    ;;
            esac
            ;;
        destroy)
            if ! __zfs_complete_switch "d,f,n,p,R,r,v"
            then
                __zfs_complete_multiple_options "$(__zfs_match_multiple_snapshots)" $cur
                __zfs_complete_nospace
            fi
            ;;
        *)
            COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
            ;;
    esac
    if type __ltrim_colon_completions &> /dev/null
    then
        __ltrim_colon_completions "$cur"
    fi
    return 0
}

__zpool_get_commands()
{
    $__ZPOOL_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
}

__zpool_get_properties()
{
    $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
}

__zpool_get_editable_properties()
{
    $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
}

__zpool_list_pools()
{
    $__ZPOOL_CMD list -H -o name
}

__zpool_complete()
{
    local cur prev cmd cmds
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd="${COMP_WORDS[1]}"

    if [[ ${prev##*/} == zpool ]]
    then
        cmds=$(__zpool_get_commands)
        COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
        return 0
    fi

    case "${cmd}" in
        get)
            __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
            return 0
            ;;
        import)
            if [[ $prev == -d ]]
            then
                _filedir -d
            else
                COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
            fi
            return 0
            ;;
        set)
            __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
            __zfs_complete_nospace
            return 0
            ;;
        add|attach|clear|create|detach|offline|online|remove|replace)
            local pools="$(__zpool_list_pools)"
            if __zfs_argument_chosen $pools
            then
                _filedir
            else
                COMPREPLY=($(compgen -W "$pools" -- "$cur"))
            fi
            return 0
            ;;
        *)
            COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
            return 0
            ;;
    esac

}

complete -F __zfs_complete zfs
complete -F __zpool_complete zpool