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