]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/zsh/_systemctl.in
Merge pull request #5926 from fsateler/condition-uid
[thirdparty/systemd.git] / shell-completion / zsh / _systemctl.in
1 #compdef systemctl
2
3 (( $+functions[_systemctl_command] )) || _systemctl_command()
4 {
5 local -a _systemctl_cmds
6 _systemctl_cmds=(
7 "list-sockets:List sockets"
8 "list-timers:List timers"
9 "list-units:List units"
10 "start:Start (activate) one or more units"
11 "stop:Stop (deactivate) one or more units"
12 "reload:Reload one or more units"
13 "restart:Start or restart one or more units"
14 "condrestart:Restart one or more units if active"
15 "try-restart:Restart one or more units if active"
16 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
17 "force-reload:Reload one or more units if possible, otherwise restart if active"
18 "hibernate:Hibernate the system"
19 "hybrid-sleep:Hibernate and suspend the system"
20 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
21 "isolate:Start one unit and stop all others"
22 "kill:Send signal to processes of a unit"
23 "is-active:Check whether units are active"
24 "is-failed:Check whether units are failed"
25 "status:Show runtime status of one or more units"
26 "show:Show properties of one or more units/jobs or the manager"
27 "cat:Show the source unit files and drop-ins"
28 "reset-failed:Reset failed state for all, one, or more units"
29 "list-unit-files:List installed unit files"
30 "enable:Enable one or more unit files"
31 "disable:Disable one or more unit files"
32 "reenable:Reenable one or more unit files"
33 "preset:Enable/disable one or more unit files based on preset configuration"
34 "set-default:Set the default target"
35 "get-default:Query the default target"
36 "edit:Edit one or more unit files"
37 "is-system-running:Query overall status of the system"
38 "help:Show documentation for specified units"
39 "list-dependencies:Show unit dependency tree"
40 "mask:Mask one or more units"
41 "unmask:Unmask one or more units"
42 "link:Link one or more units files into the search path"
43 "is-enabled:Check whether unit files are enabled"
44 "list-jobs:List jobs"
45 "cancel:Cancel all, one, or more jobs"
46 "show-environment:Dump environment"
47 "set-environment:Set one or more environment variables"
48 "unset-environment:Unset one or more environment variables"
49 "daemon-reload:Reload systemd manager configuration"
50 "daemon-reexec:Reexecute systemd manager"
51 "default:Enter system default mode"
52 "rescue:Enter system rescue mode"
53 "emergency:Enter system emergency mode"
54 "halt:Shut down and halt the system"
55 "suspend:Suspend the system"
56 "poweroff:Shut down and power-off the system"
57 "reboot:Shut down and reboot the system"
58 "kexec:Shut down and reboot the system with kexec"
59 "exit:Ask for user instance termination"
60 "switch-root:Change root directory"
61 "revert:Revert unit files to their vendor versions"
62 )
63
64 if (( CURRENT == 1 )); then
65 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
66 else
67 local curcontext="$curcontext" expl
68
69 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
70 # Deal with any aliases
71 case $cmd in
72 condrestart) cmd="try-restart";;
73 force-reload) cmd="try-reload-or-restart";;
74 esac
75
76 if (( $#cmd )); then
77 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
78
79 local update_policy
80 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
81 if [[ -z "$update_policy" ]]; then
82 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
83 fi
84
85 _call_function ret _systemctl_$cmd || _message 'no more arguments'
86 else
87 _message "unknown systemctl command: $words[1]"
88 fi
89 return ret
90 fi
91 }
92
93 __systemctl()
94 {
95 systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null
96 }
97
98
99 # Fills the unit list
100 _systemctl_all_units()
101 {
102 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
103 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
104 then
105 _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
106 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
107 fi
108 }
109
110 # Fills the unit list including all file units
111 _systemctl_really_all_units()
112 {
113 local -a all_unit_files;
114 local -a really_all_units;
115 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
116 ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
117 then
118 all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
119 _systemctl_all_units
120 really_all_units=($_sys_all_units $all_unit_files)
121 _sys_really_all_units=(${(u)really_all_units})
122 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
123 fi
124 }
125
126 _filter_units_by_property() {
127 local property=$1 value=$2; shift 2
128 local -a units; units=("${(q-)@}")
129 local -A props
130 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
131 echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}"
132 }
133
134 _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
135
136
137 _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
138
139 _systemctl_startable_units(){
140 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
141 _filter_units_by_property CanStart yes $(
142 __systemctl $mode list-unit-files --state enabled,disabled,static | \
143 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
144 __systemctl $mode list-units --state inactive,failed | \
145 { while read -r a b; do echo -E - " $a"; done; } )) ) )
146 }
147
148 _systemctl_restartable_units(){
149 _sys_restartable_units=( $(_filter_units_by_property CanStart yes $(
150 __systemctl $mode list-unit-files --state enabled,disabled,static | \
151 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
152 __systemctl $mode list-units | \
153 { while read -r a b; do echo -E - " $a"; done; } )) )
154 }
155
156 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) }
157 _systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) }
158
159 local fun
160 # Completion functions for ALL_UNITS
161 for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit revert ; do
162 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
163 {
164 _systemctl_really_all_units
165 _wanted systemd-units expl unit \
166 compadd "$@" -a - _sys_really_all_units
167 }
168 done
169
170 # Completion functions for ENABLED_UNITS
171 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
172 {
173 local _sys_unit_state; _systemctl_unit_state
174 _wanted systemd-units expl 'enabled unit' \
175 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
176 }
177
178 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
179 {
180 local _sys_unit_state; _systemctl_unit_state
181 _wanted systemd-units expl 'enabled/disabled unit' \
182 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
183 }
184
185 # Completion functions for DISABLED_UNITS
186 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
187 {
188 local _sys_unit_state; _systemctl_unit_state
189 _wanted systemd-units expl 'disabled unit' \
190 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
191 }
192
193 # Completion functions for FAILED_UNITS
194 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
195 {
196 local _sys_failed_units; _systemctl_failed_units
197 _wanted systemd-units expl 'failed unit' \
198 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
199 }
200
201 # Completion functions for STARTABLE_UNITS
202 (( $+functions[_systemctl_start] )) || _systemctl_start()
203 {
204 local _sys_startable_units; _systemctl_startable_units
205 _wanted systemd-units expl 'startable unit' \
206 compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
207 }
208
209 # Completion functions for STOPPABLE_UNITS
210 for fun in stop kill try-restart condrestart ; do
211 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
212 {
213 local _sys_active_units; _systemctl_active_units
214 _wanted systemd-units expl 'stoppable unit' \
215 compadd "$@" - $( _filter_units_by_property CanStop yes \
216 ${_sys_active_units[*]} )
217 }
218 done
219
220 # Completion functions for ISOLATABLE_UNITS
221 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
222 {
223 _systemctl_all_units
224 _wanted systemd-units expl 'isolatable unit' \
225 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
226 ${_sys_all_units[*]} )
227 }
228
229 # Completion functions for RELOADABLE_UNITS
230 for fun in reload try-reload-or-restart force-reload ; do
231 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
232 {
233 local _sys_active_units; _systemctl_active_units
234 _wanted systemd-units expl 'reloadable unit' \
235 compadd "$@" - $( _filter_units_by_property CanReload yes \
236 ${_sys_active_units[*]} )
237 }
238 done
239
240 # Completion functions for RESTARTABLE_UNITS
241 for fun in restart reload-or-restart ; do
242 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
243 {
244 local _sys_restartable_units; _systemctl_restartable_units
245 _wanted systemd-units expl 'restartable unit' \
246 compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
247 }
248 done
249
250 # Completion functions for MASKED_UNITS
251 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
252 {
253 local _sys_unit_state; _systemctl_unit_state
254 _wanted systemd-units expl 'masked unit' \
255 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
256 }
257
258 # Completion functions for JOBS
259 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
260 {
261 _wanted systemd-jobs expl job \
262 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
263 _message "no jobs found"
264 }
265
266 # Completion functions for TARGETS
267 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
268 {
269 _wanted systemd-targets expl target \
270 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
271 _message "no targets found"
272 }
273
274 # Completion functions for ENVS
275 for fun in set-environment unset-environment ; do
276 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
277 {
278 local fun=$0 ; fun=${fun##_systemctl_}
279 local suf
280 if [[ "${fun}" = "set-environment" ]]; then
281 suf='-S='
282 fi
283 _wanted systemd-environment expl 'environment variable' \
284 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
285 }
286 done
287
288 (( $+functions[_systemctl_link] )) || _systemctl_link() {
289 _sd_unit_files
290 }
291
292 (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
293 _files
294 }
295
296 # no systemctl completion for:
297 # [STANDALONE]='daemon-reexec daemon-reload default
298 # emergency exit halt kexec list-jobs list-units
299 # list-unit-files poweroff reboot rescue show-environment'
300
301 _systemctl_caching_policy()
302 {
303 local _sysunits
304 local -a oldcache
305
306 # rebuild if cache is more than a day old
307 oldcache=( "$1"(mh+1) )
308 (( $#oldcache )) && return 0
309
310 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
311
312 if (( $#_sysunits )); then
313 for unit in $_sysunits; do
314 [[ "$unit" -nt "$1" ]] && return 0
315 done
316 fi
317
318 return 1
319 }
320
321 _unit_states() {
322 local -a _states
323 _states=("${(fo)$(__systemctl --state=help)}")
324 _values -s , "${_states[@]}"
325 }
326
327 _unit_types() {
328 local -a _types
329 _types=("${(fo)$(__systemctl -t help)}")
330 _values -s , "${_types[@]}"
331 }
332
333 _unit_properties() {
334 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
335 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
336 then
337 _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all;
338 @rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*}
339 )
340 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
341 fi
342 _values -s , "${_sys_all_properties[@]}"
343 }
344
345 _job_modes() {
346 local -a _modes
347 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
348 _values -s , "${_modes[@]}"
349 }
350
351 # Build arguments for "systemctl" to be used in completion.
352 local -a _modes; _modes=("--user" "--system")
353 # Use the last mode (they are exclusive and the last one is used).
354 local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
355 _arguments -s \
356 {-h,--help}'[Show help]' \
357 '--version[Show package version]' \
358 {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
359 '--state=[Display units in the specified state]:unit state:_unit_states' \
360 '--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \
361 {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \
362 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
363 '--reverse[Show reverse dependencies]' \
364 '--after[Show units ordered after]' \
365 '--before[Show units ordered before]' \
366 {-l,--full}"[Don't ellipsize unit names on output]" \
367 '--show-types[When showing sockets, show socket type]' \
368 {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
369 {-q,--quiet}'[Suppress output]' \
370 '--no-block[Do not wait until operation finished]' \
371 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
372 '--no-pager[Do not pipe output into a pager]' \
373 '--system[Connect to system manager]' \
374 '--user[Connect to user service manager]' \
375 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
376 '--global[Enable/disable unit files globally]' \
377 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
378 '--no-ask-password[Do not ask for system passwords]' \
379 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
380 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
381 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
382 '--root=[Enable unit files in the specified root directory]:directory:_directories' \
383 '--runtime[Enable unit files only temporarily until next reboot]' \
384 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
385 {-P,--privileged}'[Acquire privileges before execution]' \
386 {-n+,--lines=}'[Journal entries to show]:number of entries' \
387 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
388 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
389 '--plain[When used with list-dependencies, print output as a list]' \
390 '--failed[Show failed units]' \
391 '*::systemctl command:_systemctl_command'