chiark / gitweb /
bus: implicitly handle peer commands Ping() and GetMachineId()
[elogind.git] / shell-completion / bash / systemctl
1 # systemctl(1) completion                                 -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2010 Ran Benita
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 __systemctl() {
21         systemctl --full --no-legend "$@"
22 }
23
24 __contains_word () {
25         local word=$1; shift
26         for w in $*; do [[ $w = $word ]] && return 0; done
27         return 1
28 }
29
30 __filter_units_by_property () {
31         local property=$1 value=$2 ; shift 2
32         local units=("$@")
33         local props
34         IFS=$'\n' read -rd '' -a props < \
35             <(__systemctl show --property "$property" -- "${units[@]}")
36         for ((i=0; $i < ${#units[*]}; i++)); do
37                 if [[ "${props[i]}" = "$property=$value" ]]; then
38                         printf "%s\n" "${units[i]}"
39                 fi
40         done
41 }
42
43 __get_all_units      () { __systemctl list-units --all \
44         | { while read -r a b; do printf "%s\n" "$a"; done; }; }
45 __get_active_units   () { __systemctl list-units       \
46         | { while read -r a b; do printf "%s\n" "$a"; done; }; }
47 __get_inactive_units () { __systemctl list-units --all \
48         | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
49 __get_failed_units   () { __systemctl list-units       \
50         | { while read -r a b c d; do [[ $c == "failed"   ]] && printf "%s\n" "$a"; done; }; }
51 __get_enabled_units  () { __systemctl list-unit-files  \
52         | { while read -r a b c  ; do [[ $b == "enabled"  ]] && printf "%s\n" "$a"; done; }; }
53 __get_disabled_units () { __systemctl list-unit-files  \
54         | { while read -r a b c  ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
55 __get_masked_units   () { __systemctl list-unit-files  \
56         | { while read -r a b c  ; do [[ $b == "masked"   ]] && printf "%s\n" "$a"; done; }; }
57
58 _systemctl () {
59         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
60         local i verb comps
61
62         local -A OPTS=(
63                [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
64                              --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
65                              --quiet -q --privileged -P --system --user --version --runtime'
66                       [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
67         )
68
69         if __contains_word "$prev" ${OPTS[ARG]}; then
70                 case $prev in
71                         --signal|-s)
72                                 comps=$(compgen -A signal)
73                         ;;
74                         --type|-t)
75                                 comps='automount device mount path service snapshot socket swap target timer'
76                         ;;
77                         --kill-who)
78                                 comps='all control main'
79                         ;;
80                         --kill-mode)
81                                 comps='control-group process'
82                         ;;
83                         --root)
84                                 comps=$(compgen -A directory -- "$cur" )
85                                 compopt -o filenames
86                         ;;
87                         --host|-H)
88                                 comps=$(compgen -A hostname)
89                         ;;
90                         --property|-p)
91                                 comps=''
92                         ;;
93                 esac
94                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
95                 return 0
96         fi
97
98
99         if [[ "$cur" = -* ]]; then
100                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
101                 return 0
102         fi
103
104         local -A VERBS=(
105                 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset'
106             [ENABLED_UNITS]='disable reenable'
107            [DISABLED_UNITS]='enable'
108              [FAILED_UNITS]='reset-failed'
109           [STARTABLE_UNITS]='start'
110           [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
111          [ISOLATABLE_UNITS]='isolate'
112          [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
113         [RESTARTABLE_UNITS]='restart reload-or-restart'
114              [MASKED_UNITS]='unmask'
115                      [JOBS]='cancel'
116                 [SNAPSHOTS]='delete'
117                      [ENVS]='set-environment unset-environment'
118                [STANDALONE]='daemon-reexec daemon-reload default dump
119                              emergency exit halt hibernate hybrid-sleep kexec list-jobs
120                              list-units list-unit-files poweroff reboot rescue
121                              show-environment suspend'
122                      [NAME]='snapshot load'
123                      [FILE]='link'
124         )
125
126         for ((i=0; $i <= $COMP_CWORD; i++)); do
127                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
128                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
129                         verb=${COMP_WORDS[i]}
130                         break
131                 fi
132         done
133
134         if   [[ -z $verb ]]; then
135                 comps="${VERBS[*]}"
136
137         elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
138                 comps=$( __get_all_units )
139
140         elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
141                 comps=$( __get_enabled_units )
142
143         elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
144                 comps=$( __get_disabled_units )
145
146         elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
147                 comps=$( __filter_units_by_property CanStart yes \
148                       $( __get_inactive_units \
149                         | while read -r line; do \
150                                 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
151                         done ))
152
153         elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
154                 comps=$( __filter_units_by_property CanStart yes \
155                       $( __get_all_units \
156                         | while read -r line; do \
157                                 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
158                         done ))
159
160         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
161                 comps=$( __filter_units_by_property CanStop yes \
162                       $( __get_active_units ) )
163
164         elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
165                 comps=$( __filter_units_by_property CanReload yes \
166                       $( __get_active_units ) )
167
168         elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
169                 comps=$( __filter_units_by_property AllowIsolate yes \
170                       $( __get_all_units ) )
171
172         elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
173                 comps=$( __get_failed_units )
174
175         elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
176                 comps=$( __get_masked_units )
177
178         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
179                 comps=''
180
181         elif __contains_word "$verb" ${VERBS[JOBS]}; then
182                 comps=$( __systemctl list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
183
184         elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
185                 comps=$( __systemctl list-units --type snapshot --full --all \
186                         | { while read -r a b; do printf "%s\n" "$a"; done; } )
187
188         elif __contains_word "$verb" ${VERBS[ENVS]}; then
189                 comps=$( __systemctl show-environment \
190                     | while read -r line; do printf "%s\n" "${line%%=*}=";done )
191                 compopt -o nospace
192
193         elif __contains_word "$verb" ${VERBS[FILE]}; then
194                 comps=$( compgen -A file -- "$cur" )
195                 compopt -o filenames
196         fi
197
198         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
199         return 0
200 }
201
202 complete -F _systemctl systemctl