3 # Copyright (c) 2011 Foudil Brétel <foudil.newbie+zshsystemctl@gmail.com>
5 # This file is released under the GPLv3.
7 # inspired from _yum and systemctl-bash-completion.sh (shipped with systemctl)
9 # TODO: enable options after commands. Ex: systemctl list-units --all --full
14 local curcontext="$curcontext" state lstate line
16 # -s for aggregated options like -aP
18 {-h,--help}'[Show help]' \
19 '--version[Show package version]' \
20 {-t,--type=}'[List only units of a particular type]:unit type:(automount device mount path service snapshot socket swap target timer)' \
21 \*{-p,--property=}'[Show only properties by specific name]:unit property:' \
22 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
23 '--failed[Show only failed units]' \
24 "--full[Don't ellipsize unit names on output]" \
25 '--fail[When queueing a new job, fail if conflicting jobs are pending]' \
26 '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \
27 '--kill-mode=[How to send signal]:killmode:(control-group process)' \
28 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
29 {-s,--signal=}'[Which signal to send]:signal:_signals' \
30 {-H,--host=}'[Show information for remote host]:userathost:_hosts_or_user_at_host' \
31 {-P,--privileged}'[Acquire privileges before execution]' \
32 {-q,--quiet}'[Suppress output]' \
33 '--no-block[Do not wait until operation finished]' \
34 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
35 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
36 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
37 '--no-pager[Do not pipe output into a pager]' \
38 '--no-ask-password[Do not ask for system passwords]' \
39 '--order[When generating graph for dot, show only order]' \
40 '--require[When generating graph for dot, show only requirement]' \
41 '--system[Connect to system manager]' \
42 '--user[Connect to user service manager]' \
43 '--global[Enable/disable unit files globally]' \
44 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
45 '--defaults[When disabling unit files, remove default symlinks only]' \
46 '*::systemctl command:_systemctl_command'
49 _hosts_or_user_at_host()
52 'users-hosts:: _user_at_host' \
56 (( $+functions[_systemctl_command] )) || _systemctl_command()
58 local -a _systemctl_cmds
60 "list-units:List units"
61 "start:Start (activate) one or more units"
62 "stop:Stop (deactivate) one or more units"
63 "reload:Reload one or more units"
64 "restart:Start or restart one or more units"
65 "condrestart:Restart one or more units if active"
66 "try-restart:Restart one or more units if active"
67 "reload-or-restart:Reload one or more units is possible, otherwise start or restart"
68 "force-reload:Reload one or more units is possible, otherwise restart if active"
69 "reload-or-try-restart:Reload one or more units is possible, otherwise restart if active"
70 "isolate:Start one unit and stop all others"
71 "kill:Send signal to processes of a unit"
72 "is-active:Check whether units are active"
73 "status:Show runtime status of one or more units"
74 "show:Show properties of one or more units/jobs or the manager"
75 "reset-failed:Reset failed state for all, one, or more units"
76 "enable:Enable one or more unit files"
77 "disable:Disable one or more unit files"
78 "is-enabled:Check whether unit files are enabled"
79 "load:Load one or more units"
81 "cancel:Cancel all, one, or more jobs"
82 "monitor:Monitor unit/job changes"
83 "dump:Dump server status"
84 "dot:Dump dependency graph for dot(1)"
85 "snapshot:Create a snapshot"
86 "delete:Remove one or more snapshots"
87 "daemon-reload:Reload systemd manager configuration"
88 "daemon-reexec:Reexecute systemd manager"
89 "show-environment:Dump environment"
90 "set-environment:Set one or more environment variables"
91 "unset-environment:Unset one or more environment variables"
92 "default:Enter system default mode"
93 "rescue:Enter system rescue mode"
94 "emergency:Enter system emergency mode"
95 "halt:Shut down and halt the system"
96 "poweroff:Shut down and power-off the system"
97 "reboot:Shut down and reboot the system"
98 "kexec:Shut down and reboot the system with kexec"
99 "exit:Ask for user instance termination"
102 if (( CURRENT == 1 )); then
103 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
105 local curcontext="$curcontext"
107 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
108 # Deal with any aliases
110 condrestart) cmd="try-restart";;
111 force-reload) cmd="reload-or-try-restart";;
115 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
118 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
119 if [[ -z "$update_policy" ]]; then
120 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
123 _call_function ret _systemctl_$cmd || _message 'no more arguments'
125 _message "unknown systemctl command: $words[1]"
131 __check_option_nolegend()
133 systemctl --no-legend --version 2>&1 | grep -q 'unrecognized option'
134 print -Pn '%(?..--no-legend)'
136 nolegend=$(__check_option_nolegend)
139 # Fills the unit lists
140 _systemctl_all_units()
142 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
143 ! _retrieve_cache SYS_ALL_UNITS;
145 _sys_all_units=( $(systemctl ${nolegend} list-units --full --all \
146 | cut -d' ' -f1 2>/dev/null) )
147 _store_cache SYS_ALL_UNITS _sys_all_units
151 _systemctl_inactive_units()
153 _sys_inactive_units=( $(systemctl ${nolegend} list-units --full --all \
154 | awk '$3 != "active" {print $1}' 2>/dev/null) )
157 _systemctl_active_units()
159 _sys_active_units=( $(systemctl ${nolegend} list-units --full \
160 | cut -d' ' -f1 2>/dev/null) )
163 _systemctl_failed_units()
165 _sys_failed_units=( $(systemctl ${nolegend} list-units --full --failed \
166 | cut -d' ' -f1 2>/dev/null) )
169 _filter_units_by_property () {
170 local property=$1 value=$2 ; shift ; shift
171 local -a units ; units=($*)
172 local -a props ; props=( $(systemctl show --property "$property" -- \
173 ${units[*]} | grep -v '^$') )
174 for ((i=1; $i <= ${#units[*]}; i++)); do
175 if [[ "${props[i]}" = "$property=$value" ]]; then
181 # Completion functions for ALL_UNITS
182 for fun in enable disable is-active is-enabled status show ; do
183 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
186 compadd "$@" -a - _sys_all_units
190 # Completion functions for STARTABLE_UNITS
191 (( $+functions[_systemctl_start] )) || _systemctl_start()
193 _systemctl_inactive_units
194 compadd "$@" - $( _filter_units_by_property CanStart yes \
195 ${_sys_inactive_units[*]} | grep -Ev '\.(device|snapshot)$' )
198 # Completion functions for RESTARTABLE_UNITS
199 for fun in restart reload-or-restart ; do
200 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
203 compadd "$@" - $( _filter_units_by_property CanStart yes \
204 ${_sys_all_units[*]} | grep -Ev '\.(device|snapshot|socket|timer)$' )
208 # Completion functions for STOPPABLE_UNITS
209 for fun in stop kill try-restart condrestart ; do
210 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
212 _systemctl_active_units
213 compadd "$@" - $( _filter_units_by_property CanStop yes \
214 ${_sys_active_units[*]} )
218 # Completion functions for RELOADABLE_UNITS
219 for fun in reload reload-or-try-restart force-reload ; do
220 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
222 _systemctl_active_units
223 compadd "$@" - $( _filter_units_by_property CanReload yes \
224 ${_sys_active_units[*]} )
228 # Completion functions for ISOLATABLE_UNITS
229 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
232 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
233 ${_sys_all_units[*]} )
236 # Completion functions for FAILED_UNITS
237 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
239 _systemctl_failed_units
240 compadd "$@" -a - _sys_failed_units || _message "no failed-unit found"
243 # Completion functions for JOBS
244 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
246 compadd "$@" - $(systemctl ${nolegend} list-jobs \
247 | cut -d' ' -f1 2>/dev/null ) || _message "no job found"
250 # Completion functions for SNAPSHOTS
251 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
253 compadd "$@" - $(systemctl ${nolegend} list-units --type snapshot --full \
254 --all | cut -d' ' -f1 2>/dev/null ) || _message "no snampshot found"
257 # Completion functions for ENVS
258 for fun in set-environment unset-environment ; do
259 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
261 local fun=$0 ; fun=${fun##_systemctl_}
263 if [[ "${fun}" = "set-environment" ]]; then
267 compadd "$@" ${suf} - $(systemctl show-environment \
268 | sed 's_\([^=]\+\)=.*_\1_' )
273 # [STANDALONE]='daemon-reexec daemon-reload default dot dump emergency exit
274 # halt kexec list-jobs list-units monitor poweroff reboot
275 # rescue show-environment'
276 # [NAME]='snapshot load'
278 _systemctl_caching_policy()
283 # rebuild if cache is more than a day old
284 oldcache=( "$1"(mh+1) )
285 (( $#oldcache )) && return 0
287 _sysunits=($(systemctl ${nolegend} --full --all | cut -d' ' -f1))
289 if (( $#_sysunits )); then
290 for unit in $_sysunits; do
291 [[ "$unit" -nt "$1" ]] && return 0
303 # indent-tabs-mode: nil