-
-
Save pfreixes/3187511 to your computer and use it in GitHub Desktop.
# pfreixes, 2012-07-27 | |
# Add to /etc/bash_completion.d/supervisorctl | |
_supervisor() | |
{ | |
local cur prev opts base | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
prev="${COMP_WORDS[COMP_CWORD-1]}" | |
# | |
# The basic options we'll complete. | |
# | |
opts="add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version" | |
# | |
# Complete the arguments to some of the basic commands. | |
# | |
case "${prev}" in | |
start|stop|restart) | |
local process=$(for x in `supervisorctl avail | awk '{print $1}'`; do echo ${x} ; done ) | |
COMPREPLY=( $(compgen -W "${process}" -- ${cur}) ) | |
return 0 | |
;; | |
*) | |
;; | |
esac | |
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) | |
return 0 | |
} | |
complete -F _supervisor supervisorctl |
another good practice is also to do:
have supervisorctl && complete -F _supervisor supervisorctl
to check that the program exists before adding the bash completion for it.
Adding 'tail' to the basic commands with argument completion would be really useful :)
Final version
have supervisorctl &&
_supervisor()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
#
# The basic options we'll complete.
#
opts="add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version"
#
# Complete the arguments to some of the basic commands.
#
case "${prev}" in
start|stop|restart)
local process=$(for x in `supervisorctl avail | awk '{print $1}'`; do echo ${x} ; done )
COMPREPLY=( $(compgen -W "${process} all" -- ${cur}) )
return 0
;;
*)
;;
esac
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
} &&
complete -F _supervisor supervisorctl
This will not work if You are using groups of processes. In this case, supervisor's processes looks like <group_name:process_name>, e.g. database:etcd
, but bash-completion treats colon ':' as a separator and autocomplete fails.
Solution using _init_completion -n :
and __ltrim_colon_completions "$cur"
:
_supervisor()
{
local cur prev words cword
COMPREPLY=()
_init_completion -n : || return
opts="add clear fg open remove restart start stop update avail maintail pid reload reread shutdown status tail version help"
COMPREPLY=( $(compgen -W "$opts" -- $cur) )
case "$prev" in
start|stop|restart|status|signal|tail|pid|clear|fg|add|remove)
local processes=$(supervisorctl avail | awk '{print $1}')
COMPREPLY=( $(compgen -W "$processes all" -- $cur) )
;;
update)
local groups=$(supervisorctl avail | awk '{print $1}' | awk -F':' '{print $1}' | sort -u)
COMPREPLY=( $(compgen -W "$groups all" -- $cur) )
;;
*)
;;
esac
__ltrim_colon_completions "$cur"
return 0
} &&
complete -F _supervisor supervisorctl
Also, i've devided commands because supervisorctl update
expects a group name, not a process. Also added additional 'all' keyword into the list to get status, stop, start or something else of all items.
Tested on Linux. In MacOS _init_completion
may not work (maybe _get_comp_words_by_ref -n : cur
must be used as alternative)
I suggest to change:$(compgen -W "$ {process}" -- ${cur}) )$(compgen -W "$ {process} all" -- ${cur}) )
COMPREPLY=(
to:
COMPREPLY=(
as it will complete "supervisor restart a" to "supervisor restart all"