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