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