chiark / gitweb /
build-sys: add missing makefile symlinks
[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_endpoints() {
34         local a b
35         busctl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
36 }
37
38 _busctl() {
39         local i verb comps
40         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
41         local -A OPTS=(
42                [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
43                              --show-machine --unique --acquired --activatable'
44                       [ARG]='-H --host -M --machine --address --match'
45         )
46
47         if __contains_word "$prev" ${OPTS[ARG]}; then
48                 case $prev in
49                         --host|-H)
50                                 comps=$(compgen -A hostname)
51                         ;;
52                         --machine|-M)
53                                 comps=$( __get_machines )
54                 esac
55                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
56                 return 0
57         fi
58
59         if [[ "$cur" = -* ]]; then
60                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
61                 return 0
62         fi
63
64         local -A VERBS=(
65                 [STANDALONE]='list help'
66                 [ENDPOINT]='monitor status'
67         )
68
69         for ((i=0; i < COMP_CWORD; i++)); do
70                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
71                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
72                         verb=${COMP_WORDS[i]}
73                         break
74                 fi
75         done
76
77         if [[ -z $verb ]]; then
78                 comps=${VERBS[*]}
79         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
80                 comps=''
81         elif __contains_word "$verb" ${VERBS[ENDPOINT]}; then
82                 comps=$( __get_endpoints )
83         fi
84
85         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
86         return 0
87 }
88
89 complete -F _busctl busctl