]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl.in
shell-completion: use 4 space indentation too
[thirdparty/systemd.git] / shell-completion / bash / systemctl.in
CommitLineData
d611dadc 1# systemctl(1) completion -*- shell-script -*-
7059062c 2# SPDX-License-Identifier: LGPL-2.1+
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
7
8__systemctl() {
843cfcb1
ZJS
9 local mode=$1; shift 1
10 systemctl $mode --full --no-legend --no-pager "$@" 2>/dev/null
d611dadc
MB
11}
12
caffaf58 13__systemd_properties() {
843cfcb1 14 @rootlibexecdir@/systemd --dump-bus-properties
caffaf58
ZJS
15}
16
d611dadc 17__contains_word () {
843cfcb1
ZJS
18 local w word=$1; shift
19 for w in "$@"; do
20 [[ $w = "$word" ]] && return
21 done
d611dadc
MB
22}
23
b1bdb649 24__filter_units_by_properties () {
843cfcb1
ZJS
25 local mode=$1 properties=$2; shift 2
26 local units=("$@")
27 local props i p n
28 local names= count=0
29
30 IFS=$',' read -r -a p < <(echo "Names,$properties")
31 n=${#p[*]}
32 readarray -t props < \
33 <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}")
34
35 for ((i=0; i < ${#props[*]}; i++)); do
36 if [[ -z ${props[i]} ]]; then
37 if (( count == n )) && [[ -n $names ]]; then
f28255e2 38 echo $names
843cfcb1
ZJS
39 fi
40 names=
41 count=0
42 else
43 (( count++ ))
44 if [[ ${props[i]%%=*} == 'Names' ]]; then
45 names=${props[i]#*=}
46 fi
f28255e2 47 fi
843cfcb1
ZJS
48 done
49 if (( count == n )) && [[ -n $names ]]; then
50 echo $names
51 fi
b1bdb649
YW
52}
53
98476dc8 54__get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 55 | { while read -r a b; do echo " $a"; done; }; }
98476dc8 56__get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 57 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
c839b729 58__get_template_names () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 59 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
c839b729 60__get_active_units () { __systemctl $1 list-units "$2*" \
843cfcb1 61 | { while read -r a b; do echo " $a"; done; }; }
f28255e2
YW
62
63__get_not_masked_unit_files() {
843cfcb1
ZJS
64 # filter out masked, not-found, or template units.
65 __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \
66 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
f28255e2
YW
67}
68
f29c77bc 69__get_startable_units () {
843cfcb1
ZJS
70 __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $(
71 { __get_not_masked_unit_files $1 $2
72 # get inactive template units
73 __systemctl $1 list-units --state inactive,failed "$2*" | \
74 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
75 } | sort -u )
9ff8af54 76}
f29c77bc 77__get_restartable_units () {
843cfcb1
ZJS
78 # filter out masked and not-found
79 __filter_units_by_properties $1 CanStart=yes $(
80 { __get_not_masked_unit_files $1 $2
81 __get_active_units $1 $2
82 } | sort -u )
f28255e2
YW
83}
84
85__get_stoppable_units () {
843cfcb1
ZJS
86 # filter out masked and not-found
87 local units=$(
88 { __get_not_masked_unit_files $1 $2
89 __get_active_units $1 $2
90 } | sort -u )
91 __filter_units_by_properties $1 ActiveState=active,CanStop=yes $units
92 __filter_units_by_properties $1 ActiveState=reloading,CanStop=yes $units
93 __filter_units_by_properties $1 ActiveState=activating,CanStop=yes $units
9ff8af54 94}
f28255e2
YW
95
96__get_reloadable_units () {
843cfcb1
ZJS
97 # filter out masked and not-found
98 __filter_units_by_properties $1 ActiveState=active,CanReload=yes $(
99 { __get_not_masked_unit_files $1 $2
100 __get_active_units $1 $2
101 } | sort -u )
f28255e2
YW
102}
103
c839b729 104__get_failed_units () { __systemctl $1 list-units "$2*" \
843cfcb1 105 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
c839b729 106__get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 107 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
c839b729 108__get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 109 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
c839b729 110__get_masked_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 111 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
98476dc8 112__get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; }
d611dadc 113
3a221b5d 114__get_machines() {
843cfcb1
ZJS
115 local a b
116 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
117 { while read a b; do echo " $a"; done; }
3a221b5d
EV
118}
119
d611dadc 120_systemctl () {
843cfcb1
ZJS
121 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
122 local i verb comps mode
d611dadc 123
843cfcb1
ZJS
124 local -A OPTS=(
125 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
0067c7b2 126 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
035dd8c0
YW
127 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
128 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
843cfcb1 129 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
035dd8c0 130 --preset-mode -n --lines -o --output -M --machine --message'
843cfcb1
ZJS
131 )
132
133 if __contains_word "--user" ${COMP_WORDS[*]}; then
134 mode=--user
135 elif __contains_word "--global" ${COMP_WORDS[*]}; then
136 mode=--user
137 else
138 mode=--system
139 fi
140
141 if __contains_word "$prev" ${OPTS[ARG]}; then
142 case $prev in
143 --signal|-s)
144 _signals
145 return
146 ;;
147 --type|-t)
148 comps=$(__systemctl $mode -t help)
149 ;;
150 --state)
151 comps=$(__systemctl $mode --state=help)
152 ;;
153 --job-mode)
154 comps='fail replace replace-irreversibly isolate
903e7c37 155 ignore-dependencies ignore-requirements flush'
843cfcb1
ZJS
156 ;;
157 --kill-who)
158 comps='all control main'
159 ;;
160 --root)
161 comps=$(compgen -A directory -- "$cur" )
162 compopt -o filenames
163 ;;
164 --host|-H)
165 comps=$(compgen -A hostname)
166 ;;
167 --property|-p)
168 comps=$(__systemd_properties)
169 ;;
170 --preset-mode)
171 comps='full enable-only disable-only'
172 ;;
173 --output|-o)
174 comps=$( systemctl --output=help 2>/dev/null )
175 ;;
176 --machine|-M)
177 comps=$( __get_machines )
178 ;;
179 esac
180 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
181 return 0
182 fi
d611dadc 183
843cfcb1
ZJS
184 if [[ "$cur" = -* ]]; then
185 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
186 return 0
187 fi
d611dadc 188
843cfcb1
ZJS
189 local -A VERBS=(
190 [ALL_UNITS]='cat mask'
aea447c9 191 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
843cfcb1
ZJS
192 [ENABLED_UNITS]='disable'
193 [DISABLED_UNITS]='enable'
c2e09812 194 [REENABLABLE_UNITS]='reenable'
843cfcb1
ZJS
195 [FAILED_UNITS]='reset-failed'
196 [STARTABLE_UNITS]='start'
197 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
198 [ISOLATABLE_UNITS]='isolate'
199 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
d611dadc 200 [RESTARTABLE_UNITS]='restart reload-or-restart'
843cfcb1
ZJS
201 [TARGET_AND_UNITS]='add-wants add-requires'
202 [MASKED_UNITS]='unmask'
203 [JOBS]='cancel'
204 [ENVS]='set-environment unset-environment import-environment'
205 [STANDALONE]='daemon-reexec daemon-reload default
c58493c0 206 emergency exit halt hibernate hybrid-sleep
e68c79db 207 suspend-then-hibernate kexec list-jobs list-sockets
c58493c0 208 list-timers list-units list-unit-files poweroff
1cf3c30c 209 reboot rescue show-environment suspend get-default
035dd8c0 210 is-system-running preset-all'
843cfcb1
ZJS
211 [FILE]='link switch-root'
212 [TARGETS]='set-default'
213 [MACHINES]='list-machines'
214 )
215
216 for ((i=0; i < COMP_CWORD; i++)); do
217 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
218 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
219 verb=${COMP_WORDS[i]}
220 break
221 fi
222 done
d611dadc 223
843cfcb1
ZJS
224 if [[ -z $verb ]]; then
225 comps="${VERBS[*]}"
aea447c9 226
843cfcb1
ZJS
227 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
228 comps=$( __get_all_units $mode "$cur" )
229 compopt -o filenames
d611dadc 230
843cfcb1
ZJS
231 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
232 comps=$( __get_non_template_units $mode "$cur" )
233 compopt -o filenames
d611dadc 234
843cfcb1
ZJS
235 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
236 comps=$( __get_enabled_units $mode "$cur" )
237 compopt -o filenames
c2e09812 238
843cfcb1
ZJS
239 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
240 comps=$( __get_disabled_units $mode "$cur";
241 __get_template_names $mode "$cur")
242 compopt -o filenames
d611dadc 243
843cfcb1
ZJS
244 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
245 comps=$( __get_disabled_units $mode "$cur";
246 __get_enabled_units $mode "$cur";
247 __get_template_names $mode "$cur")
248 compopt -o filenames
d611dadc 249
843cfcb1
ZJS
250 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
251 comps=$( __get_startable_units $mode "$cur" )
252 compopt -o filenames
d611dadc 253
843cfcb1
ZJS
254 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
255 comps=$( __get_restartable_units $mode "$cur" )
256 compopt -o filenames
d611dadc 257
843cfcb1
ZJS
258 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
259 comps=$( __get_stoppable_units $mode "$cur" )
260 compopt -o filenames
d611dadc 261
843cfcb1
ZJS
262 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
263 comps=$( __get_reloadable_units $mode "$cur" )
264 compopt -o filenames
d611dadc 265
843cfcb1
ZJS
266 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
267 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
268 $( __get_all_units $mode "$cur" ) )
269 compopt -o filenames
270
271 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
272 comps=$( __get_failed_units $mode "$cur" )
273 compopt -o filenames
d611dadc 274
843cfcb1
ZJS
275 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
276 comps=$( __get_masked_units $mode "$cur" )
277 compopt -o filenames
278
279 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
280 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
8fc5cd71 281 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
843cfcb1
ZJS
282 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
283 | { while read -r a b; do echo " $a"; done; } )
284 else
285 comps=$( __get_all_unit_files $mode "$cur" )
286 fi
287 compopt -o filenames
8fc5cd71 288
843cfcb1
ZJS
289 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
290 comps=''
d611dadc 291
843cfcb1
ZJS
292 elif __contains_word "$verb" ${VERBS[JOBS]}; then
293 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc 294
843cfcb1
ZJS
295 elif __contains_word "$verb" ${VERBS[ENVS]}; then
296 comps=$( __systemctl $mode show-environment \
297 | while read -r line; do echo " ${line%%=*}="; done )
298 compopt -o nospace
d611dadc 299
843cfcb1
ZJS
300 elif __contains_word "$verb" ${VERBS[FILE]}; then
301 comps=$( compgen -A file -- "$cur" )
302 compopt -o filenames
035dd8c0 303
843cfcb1
ZJS
304 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
305 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
306 | { while read -r a b; do echo " $a"; done; } )
307 fi
d611dadc 308
843cfcb1
ZJS
309 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
310 return 0
d611dadc
MB
311}
312
313complete -F _systemctl systemctl