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