Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Qmail-Scanner-Diagnostics: from mail-wm0-f52.google.com by f.primenet.com.au (envelope-from <myllynen@redhat.com>, uid 7791) with qmail-scanner-2.11 
 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1.  
 Clear:RC:0(74.125.82.52):SA:0(-0.0/5.0):. 
 Processed in 0.350616 secs); 18 Jul 2016 12:14:26 -0000
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-0.0 required=5.0 tests=SPF_PASS
	autolearn=unavailable autolearn_force=no version=3.4.1
X-Envelope-From: myllynen@redhat.com
X-Qmail-Scanner-Mime-Attachments: |
X-Qmail-Scanner-Zip-Files: |
Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 74.125.82.52 as permitted sender)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:subject:reply-to:references:to:cc
         :organization:message-id:date:user-agent:mime-version:in-reply-to
         :content-transfer-encoding;
        bh=jsqqwAApil3v+BlSP4PrE9KDrN8HEnpClT2KuGIiHD8=;
        b=RH4wdkF6IZ9AyCjqSj9zI1YIRVBf9cQ69sJjQqH2UfiOyKnwFIJhblgyK8AaH4SOSu
         VjeeaIulhoH4I3mK9BCftZm8qYDiukfnFiqjgl40e9S1UTQMU7QuY2HIeM3Kgilw+qMa
         3vYfnsLx3kE1OcGnmFdF/domDbCJh0Crx3ZsBHIa1GrbT4AAZgm0uYvvnbZiU4GATkkg
         jHVyUFXs6Tb3Z/mUZR5JWg3bQmWD/uHOX4cMzcrlnKxn39JmTobH7mca2qqd3RH7zG7P
         6FpdptTVO+teaXiqjfdCD7nLoARH5d/Iy7J2pTazAxYe19rN2tOKE5jZyLHac8TrECkR
         ZnTA==
X-Gm-Message-State: ALyK8tKF69WQu2vrCEmRxABKLevb0GCbn4dZLh+PgIYBGeDyyE5YQmKZexnKPqRV+oxL6S0v
X-Received: by 10.28.185.202 with SMTP id j193mr56200263wmf.78.1468843616735;
        Mon, 18 Jul 2016 05:06:56 -0700 (PDT)
From: Marko Myllynen <myllynen@redhat.com>
Subject: Re: zsh virsh completion
Reply-To: Marko Myllynen <myllynen@redhat.com>
References: <8eb6dce0-50d7-5ab2-503a-194c1de2e45d@redhat.com>
 <20160713045957.GA3893@tarsus.local2>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh workers <zsh-workers@zsh.org>
Organization: Red Hat
Message-ID: <9968da53-c1fd-fa2a-f30c-c74f884d2478@redhat.com>
Date: Mon, 18 Jul 2016 15:06:52 +0300
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.2
MIME-Version: 1.0
In-Reply-To: <20160713045957.GA3893@tarsus.local2>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Seq: zsh-workers 38880

Hi,

On 2016-07-13 07:59, Daniel Shahaf wrote:
> Marko Myllynen wrote on Mon, Jul 11, 2016 at 14:52:02 +0300:
>> Below is a patch to implement basic completion for the virsh(1) [1]
>> command from libvirt [2], it completes all virsh commands and their
>> respective options.
> 
> Note there are two or three other _virsh's floating around:
> 
> https://github.com/jplitza/zsh-virsh-autocomplete/blob/master/_virsh
> https://github.com/zsh-users/zsh-completions/blob/master/src/_virsh
> https://github.com/Aso23/zsh_virsh_autocompletion/blob/master/_virsh

I wasn't aware of these. The last one on the list seems to be the first
incarnation, the middle one adds very little to it, the first link
provides the most recent version.

Those repos seem to have some other goodies as well, it's a pity that
they haven't been upstreamed. Many of us work with several systems and
downloading something for each is inconvenient, sometimes even
impossible, so if something is not in upstream and in distro packages,
it's almost like it didn't exist at all, unfortunately.

> Those seem to have features your patch lacks, e.g., completing domain
> (VM name) arguments.

Domain completion is certainly nice, however option completion seems to
be broken (try e.g. virsh perf <tab>) and all lack at least storage
pool and network completions.

Oliver cleaned up and committed my earlier patch, here's an update for
it, this patch implements completion for the rest of the commands that
are part of libvirt (as is with libvirt-2.0 RHEL 7 packages). Few of
them barely deserve completion but are there for completeness sake.
(No virt-admin command accepts filenames so those are not offered.)

---
 Completion/Unix/Command/_libvirt | 89 ++++++++++++++++++++++++++++++++++------
 1 file changed, 76 insertions(+), 13 deletions(-)

diff --git a/Completion/Unix/Command/_libvirt b/Completion/Unix/Command/_libvirt
index a9249b3..2053d15 100644
--- a/Completion/Unix/Command/_libvirt
+++ b/Completion/Unix/Command/_libvirt
@@ -1,34 +1,82 @@
-#compdef virsh
+#compdef virsh virt-admin virt-host-validate virt-pki-validate virt-xml-validate
 
 local curcontext="$curcontext" state line expl ret=1
 
