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