Merge pull request #102 from scotje/service_subcommand_completions

Update completion scripts for `service` subcommands
This commit is contained in:
Gene Liverman 2020-09-25 13:02:06 -04:00 committed by GitHub
commit a4231cc6f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 15 deletions

View file

@ -2,29 +2,37 @@
_vmfloaty() _vmfloaty()
{ {
local cur prev subcommands template_subcommands hostname_subcommands local cur prev commands template_arg_commands hostname_arg_commands service_subcommands
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
subcommands="delete get help list modify query revert service snapshot ssh status summary token" commands="delete get help list modify query revert service snapshot ssh status summary token"
template_subcommands="get ssh" template_arg_commands="get ssh"
hostname_subcommands="delete modify query revert snapshot" hostname_arg_commands="delete modify query revert snapshot"
service_subcommands="types examples"
if [[ $cur == -* ]] ; then if [[ $cur == -* ]] ; then
# TODO: option completion # TODO: option completion
COMPREPLY=() COMPREPLY=()
elif [[ $template_subcommands =~ (^| )$prev($| ) ]] ; then elif [[ $template_arg_commands =~ (^| )$prev($| ) ]] ; then
if [[ -z "$_vmfloaty_avail_templates" ]] ; then if [[ -z "$_vmfloaty_avail_templates" ]] ; then
# TODO: need a --hostnameonly equivalent here because the section headers of
# `floaty list` are adding some spurious entries (including files in current
# directory because part of the headers is `**` which is getting expanded)
_vmfloaty_avail_templates=$(floaty list 2>/dev/null) _vmfloaty_avail_templates=$(floaty list 2>/dev/null)
fi fi
COMPREPLY=( $(compgen -W "${_vmfloaty_avail_templates}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${_vmfloaty_avail_templates}" -- "${cur}") )
elif [[ $hostname_subcommands =~ (^| )$prev($| ) ]] ; then elif [[ $hostname_arg_commands =~ (^| )$prev($| ) ]] ; then
_vmfloaty_active_hostnames=$(floaty list --active --hostnameonly 2>/dev/null) _vmfloaty_active_hostnames=$(floaty list --active --hostnameonly 2>/dev/null)
COMPREPLY=( $(compgen -W "${_vmfloaty_active_hostnames}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${_vmfloaty_active_hostnames}" -- "${cur}") )
else elif [[ "service" == $prev ]] ; then
COMPREPLY=( $(compgen -W "${subcommands}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${service_subcommands}" -- "${cur}") )
elif [[ $1 == $prev ]] ; then
# only show top level commands we are at root
COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
fi fi
} }
complete -F _vmfloaty floaty complete -F _vmfloaty floaty

View file

@ -1,26 +1,32 @@
_floaty() _floaty()
{ {
local line subcommands template_subcommands hostname_subcommands local line commands template_arg_commands hostname_arg_commands service_subcommands
subcommands="delete get help list modify query revert snapshot ssh status summary token" commands="delete get help list modify query revert service snapshot ssh status summary token"
template_subcommands=("get" "ssh") template_arg_commands=("get" "ssh")
hostname_subcommands=("delete" "modify" "query" "revert" "snapshot") hostname_arg_commands=("delete" "modify" "query" "revert" "snapshot")
service_subcommands=("types" "examples")
_arguments -C \ _arguments -C \
"1: :(${subcommands})" \ "1: :(${commands})" \
"*::arg:->args" "*::arg:->args"
if ((template_subcommands[(Ie)$line[1]])); then if ((template_arg_commands[(Ie)$line[1]])); then
_floaty_template_sub _floaty_template_sub
elif ((hostname_subcommands[(Ie)$line[1]])); then elif ((hostname_arg_commands[(Ie)$line[1]])); then
_floaty_hostname_sub _floaty_hostname_sub
elif [[ "service" == $line[1] ]]; then
_arguments "1: :(${service_subcommands})"
fi fi
} }
_floaty_template_sub() _floaty_template_sub()
{ {
if [[ -z "$_vmfloaty_avail_templates" ]] ; then if [[ -z "$_vmfloaty_avail_templates" ]] ; then
# TODO: need a --hostnameonly equivalent here because the section headers of
# `floaty list` are adding some spurious entries (including files in current
# directory because part of the headers is `**` which is getting expanded)
_vmfloaty_avail_templates=$(floaty list 2>/dev/null) _vmfloaty_avail_templates=$(floaty list 2>/dev/null)
fi fi