chiark / gitweb /
zsh-completion: add missing completions for systemd-run
[elogind.git] / shell-completion / bash / busctl
1 # busctl(1) completion                               -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
6 # Copyright 2014 Thomas H.P. Andersen
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 __contains_word () {
22         local w word=$1; shift
23         for w in "$@"; do
24                 [[ $w = "$word" ]] && return
25         done
26 }
27
28 __get_machines() {
29         local a b
30         machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
31 }
32
33 __get_busnames() {
34         local mode=$1
35         local a b
36         busctl $mode list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
37 }
38
39 __get_objects() {
40         local mode=$1
41         local busname=$2
42         local a b
43         busctl $mode tree --list --no-legend --no-pager $busname | { while read a b; do echo " $a"; done; };
44 }
45
46 __get_interfaces() {
47         local mode=$1
48         local busname=$2
49         local path=$3
50         local a b
51         busctl $mode introspect --list --no-legend --no-pager $busname $path | { while read a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; };
52 }
53
54 __get_members() {
55         local mode=$1
56         local busname=$2
57         local path=$3
58         local interface=$4
59         local type=$5
60         local a b
61         busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c; do [[ "$b" == "$type" ]] && echo " $a"; done; };
62 }
63
64 __get_signature() {
65         local mode=$1
66         local busname=$2
67         local path=$3
68         local interface=$4
69         local member=$5
70         local a b
71         busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" ]] && echo " \"$c\""; done; };
72 }
73
74 _busctl() {
75         local i verb comps mode
76         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
77         local -A OPTS=(
78                [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
79                              --show-machine --unique --acquired --activatable --list
80                              --quiet --verbose --expect-reply=no --auto-start=no
81                              --allow-interactive-authorization=yes --augment-creds=no'
82                       [ARG]='-H --host -M --machine --address --match --timeout'
83         )
84
85         if __contains_word "--user" ${COMP_WORDS[*]}; then
86                 mode=--user
87         else
88                 mode=--system
89         fi
90
91         if __contains_word "$prev" ${OPTS[ARG]}; then
92                 case $prev in
93                         --host|-H)
94                                 comps=$(compgen -A hostname)
95                         ;;
96                         --machine|-M)
97                                 comps=$( __get_machines )
98                 esac
99                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
100                 return 0
101         fi
102
103         if [[ "$cur" = -* ]]; then
104                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
105                 return 0
106         fi
107
108         local -A VERBS=(
109                 [STANDALONE]='list help'
110                 [BUSNAME]='status monitor capture tree'
111                 [OBJECT]='introspect'
112                 [METHOD]='call'
113                 [PROPERTY_GET]='get-property'
114                 [PROPERTY_SET]='set-property'
115         )
116
117         for ((i=0; i < COMP_CWORD; i++)); do
118                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
119                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
120                         verb=${COMP_WORDS[i]}
121                         break
122                 fi
123         done
124
125         n=$(($COMP_CWORD - $i))
126
127         if [[ -z $verb ]]; then
128                 comps=${VERBS[*]}
129         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
130                 comps=''
131         elif __contains_word "$verb" ${VERBS[BUSNAME]}; then
132                 comps=$( __get_busnames $mode)
133         elif __contains_word "$verb" ${VERBS[OBJECT]}; then
134                 if [[ $n -eq 1 ]] ; then
135                         comps=$( __get_busnames $mode)
136                 elif [[ $n -eq 2 ]] ; then
137                         comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
138                 elif [[ $n -eq 3 ]] ; then
139                         comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
140                 else
141                         comps=''
142                 fi
143         elif __contains_word "$verb" ${VERBS[METHOD]}; then
144                 if [[ $n -eq 1 ]] ; then
145                         comps=$( __get_busnames $mode)
146                 elif [[ $n -eq 2 ]] ; then
147                         comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
148                 elif [[ $n -eq 3 ]] ; then
149                         comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
150                 elif [[ $n -eq 4 ]] ; then
151                         comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method)
152                 elif [[ $n -eq 5 ]] ; then
153                         comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
154                 else
155                         comps=''
156                 fi
157         elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then
158                 if [[ $n -eq 1 ]] ; then
159                         comps=$( __get_busnames $mode)
160                 elif [[ $n -eq 2 ]] ; then
161                         comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
162                 elif [[ $n -eq 3 ]] ; then
163                         comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
164                 elif [[ $n -eq 4 ]] ; then
165                         comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
166                 else
167                         comps=''
168                 fi
169         elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then
170                 if [[ $n -eq 1 ]] ; then
171                         comps=$( __get_busnames $mode)
172                 elif [[ $n -eq 2 ]] ; then
173                         comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
174                 elif [[ $n -eq 3 ]] ; then
175                         comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
176                 elif [[ $n -eq 4 ]] ; then
177                         comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
178                 elif [[ $n -eq 5 ]] ; then
179                         comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
180                 else
181                         comps=''
182                 fi
183         fi
184
185         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
186         return 0
187 }
188
189 complete -F _busctl busctl