diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-03-22 13:56:47 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-03-22 13:56:47 +0900 |
commit | 34e29a1102d06769987682be5be5d2bd6f1a53bf (patch) | |
tree | 9bae0849f21aa4e0123bd7b33eab60077dacc549 | |
parent | 623de6fbda2ce263c4915dcf992db11045cf33e3 (diff) |
add bash completion for gobgpcli
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rwxr-xr-x | cli/gobgpcli | 12 | ||||
-rw-r--r-- | tools/completion/gobgpcli-completion.bash | 180 |
2 files changed, 190 insertions, 2 deletions
diff --git a/cli/gobgpcli b/cli/gobgpcli index b2130f2d..f3cde90a 100755 --- a/cli/gobgpcli +++ b/cli/gobgpcli @@ -183,11 +183,17 @@ class Show(object): print neighbors return 0 sorted_neighbors = [] + for n in sorted(neighbors, key=lambda n: n["conf"]["remote_ip"]): + sorted_neighbors.append(n) + if self.options.quiet: + sorted_neighbors_ip = [] + for n in sorted_neighbors: + print str(n["conf"]["remote_ip"]) + return 0 maxaddrlen = 0 maxaslen = 0 maxtimelen = len("Up/Down") - for n in sorted(neighbors, key=lambda n: n["conf"]["remote_ip"]): - sorted_neighbors.append(n) + for n in sorted_neighbors: if len(n["conf"]["remote_ip"]) > maxaddrlen: maxaddrlen = len(n["conf"]["remote_ip"]) if len(str(n["conf"]["remote_as"])) > maxaslen: @@ -370,6 +376,8 @@ def main(): help="specifying a port (8080 by default)") parser.add_option("-d", "--debug", dest="debug", action="store_true", help="dump raw json") + parser.add_option("-q", "--quiet", dest="quiet", action="store_true", + help="for shell completion") (options, args) = parser.parse_args() diff --git a/tools/completion/gobgpcli-completion.bash b/tools/completion/gobgpcli-completion.bash new file mode 100644 index 00000000..4b92292e --- /dev/null +++ b/tools/completion/gobgpcli-completion.bash @@ -0,0 +1,180 @@ +#!bash + +__gobgpcli_q() { + gobgpcli 2>/dev/null "$@" +} + +__search_target() { + local word target c=1 + + while [ $c -lt $cword ]; do + word="${words[c]}" + for target in $1; do + if [ "$target" = "$word" ]; then + echo "$target" + return + fi + done + ((c++)) + done +} + +__gobgpcli_table_list() { + local table_list=("local adj-in adj-out") + local table="$(__search_target "${table_list}")" + if [ -z "$table" ]; then + case "$cur" in + *) + COMPREPLY=( $( compgen -W "${table_list}" -- "$cur") ) + ;; + esac + return + fi + COMPREPLY=( $( compgen -W "ipv4 ipv6 evpn" -- "$cur") ) + return +} + +__gobgpcli_neighbr_list() { + local neighbor_list=( $(__gobgpcli_q show --quiet neighbors) ) + local target="$(__search_target "${neighbor_list[*]}")" + if [ -z "$target" ]; then + case "$cur" in + *) + COMPREPLY=( $( compgen -W "${neighbor_list[*]}" -- "$cur") ) + ;; + esac + __ltrim_colon_completions "$cur" + return + fi +} + +_gobgpcli_show_neighbor() { + __gobgpcli_neighbr_list + if [ -z ${COMPREPLY} ]; then + __gobgpcli_table_list + return + fi +} + +_gobgpcli_show_neighbors() { + case "$cur" in + *) + ;; + esac + return +} + +_gobgpcli_show_global() { + local targets="ipv4 ipv6 evpn" + local target="$(__search_target "$targets")" + if [ -z "$target" ]; then + case "$cur" in + *) + COMPREPLY=( $( compgen -W "${targets[*]}" -- "$cur" ) ) + ;; + esac + return + fi +} + +_gobgpcli_show() { + local targets="neighbor neighbors global" + local target="$(__search_target "$targets")" + if [ -z "$target" ]; then + case "$cur" in + *) + COMPREPLY=( $( compgen -W "${targets[*]}" -- "$cur" ) ) + ;; + esac + return + fi + _gobgpcli_show_${target} +} + +__gobgpcli_generic_reset() { + local targets="neighbor" + local target="$(__search_target "$targets")" + if [ -z "$target" ]; then + case "$cur" in + *) + COMPREPLY=( $( compgen -W "${targets[*]}" -- "$cur" ) ) + ;; + esac + return + fi + __gobgpcli_neighbr_list +} + +_gobgpcli_reset() { + __gobgpcli_generic_reset +} + +_gobgpcli_softresetin() { + __gobgpcli_generic_reset +} + +_gobgpcli_softresetout() { + __gobgpcli_generic_reset +} + +_gobgpcli_shutdown() { + __gobgpcli_generic_reset +} + +_gobgpcli_gobgpcli() { + case "$prev" in + -h) + return + ;; + *) + ;; + esac + + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "-h" -- "$cur" ) ) + ;; + *) + COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) ) + ;; + esac +} + +_gobgpcli() { + local commands=( + show + reset + softresetin + softresetout + shutdown + ) + + COMPREPLY=() + local cur prev words cword + _get_comp_words_by_ref -n : cur prev words cword + + local command='gobgpcli' + local counter=1 + while [ $counter -lt $cword ]; do + case "${words[$counter]}" in + -h) + (( counter++ )) + ;; + -*) + ;; + *) + command="${words[$counter]}" + cpos=$counter + (( cpos++ )) + break + ;; + esac + (( counter++ )) + done + local completions_func=_gobgpcli_${command} + declare -F $completions_func > /dev/null && $completions_func + + return 0 +} + +complete -F _gobgpcli gobgpcli |