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