From: Simon Ruderich Date: Tue, 17 Jul 2018 12:30:19 +0000 (+0200) Subject: zsh: _systemctl: sync with latest version from Debian's zsh X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=7e25020b0fcf8389178c24babd6e8256901cafd4;p=config%2Fdotfiles.git zsh: _systemctl: sync with latest version from Debian's zsh --- diff --git a/shell/zsh/functions/_systemctl b/shell/zsh/functions/_systemctl index c440dd7..8f5b5d1 100644 --- a/shell/zsh/functions/_systemctl +++ b/shell/zsh/functions/_systemctl @@ -1,4 +1,5 @@ #compdef systemctl +# SPDX-License-Identifier: LGPL-2.1+ (( $+functions[_systemctl_command] )) || _systemctl_command() { @@ -17,7 +18,8 @@ "force-reload:Reload one or more units if possible, otherwise restart if active" "hibernate:Hibernate the system" "hybrid-sleep:Hibernate and suspend the system" - "reload-or-try-restart:Reload one or more units if possible, otherwise restart if active" + "suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it" + "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active" "isolate:Start one unit and stop all others" "kill:Send signal to processes of a unit" "is-active:Check whether units are active" @@ -29,6 +31,8 @@ "list-unit-files:List installed unit files" "enable:Enable one or more unit files" "disable:Disable one or more unit files" + "add-wants:Add Wants= dependencies to a unit" + "add-requires:Add Requires= dependencies to a unit" "reenable:Reenable one or more unit files" "preset:Enable/disable one or more unit files based on preset configuration" "set-default:Set the default target" @@ -43,8 +47,6 @@ "is-enabled:Check whether unit files are enabled" "list-jobs:List jobs" "cancel:Cancel all, one, or more jobs" - "snapshot:Create a snapshot" - "delete:Remove one or more snapshots" "show-environment:Dump environment" "set-environment:Set one or more environment variables" "unset-environment:Unset one or more environment variables" @@ -60,6 +62,7 @@ "kexec:Shut down and reboot the system with kexec" "exit:Ask for user instance termination" "switch-root:Change root directory" + "revert:Revert unit files to their vendor versions" ) if (( CURRENT == 1 )); then @@ -71,7 +74,7 @@ # Deal with any aliases case $cmd in condrestart) cmd="try-restart";; - force-reload) cmd="reload-or-try-restart";; + force-reload) cmd="try-reload-or-restart";; esac # CHANGED: my aliases @@ -108,18 +111,18 @@ __systemctl() { - systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" + systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null } # Fills the unit list _systemctl_all_units() { - if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) && - ! _retrieve_cache SYS_ALL_UNITS; + if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) || + ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr; then - _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} ) - _store_cache SYS_ALL_UNITS _sys_all_units + _sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} ) + _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units fi } @@ -128,57 +131,57 @@ _systemctl_really_all_units() { local -a all_unit_files; local -a really_all_units; - if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) && - ! _retrieve_cache SYS_REALLY_ALL_UNITS; + if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) || + ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr; then - all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} ) + all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} ) _systemctl_all_units really_all_units=($_sys_all_units $all_unit_files) _sys_really_all_units=(${(u)really_all_units}) - _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units + _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units fi } _filter_units_by_property() { - local property=$1 value=$2 ; shift ; shift - local -a units ; units=($*) - local props - for props in ${(ps:\n\n:)"$(_call_program units "$service show --no-pager --property="Id,$property" -- ${units} 2>/dev/null")"}; do - props=(${(f)props}) - if [[ "${props[2]}" = "$property=$value" ]]; then - echo -E - " ${props[1]#Id=}" - fi - done + local property=$1 value=$2; shift 2 + local -a units; units=("${(q-)@}") + local -A props + props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"}) + echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}" } -_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ } +_systemctl_get_non_template_names() { echo -E - ${^${(R)${(f)"$( + __systemctl $mode list-unit-files "$PREFIX*" + __systemctl $mode list-units --all "$PREFIX*" + )"}:#*@.*}%%[[:space:]]*} } + +_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}@ } -_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )} +_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )} _systemctl_startable_units(){ - _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $( - _filter_units_by_property CanStart yes $( - __systemctl $mode list-unit-files --state enabled,disabled,static | \ - { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } - __systemctl $mode list-units --state inactive,failed | \ - { while read -r a b; do echo -E - " $a"; done; } )) ) ) + _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $( + _filter_units_by_property CanStart yes ${${${(f)"$( + __systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*" + __systemctl $mode list-units --state inactive,failed "$PREFIX*" + )"}:#*@.*}%%[[:space:]]*} + )) ) } _systemctl_restartable_units(){ - _sys_restartable_units=( $(_filter_units_by_property CanStart yes $( - __systemctl $mode list-unit-files --state enabled,disabled,static | \ - { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } - __systemctl $mode list-units | \ - { while read -r a b; do echo -E - " $a"; done; } )) ) + _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$( + __systemctl $mode list-unit-files --state enabled,disabled,static "$PREFIX*" + __systemctl $mode list-units "$PREFIX*" + )"}:#*@.*}%%[[:space:]]*} ) ) } -_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --failed)"}%% *} ) } -_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) } +_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) } +_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files "$PREFIX*" ) ) } local fun # Completion functions for ALL_UNITS -for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit ; do +for fun in cat mask ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { _systemctl_really_all_units @@ -187,6 +190,15 @@ for fun in is-active is-failed is-enabled status show cat mask preset help list- } done +# Completion functions for NONTEMPLATE_UNITS +for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires ; do + (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() + { + _wanted systemd-units expl unit \ + compadd "$@" - $(_systemctl_get_non_template_names) + } +done + # Completion functions for ENABLED_UNITS (( $+functions[_systemctl_disable] )) || _systemctl_disable() { @@ -223,7 +235,7 @@ done { local _sys_startable_units; _systemctl_startable_units _wanted systemd-units expl 'startable unit' \ - compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names) + compadd "$@" - ${_sys_startable_units[*]} } # Completion functions for STOPPABLE_UNITS @@ -247,7 +259,7 @@ done } # Completion functions for RELOADABLE_UNITS -for fun in reload reload-or-try-restart force-reload ; do +for fun in reload try-reload-or-restart force-reload ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { local _sys_active_units; _systemctl_active_units @@ -263,7 +275,7 @@ for fun in restart reload-or-restart ; do { local _sys_restartable_units; _systemctl_restartable_units _wanted systemd-units expl 'restartable unit' \ - compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names) + compadd "$@" - ${_sys_restartable_units[*]} } done @@ -283,19 +295,11 @@ done _message "no jobs found" } -# Completion functions for SNAPSHOTS -(( $+functions[_systemctl_delete] )) || _systemctl_delete() -{ - _wanted systemd-snapshots expl snapshot \ - compadd "$@" - ${${(f)"$(__systemctl list-units --type snapshot --all)"}%% *} || - _message "no snapshots found" -} - # Completion functions for TARGETS (( $+functions[_systemctl_set-default] )) || _systemctl_set-default() { _wanted systemd-targets expl target \ - compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} || + compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} || _message "no targets found" } @@ -325,7 +329,6 @@ done # [STANDALONE]='daemon-reexec daemon-reload default # emergency exit halt kexec list-jobs list-units # list-unit-files poweroff reboot rescue show-environment' -# [NAME]='snapshot' _systemctl_caching_policy() { @@ -349,24 +352,22 @@ _systemctl_caching_policy() _unit_states() { local -a _states - _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked) + _states=("${(fo)$(__systemctl --state=help)}") _values -s , "${_states[@]}" } _unit_types() { local -a _types - _types=(automount busname device mount path service snapshot socket swap target timer) + _types=("${(fo)$(__systemctl -t help)}") _values -s , "${_types[@]}" } _unit_properties() { - if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) && - ! _retrieve_cache SYS_ALL_PROPERTIES; + if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) || + ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr; then - _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all; - /lib/systemd/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*} - ) - _store_cache SYS_ALL_PROPRTIES _sys_all_properties + _sys_all_properties=( ${${(M)${(f)"$(/lib/systemd/systemd --dump-bus-properties)"}}} ) + _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties fi _values -s , "${_sys_all_properties[@]}" } @@ -377,8 +378,10 @@ _job_modes() { _values -s , "${_modes[@]}" } +# Build arguments for "systemctl" to be used in completion. local -a _modes; _modes=("--user" "--system") -local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]:---system} +# Use the last mode (they are exclusive and the last one is used). +local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]} _arguments -s \ {-h,--help}'[Show help]' \ '--version[Show package version]' \ @@ -390,7 +393,6 @@ _arguments -s \ '--reverse[Show reverse dependencies]' \ '--after[Show units ordered after]' \ '--before[Show units ordered before]' \ - '--failed[Show only failed units]' \ {-l,--full}"[Don't ellipsize unit names on output]" \ '--show-types[When showing sockets, show socket type]' \ {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \ @@ -401,18 +403,19 @@ _arguments -s \ '--system[Connect to system manager]' \ '--user[Connect to user service manager]' \ "--no-wall[Don't send wall message before halt/power-off/reboot]" \ - '--global[Enable/disable unit files globally]' \ + '--global[Enable/disable/mask unit files globally]' \ "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \ '--no-ask-password[Do not ask for system passwords]' \ '--kill-who=[Who to send signal to]:killwho:(main control all)' \ {-s+,--signal=}'[Which signal to send]:signal:_signals' \ {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \ - '--root=[Enable unit files in the specified root directory]:directory:_directories' \ - '--runtime[Enable unit files only temporarily until next reboot]' \ + '--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \ + '--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \ {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \ {-P,--privileged}'[Acquire privileges before execution]' \ {-n+,--lines=}'[Journal entries to show]:number of entries' \ {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \ '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \ '--plain[When used with list-dependencies, print output as a list]' \ + '--failed[Show failed units]' \ '*::systemctl command:_systemctl_command'