plugins: Wire up file-based plugin support for NBD_INFO_INIT_STATE
[nbdkit/ericb.git] / bash / nbdkit
blobc23314a80605e45986da3ec8c942351c316aedbf
1 # nbdkit bash completion script -*- shell-script -*-
2 # Copyright (C) 2010-2018 Red Hat Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # * Neither the name of Red Hat nor the names of its contributors may be
16 # used to endorse or promote products derived from this software without
17 # specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
32 _nbdkit_list_plugins ()
34     local plugindir
35     plugindir="$(
36         nbdkit --dump-config | grep ^plugindir= | sed 's/^plugindir=//'
37     )"
38     ls -1 "$plugindir" | sed 's/^nbdkit-//' | sed 's/-plugin.*//'
41 _nbdkit_list_filters ()
43     local filterdir
44     filterdir="$(
45         nbdkit --dump-config | grep ^filterdir= | sed 's/^filterdir=//'
46     )"
47     ls -1 "$filterdir" | sed 's/^nbdkit-//' | sed 's/-filter.*//'
50 # This handler function is called when the user presses tab.
51 _nbdkit ()
53     local cur prev words cword split
54     local shortopts longopts plugin plugins filters args i
56     _init_completion -s || return
58     # Did we get the plugin name yet?
59     # This is only a heuristic because it can be confused by
60     # long opt parameters with an arguments.  XXX
61     plugin=
62     for (( i=1; i < ${#words[@]}; ++i)) ; do
63         if [[ "${words[i]}" =~ ^[a-zA-Z0-9]+$ ]]; then
64             plugin="${words[i]}"
65             break
66         fi
67     done
69     # Previous item on the current line is a completable flag or plugin name?
70     case "$prev" in
71         --filter)
72             filters="$(_nbdkit_list_filters)"
73             COMPREPLY=( $(compgen -W "$filters" "$cur") )
74             return ;;
75         --tls)
76             COMPREPLY=( $(compgen -W "off on require" "$cur") )
77             return ;;
78         # Could complete -u and -g options too.  XXX
79     esac
81     # Current item is an option we can expand?
82     case "$cur" in
83         --*)
84             longopts="$(nbdkit --long-options)"
85             COMPREPLY=( $(compgen -W "$longopts" -- "$cur") )
86             return ;;
87         -*)
88             shortopts="$(nbdkit --short-options)"
89             longopts="$(nbdkit --long-options)"
90             COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") )
91             return ;;
92         *)
93             if [ "$plugin" = "" ] || [ "$plugin" = "$cur" ]; then
94                 # Complete plugin name.
95                 plugins="$(_nbdkit_list_plugins)"
96                 COMPREPLY=( $(compgen -W "$plugins" "$cur") )
97                 return
98             else
99                 # Complete plugin args.
100                 args="$(
101                     nbdkit $plugin --help 2>/dev/null |
102                         grep -E '^[-a-z0-9]+=' | sed 's/=.*/=/'
103                 )"
104                 COMPREPLY=( $(compgen -W "$args" "$cur") )
105                 return
106             fi
107             ;;
108     esac
111 # Install the handler function.
112 complete -o default -F _nbdkit nbdkit