From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16980 invoked by alias); 21 Nov 2011 11:10:47 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 29911 Received: (qmail 23628 invoked from network); 21 Nov 2011 11:10:35 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, T_TO_NO_BRKTS_FREEMAIL autolearn=ham version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at m.gmane.org designates 80.91.229.12 as permitted sender) X-Injected-Via-Gmane: http://gmane.org/ To: zsh-workers@zsh.org From: foudfou Subject: [PATCH] various fixes to _systemctl Date: Mon, 21 Nov 2011 11:58:20 +0100 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: foudil.insa-lyon.fr User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111105 Thunderbird/8.0 * add --no-legend support (v37+) * multiple bug fixes (aliases, array range) * workaround compadd bug (compadd handles its own options) --- Completion/Unix/Command/_systemctl | 76 +++++++++++++++++++++--------------- 1 files changed, 45 insertions(+), 31 deletions(-) diff --git a/Completion/Unix/Command/_systemctl b/Completion/Unix/Command/_systemctl index 801a8e0..69adcf7 100644 --- a/Completion/Unix/Command/_systemctl +++ b/Completion/Unix/Command/_systemctl @@ -33,6 +33,7 @@ _systemctl() '--no-block[Do not wait until operation finished]' \ "--no-wall[Don't send wall message before halt/power-off/reboot]" \ "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \ + '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \ '--no-pager[Do not pipe output into a pager]' \ '--no-ask-password[Do not ask for system passwords]' \ '--order[When generating graph for dot, show only order]' \ @@ -61,8 +62,10 @@ _hosts_or_user_at_host() "stop:Stop (deactivate) one or more units" "reload:Reload one or more units" "restart:Start or restart one or more units" + "condrestart:Restart one or more units if active" "try-restart:Restart one or more units if active" "reload-or-restart:Reload one or more units is possible, otherwise start or restart" + "force-reload:Reload one or more units is possible, otherwise restart if active" "reload-or-try-restart:Reload one or more units is possible, otherwise restart if active" "isolate:Start one unit and stop all others" "kill:Send signal to processes of a unit" @@ -125,34 +128,42 @@ _hosts_or_user_at_host() fi } +__check_option_nolegend() +{ + systemctl --no-legend --version 2>&1 | grep -q 'unrecognized option' + print -Pn '%(?..--no-legend)' +} +nolegend=$(__check_option_nolegend) + + # Fills the unit lists _systemctl_all_units() { if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) && ! _retrieve_cache SYS_ALL_UNITS; then - _sys_all_units=( $(systemctl list-units --full --all | cut -d' ' -f1 \ - 2>/dev/null) ) + _sys_all_units=( $(systemctl ${nolegend} list-units --full --all \ + | cut -d' ' -f1 2>/dev/null) ) _store_cache SYS_ALL_UNITS _sys_all_units fi } _systemctl_inactive_units() { - _sys_inactive_units=( $(systemctl list-units --full --all \ - | awk '$3 != "active" {print $1}' 2>/dev/null) ) + _sys_inactive_units=( $(systemctl ${nolegend} list-units --full --all \ + | awk '$3 != "active" {print $1}' 2>/dev/null) ) } _systemctl_active_units() { - _sys_active_units=( $(systemctl list-units --full | cut -d' ' -f1 \ - 2>/dev/null) ) + _sys_active_units=( $(systemctl ${nolegend} list-units --full \ + | cut -d' ' -f1 2>/dev/null) ) } _systemctl_failed_units() { - _sys_failed_units=( $(systemctl list-units --full --failed | cut -d' ' -f1 - 2>/dev/null) ) + _sys_failed_units=( $(systemctl ${nolegend} list-units --full --failed \ + | cut -d' ' -f1 2>/dev/null) ) } _filter_units_by_property () { @@ -160,7 +171,7 @@ _filter_units_by_property () { local -a units ; units=($*) local -a props ; props=( $(systemctl show --property "$property" -- \ ${units[*]} | grep -v '^$') ) - for ((i=0; $i < ${#units[*]}; i++)); do + for ((i=1; $i <= ${#units[*]}; i++)); do if [[ "${props[i]}" = "$property=$value" ]]; then echo "${units[i]}" fi @@ -172,7 +183,7 @@ for fun in enable disable is-active is-enabled status show ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { _systemctl_all_units - compadd "$@" -a -- _sys_all_units + compadd "$@" -a - _sys_all_units } done @@ -180,7 +191,7 @@ done (( $+functions[_systemctl_start] )) || _systemctl_start() { _systemctl_inactive_units - compadd "$@" -- $( _filter_units_by_property CanStart yes \ + compadd "$@" - $( _filter_units_by_property CanStart yes \ ${_sys_inactive_units[*]} | grep -Ev '\.(device|snapshot)$' ) } @@ -189,7 +200,7 @@ for fun in restart reload-or-restart ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { _systemctl_all_units - compadd "$@" -- $( _filter_units_by_property CanStart yes \ + compadd "$@" - $( _filter_units_by_property CanStart yes \ ${_sys_all_units[*]} | grep -Ev '\.(device|snapshot|socket|timer)$' ) } done @@ -199,7 +210,7 @@ for fun in stop kill try-restart condrestart ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { _systemctl_active_units - compadd "$@" -- $( _filter_units_by_property CanStop yes \ + compadd "$@" - $( _filter_units_by_property CanStop yes \ ${_sys_active_units[*]} ) } done @@ -209,7 +220,7 @@ for fun in reload reload-or-try-restart force-reload ; do (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() { _systemctl_active_units - compadd "$@" -- $( _filter_units_by_property CanReload yes \ + compadd "$@" - $( _filter_units_by_property CanReload yes \ ${_sys_active_units[*]} ) } done @@ -218,7 +229,7 @@ done (( $+functions[_systemctl_isolate] )) || _systemctl_isolate() { _systemctl_all_units - compadd "$@" -- $( _filter_units_by_property AllowIsolate yes \ + compadd "$@" - $( _filter_units_by_property AllowIsolate yes \ ${_sys_all_units[*]} ) } @@ -226,34 +237,37 @@ done (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed() { _systemctl_failed_units - compadd "$@" -a -- _sys_failed_units || _message "no failed-unit found" + compadd "$@" -a - _sys_failed_units || _message "no failed-unit found" } # Completion functions for JOBS (( $+functions[_systemctl_cancel] )) || _systemctl_cancel() { - compadd "$@" -- $( systemctl list-jobs | cut -d' ' -f1 2>/dev/null ) || \ - _message "no job found" + compadd "$@" - $(systemctl ${nolegend} list-jobs \ + | cut -d' ' -f1 2>/dev/null ) || _message "no job found" } # Completion functions for SNAPSHOTS (( $+functions[_systemctl_delete] )) || _systemctl_delete() { - compadd "$@" -- $( systemctl list-units --type snapshot --full --all \ - | cut -d' ' -f1 2>/dev/null ) || _message "no snampshot found" + compadd "$@" - $(systemctl ${nolegend} list-units --type snapshot --full \ + --all | cut -d' ' -f1 2>/dev/null ) || _message "no snampshot found" } # Completion functions for ENVS -(( $+functions[_systemctl_set-environment] )) || _systemctl_set-environment() -{ - compadd "$@" -S '' -- $( systemctl show-environment \ - | sed 's_\([^=]\+=\).*_\1_' ) -} -(( $+functions[_systemctl_unset-environment] )) || _systemctl_unset-environment() -{ - compadd "$@" -S '' -- $( systemctl show-environment \ - | sed 's_\([^=]\+\)=.*_\1_' ) -} +for fun in set-environment unset-environment ; do + (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() + { + local fun=$0 ; fun=${fun##_systemctl_} + local suf + if [[ "${fun}" = "set-environment" ]]; then + suf='-S=' + fi + + compadd "$@" ${suf} - $(systemctl show-environment \ + | sed 's_\([^=]\+\)=.*_\1_' ) + } +done # no completion for: # [STANDALONE]='daemon-reexec daemon-reload default dot dump emergency exit @@ -270,7 +284,7 @@ _systemctl_caching_policy() oldcache=( "$1"(mh+1) ) (( $#oldcache )) && return 0 - _sysunits=($(systemctl --full --all | cut -d' ' -f1)) + _sysunits=($(systemctl ${nolegend} --full --all | cut -d' ' -f1)) if (( $#_sysunits )); then for unit in $_sysunits; do