chiark / gitweb /
3789492d723189ae314f8176f34f2dcb45d778cc
[elogind.git] / shell-completion / bash / machinectl
1 # machinectl(1) completion                      -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2014 Thomas H.P. Andersen
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
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 __contains_word() {
21         local w word=$1; shift
22         for w in "$@"; do
23                 [[ $w = "$word" ]] && return
24         done
25 }
26
27 __get_machines() {
28         local a b
29         machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
30 }
31
32 _machinectl() {
33         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34         local i verb comps
35
36         local -A OPTS=(
37                [STANDALONE]='--all -a --full --help -h --no-ask-password --no-legend --no-pager --version'
38                       [ARG]='--host -H --kill-who -M --machine --property -p --signal -s'
39         )
40
41         local -A VERBS=(
42                [STANDALONE]='list'
43                  [MACHINES]='status show terminate kill reboot login'
44         )
45
46         _init_completion || return
47
48         for ((i=0; i <= COMP_CWORD; i++)); do
49                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
50                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
51                         verb=${COMP_WORDS[i]}
52                         break
53                 fi
54         done
55
56         if __contains_word "$prev" ${OPTS[ARG]}; then
57                 case $prev in
58                         --signal|-s)
59                                 comps=$(compgen -A signal)
60                         ;;
61                         --kill-who)
62                                 comps='all leader'
63                         ;;
64                         --host|-H)
65                                 comps=$(compgen -A hostname)
66                         ;;
67                         --machine|-M)
68                                 comps=$( __get_machines )
69                         ;;
70                         --property|-p)
71                                 comps=''
72                         ;;
73                 esac
74                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
75                 return 0
76         fi
77
78         if [[ "$cur" = -* ]]; then
79                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
80                 return 0
81         fi
82
83         if [[ -z $verb ]]; then
84                 comps=${VERBS[*]}
85
86         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
87                 comps=''
88
89         elif __contains_word "$verb" ${VERBS[MACHINES]}; then
90                 comps=$( __get_machines )
91         fi
92
93         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
94         return 0
95 }
96
97 complete -F _machinectl machinectl