chiark / gitweb /
zsh-completion: Move machine listing to autoload
[elogind.git] / shell-completion / zsh / _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-units:List units"
9     "start:Start (activate) one or more units"
10     "stop:Stop (deactivate) one or more units"
11     "reload:Reload one or more units"
12     "restart:Start or restart one or more units"
13     "condrestart:Restart one or more units if active"
14     "try-restart:Restart one or more units if active"
15     "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
16     "force-reload:Reload one or more units if possible, otherwise restart if active"
17     "hibernate:Hibernate the system"
18     "hybrid-sleep:Hibernate and suspend the system"
19     "reload-or-try-restart:Reload one or more units if possible, otherwise restart if active"
20     "isolate:Start one unit and stop all others"
21     "kill:Send signal to processes of a unit"
22     "is-active:Check whether units are active"
23     "is-failed:Check whether units are failed"
24     "status:Show runtime status of one or more units"
25     "show:Show properties of one or more units/jobs or the manager"
26     "reset-failed:Reset failed state for all, one, or more units"
27     "load:Load one or more units"
28     "list-unit-files:List installed unit files"
29     "enable:Enable one or more unit files"
30     "disable:Disable one or more unit files"
31     "reenable:Reenable one or more unit files"
32     "preset:Enable/disable one or more unit files based on preset configuration"
33     "help:Show documentation for specified units"
34     "list-dependencies:Show unit dependency tree"
35     "mask:Mask one or more units"
36     "unmask:Unmask one or more units"
37     "link:Link one or more units files into the search path"
38     "is-enabled:Check whether unit files are enabled"
39     "list-jobs:List jobs"
40     "cancel:Cancel all, one, or more jobs"
41     "snapshot:Create a snapshot"
42     "delete:Remove one or more snapshots"
43     "show-environment:Dump environment"
44     "set-environment:Set one or more environment variables"
45     "unset-environment:Unset one or more environment variables"
46     "daemon-reload:Reload systemd manager configuration"
47     "daemon-reexec:Reexecute systemd manager"
48     "default:Enter system default mode"
49     "rescue:Enter system rescue mode"
50     "emergency:Enter system emergency mode"
51     "halt:Shut down and halt the system"
52     "suspend:Suspend the system"
53     "poweroff:Shut down and power-off the system"
54     "reboot:Shut down and reboot the system"
55     "kexec:Shut down and reboot the system with kexec"
56     "exit:Ask for user instance termination"
57   )
58
59   if (( CURRENT == 1 )); then
60     _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
61   else
62     local curcontext="$curcontext"
63
64     cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
65     # Deal with any aliases
66     case $cmd in
67       condrestart) cmd="try-restart";;
68       force-reload) cmd="reload-or-try-restart";;
69     esac
70
71     if (( $#cmd )); then
72       curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
73
74       local update_policy
75       zstyle -s ":completion:${curcontext}:" cache-policy update_policy
76       if [[ -z "$update_policy" ]]; then
77         zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
78       fi
79
80       _call_function ret _systemctl_$cmd || _message 'no more arguments'
81     else
82       _message "unknown systemctl command: $words[1]"
83     fi
84     return ret
85   fi
86 }
87
88 __systemctl()
89 {
90   local -a _modes
91   _modes=("--user" "--system")
92   systemctl ${words:*_modes} --full --no-legend --no-pager "$@"
93 }
94
95
96 # Fills the unit list
97 _systemctl_all_units()
98 {
99   if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
100     ! _retrieve_cache SYS_ALL_UNITS;
101   then
102     _sys_all_units=( $(__systemctl list-units --all | { while read a b; do echo " $a"; done; }) )
103     _store_cache SYS_ALL_UNITS _sys_all_units
104   fi
105 }
106
107 # Fills the unit list including all file units
108 _systemctl_really_all_units()
109 {
110   local -a all_unit_files;
111   local -a really_all_units;
112   if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) &&
113     ! _retrieve_cache SYS_REALLY_ALL_UNITS;
114   then
115     all_unit_files=( $(__systemctl list-unit-files | { while read a b; do echo " $a"; done; }) )
116     _systemctl_all_units
117     really_all_units=($_sys_all_units $all_unit_files)
118     _sys_really_all_units=(${(u)really_all_units})
119     _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units
120   fi
121 }
122
123 _filter_units_by_property() {
124   local property=$1 value=$2 ; shift ; shift
125   local -a units ; units=($*)
126   local prop unit
127   for ((i=1; $i <= ${#units[*]}; i++)); do
128     # FIXME: "Failed to issue method call: Unknown unit" errors are ignored for
129     # now (related to DBUS_ERROR_UNKNOWN_OBJECT). in the future, we need to
130     # revert to calling 'systemctl show' once for all units, which is way
131     # faster
132     unit=${units[i]}
133     prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"}
134     if [[ "${prop}" = "$property=$value" ]]; then
135       echo " ${unit}"
136     fi
137   done
138 }
139
140 _systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo " $a"; done; }) )}
141 _systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" || $c == "failed" ]] && echo " $a"; done; }) )}
142 _systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo " $a"; done; }) )}
143 _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo " $a"; done; }) )}
144 _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo " $a"; done; }) )}
145 _systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read a b; do [[ $b == "masked" ]] && echo " $a"; done; }) )}
146
147 # Completion functions for ALL_UNITS
148 for fun in is-active is-failed is-enabled status show mask preset help list-dependencies ; do
149   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
150   {
151     _systemctl_really_all_units
152     compadd "$@" -a - _sys_really_all_units
153   }
154 done
155
156 # Completion functions for ENABLED_UNITS
157 for fun in disable reenable ; do
158   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
159   {
160     _systemctl_enabled_units
161     _systemctl_disabled_units
162     compadd "$@" -a - _sys_enabled_units _sys_disabled_units
163   }
164 done
165
166 # Completion functions for DISABLED_UNITS
167 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
168 {
169   _systemctl_disabled_units
170   compadd "$@" -a - _sys_disabled_units
171 }
172
173 # Completion functions for FAILED_UNITS
174 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
175 {
176   _systemctl_failed_units
177   compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
178 }
179
180 # Completion functions for STARTABLE_UNITS
181 (( $+functions[_systemctl_start] )) || _systemctl_start()
182 {
183   _systemctl_inactive_units
184   compadd "$@" -a - _sys_inactive_units
185 }
186
187 # Completion functions for STOPPABLE_UNITS
188 for fun in stop kill try-restart condrestart ; do
189   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
190   {
191     _systemctl_active_units
192     compadd "$@" - $( _filter_units_by_property CanStop yes \
193       ${_sys_active_units[*]} )
194   }
195 done
196
197 # Completion functions for ISOLATABLE_UNITS
198 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
199 {
200   _systemctl_all_units
201   compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
202     ${_sys_all_units[*]} )
203 }
204
205 # Completion functions for RELOADABLE_UNITS
206 for fun in reload reload-or-try-restart force-reload ; do
207   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
208   {
209     _systemctl_active_units
210     compadd "$@" - $( _filter_units_by_property CanReload yes \
211       ${_sys_active_units[*]} )
212   }
213 done
214
215 # Completion functions for RESTARTABLE_UNITS
216 for fun in restart reload-or-restart ; do
217   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
218   {
219     _systemctl_all_units
220     compadd "$@" - $( _filter_units_by_property CanStart yes \
221       ${_sys_all_units[*]} | while read line; do \
222       [[ "$line" =~ \.device$ ]] || echo " $line"; \
223       done )
224   }
225 done
226
227 # Completion functions for MASKED_UNITS
228 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
229 {
230   _systemctl_masked_units
231   compadd "$@" -a - _sys_masked_units || _message "no masked unit found"
232 }
233
234 # Completion functions for JOBS
235 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
236 {
237   compadd "$@" - $(__systemctl list-jobs \
238     | cut -d' ' -f1  2>/dev/null ) || _message "no job found"
239 }
240
241 # Completion functions for SNAPSHOTS
242 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
243 {
244   compadd "$@" - $(__systemctl list-units --type snapshot --all \
245     | cut -d' ' -f1  2>/dev/null ) || _message "no snapshot found"
246 }
247
248 # Completion functions for ENVS
249 for fun in set-environment unset-environment ; do
250   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
251   {
252     local fun=$0 ; fun=${fun##_systemctl_}
253     local suf
254     if [[ "${fun}" = "set-environment" ]]; then
255       suf='-S='
256     fi
257
258     compadd "$@" ${suf} - $(systemctl show-environment \
259       | while read line; do echo " ${line%%\=}";done )
260   }
261 done
262
263 (( $+functions[_systemctl_link] )) || _systemctl_link() { _files }
264
265 # no systemctl completion for:
266 #    [STANDALONE]='daemon-reexec daemon-reload default
267 #                  emergency exit halt kexec list-jobs list-units
268 #                  list-unit-files poweroff reboot rescue show-environment'
269 #         [NAME]='snapshot load'
270
271 _systemctl_caching_policy()
272 {
273   local _sysunits
274   local -a oldcache
275
276   # rebuild if cache is more than a day old
277   oldcache=( "$1"(mh+1) )
278   (( $#oldcache )) && return 0
279
280   _sysunits=($(__systemctl --all | cut -d' ' -f1))
281
282   if (( $#_sysunits )); then
283     for unit in $_sysunits; do
284       [[ "$unit" -nt "$1" ]] && return 0
285     done
286   fi
287
288   return 1
289 }
290
291 _unit_states() {
292     local -a _states
293     _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked)
294     _values -s , "${_states[@]}"
295 }
296
297 _unit_types() {
298     local -a _types
299     _types=(automount device mount path service snapshot socket swap target timer)
300     _values -s , "${_types[@]}"
301 }
302
303 _arguments -s \
304     {-h,--help}'[Show help]' \
305     '--version[Show package version]' \
306     {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
307     '--state=[Display units in the specifyied state]:unit state:_unit_states' \
308     \*{-p+,--property=}'[Show only properties by specific name]:unit property' \
309     {-a,--all}'[Show all units/properties, including dead/empty ones]' \
310     '--reverse[Show reverse dependencies]' \
311     '--after[Show units ordered after]' \
312     '--before[Show units ordered before]' \
313     '--failed[Show only failed units]' \
314     {-l,--full}"[Don't ellipsize unit names on output]" \
315     '--fail[When queueing a new job, fail if conflicting jobs are pending]' \
316     '--show-types[When showing sockets, show socket type]' \
317     '--irreversible[Mark transactions as irreversible]' \
318     '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \
319     {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
320     {-q,--quiet}'[Suppress output]' \
321     '--no-block[Do not wait until operation finished]' \
322     '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
323     '--no-pager[Do not pipe output into a pager]' \
324     '--system[Connect to system manager]' \
325     '--user[Connect to user service manager]' \
326     "--no-wall[Don't send wall message before halt/power-off/reboot]" \
327     '--global[Enable/disable unit files globally]' \
328     "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
329     '--no-ask-password[Do not ask for system passwords]' \
330     '--kill-who=[Who to send signal to]:killwho:(main control all)' \
331     {-s+,--signal=}'[Which signal to send]:signal:_signals' \
332     {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
333     '--root=[Enable unit files in the specified root directory]:directory:_directories' \
334     '--runtime[Enable unit files only temporarily until next reboot]' \
335     {-H+,--host=}'[Show information for remote host]:userathost:_sd_hosts_or_user_at_host' \
336     {-P,--privileged}'[Acquire privileges before execution]' \
337     {-n+,--lines=}'[Journal entries to show]:number of entries' \
338     {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
339     '--plain[When used with list-dependencies, print output as a list]' \
340     '*::systemctl command:_systemctl_command'