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