+local exargs="-h --help -V -v --version=short --version=long"
+local -a common_opts interact_cmds
+common_opts=(
+  '(- *)'{-h,--help}'[print help information and exit]'
+  '(- *)'{-v,--version=short}'[print short version information and exit]'
+  '(- *)'{-V,--version=long}'[print long version information and exit]'
+  "(-c --connect $exargs)"{-c+,--connect}'[specify connection URI]:URI:_hosts'
+  "(-d --debug -q --quiet $exargs)"{-d+,--debug}'[set debug level]:level:(0 1 2 3 4)'
+  "(-l --log $exargs)"{-l+,--log}'[specify log file]:file:_files'
+  "(-q --quiet -d --debug $exargs)"{-q,--quiet}'[quiet mode]'
+)
+interact_cmds=(cd echo exit quit connect)
+
 case $service in
   virsh)
     if (( ! $+_cache_virsh_cmds )); then
       _cache_virsh_cmds=( ${${${${(f):-"$(_call_program options virsh help)"}:#*:}/# ##}/ *} )
+      for icmd in $interact_cmds; do
+        _cache_virsh_cmds[$_cache_virsh_cmds[(i)$icmd]]=()
+      done
     fi
     if (( ! $+_cache_virsh_cmdopts )); then
       typeset -gA _cache_virsh_cmdopts
     fi
     _arguments -A "-*" -C -S -s -w \
-      '(- *)'{-h,--help}'[print help information and exit]' \
-      '(- *)'{-v,--version=short}'[print short version information and exit]' \
-      '(- *)'{-V,--version=long}'[print long version information and exit]' \
-      '(-c --connect)'{-c+,--connect}'[specify connection URI]:URI:_hosts' \
-      '(-d --debug)'{-d+,--debug}'[set debug level]:level:(0 1 2 3 4)' \
-      '(-e --escape)'{-e+,--escape}'[set escape sequence for console]:sequence' \
-      '(-k --keepalive-interval)'{-k+,--keepalive-interval}'[set keepalive interval]:interval' \
-      '(-K --keepalive-count)'{-K+,--keepalive-count}'[set keepalive count]:count' \
-      '(-l --log)'{-l+,--log}'[specify log file]:file:_files' \
-      '(-q --quiet)'{-q,--quiet}'[quiet mode]' \
-      '(-r --readonly)'{-r,--readonly}'[connect readonly]' \
-      '(-t --timing)'{-t,--timing}'[print timing information]' \
+      "$common_opts[@]" \
+      "(-e --escape $exargs)"{-e+,--escape}'[set escape sequence for console]:sequence' \
+      "(-k --keepalive-interval $exargs)"{-k+,--keepalive-interval}'[set keepalive interval]:interval' \
+      "(-K --keepalive-count $exargs)"{-K+,--keepalive-count}'[set keepalive count]:count' \
+      "(-r --readonly $exargs)"{-r,--readonly}'[connect readonly]' \
+      "(-t --timing $exargs)"{-t,--timing}'[print timing information]' \
       '1:command:->virsh_cmds' \
       '*:cmdopt:->virsh_cmdopts' && return
       # We accept only virsh command options after the first non-option argument
       # (i.e., the virsh command itself), this makes it so with the -A "-*" above
       [[ -z $state ]] && state=virsh_cmdopts
   ;;
+  virt-admin)
+    if (( ! $+_cache_virt_admin_cmds )); then
+      _cache_virt_admin_cmds=( ${${${${(f):-"$(_call_program options virt-admin help)"}:#*:}/# ##}/ *} )
+      for icmd in $interact_cmds; do
+        _cache_virt_admin_cmds[$_cache_virt_admin_cmds[(i)$icmd]]=()
+      done
+    fi
+    if (( ! $+_cache_virt_admin_cmdopts )); then
+      typeset -gA _cache_virt_admin_cmdopts
+    fi
+    _arguments -A "-*" -C -S -s -w \
+      "$common_opts[@]" \
+      '1:command:->virt_admin_cmds' \
+      '*:cmdopt:->virt_admin_cmdopts' && return
+      # Same as with virsh above
+      [[ -z $state ]] && state=virt_admin_cmdopts
+  ;;
+  virt-host-validate)
+    _arguments -A "-*" -S \
+      '(- *)'{-h,--help}'[print help information and exit]' \
+      '(- *)'{-v,--version}'[print version information and exit]' \
+      '(- *)'{-q,--quiet}'[quiet mode]' \
+      '1:hv-type:(qemu lxc)' && return
+  ;;
+  virt-pki-validate)
+    _arguments -A "-*" -S \
+      '(- *)'{-h,--help}'[print help information and exit]' \
+      '(- *)'{-V,--version}'[print version information and exit]' \
+      && return
+  ;;
+  virt-xml-validate)
+    _arguments -A "-*" -S \
+      '(- *)'{-h,--help}'[print help information and exit]' \
+      '(- *)'{-V,--version}'[print version information and exit]' \
+      '1:file:_files -g "*.xml(-.)"' \
+      '2:schema:(domainsnapshot domain network storagepool storagevol nodedev capability nwfilter secret interface)' \
+      && return
+  ;;
 esac
 
 case $state in
@@ -50,6 +98,21 @@ case $state in
     fi
     _values -w options ${=_cache_virsh_cmdopts[$cmd]} && ret=0
   ;;
+  virt_admin_cmds)
+    _wanted commands expl 'virt-admin command' compadd -a _cache_virt_admin_cmds && ret=0
+  ;;
+  virt_admin_cmdopts)
+    local cmd
+    for (( i = 2; i <= $#words; i++ )); do
+      [[ -n "${_cache_virt_admin_cmds[(r)$words[$i]]}" ]] && cmd=$words[$i] && break
+    done
+    [[ -z $cmd ]] && return 1
+    if [[ -z $_cache_virt_admin_cmdopts[$cmd] ]]; then
+      _cache_virt_admin_cmdopts[$cmd]=${(M)${${${${=${(f)"$(_call_program virt-admin virt-admin help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*}
+    fi
+    _values -w options ${=_cache_virt_admin_cmdopts[$cmd]} && ret=0
+  ;;
+
 esac
 
 return ret

Thanks,

-- 
Marko Myllynen

