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