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