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