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