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