github.com/osrg/gobgp@v2.0.0+incompatible/tools/completion/README.md (about) 1 # Completion 2 3 This page explains completion for gobgp client. 4 5 ## Bash completion 6 7 The described how to use and how to customize of bash completion. 8 9 ### How to use 10 11 1. install bash-completion as follows: 12 13 ```bash 14 % sudo apt-get install bash-completion 15 ``` 16 17 1. add gobgp's path to PATH environment variable 18 19 If you run 'go get github.com/osrg/gobgp/gobgp', gobgp command is installed 20 in $GOPATH/bin. 21 22 ```bash 23 % export PATH=$PATH:$GOPATH/bin 24 ``` 25 26 1. load completion file 27 28 ```bash 29 % source $GOPATH/src/github.com/osrg/gobgp/tools/completion/gobgp-completion.bash 30 ``` 31 32 You can use tab completion for gobgp after loading gobgp-completion.bash. 33 34 ### How to customize 35 36 In order to customize the bash completion, please follow steps below: 37 38 1. generate bash completion file 39 40 Generate the bash completion file by using binary of gobgp client. 41 This generating function uses 42 [cobra bash completion](https://github.com/spf13/cobra#generating-bash-completions-for-your-command) 43 internally. 44 45 ```bash 46 % gobgp --gen-cmpl --bash-cmpl-file=<specifying a file name> 47 ``` 48 49 The following function is generated if added the "gobgp neighbor" command 50 to gobgp client. 51 52 ```bash 53 _gobgp_neighbor() 54 { 55 last_command="gobgp_neighbor" 56 commands=() 57 58 flags=() 59 two_word_flags=() 60 flags_with_completion=() 61 flags_completion=() 62 63 flags+=("--bash-cmpl-file=") 64 flags+=("--debug") 65 flags+=("-d") 66 flags+=("--gen-cmpl") 67 flags+=("-c") 68 flags+=("--host=") 69 two_word_flags+=("-u") 70 flags+=("--json") 71 flags+=("-j") 72 flags+=("--port=") 73 two_word_flags+=("-p") 74 flags+=("--quiet") 75 flags+=("-q") 76 77 must_have_one_flag=() 78 must_have_one_noun=() 79 } 80 ``` 81 82 1. copy the generated functions 83 84 Copy the above function to **gogbp-static-completion.bash**. 85 86 1. implement a dynamic completion 87 88 If you want to add dynamic completion, you need to implement that part 89 yourself. 90 For example, if you want to add the neighbor address after the "gobgp 91 neighbor" command dynamically, you can achieve it by implementing the 92 specific internal command in **gobgp-dynamic-completion.bash**. 93 94 1. implement command to get a list of the neighbor address 95 96 You need to add the processing function of the following like below to 97 **gobgp-dynamic-completion.bash**. 98 99 ```bash 100 # Get bgp neighbors by using gobgp command. 101 __gobgp_q_neighbor() 102 { 103 local neighbors=( $(__gobgp_q $url $port --quiet neighbor) ) 104 case "${neighbors[*]}" in 105 "grpc: timed out"* | "rpc error:"* ) 106 req_faild="True" 107 return 108 ;; 109 esac 110 for n in ${neighbors[*]}; do 111 commands+=($n) 112 done 113 searched="True" 114 } 115 ``` 116 117 1. add a call to "__gobgp_q_neighbor" 118 119 You can call the above functions by implementing as follows to 120 **gobgp-static-completion.bash**: 121 122 ```bash 123 _gobgp_neighbor() 124 { 125 last_command="gobgp_neighbor" 126 commands=() 127 128 flags=() 129 two_word_flags=() 130 flags_with_completion=() 131 flags_completion=() 132 133 flags+=("--bash-cmpl-file=") 134 flags+=("--debug") 135 flags+=("-d") 136 flags+=("--gen-cmpl") 137 flags+=("-c") 138 flags+=("--host=") 139 two_word_flags+=("-u") 140 flags+=("--json") 141 flags+=("-j") 142 flags+=("--port=") 143 two_word_flags+=("-p") 144 flags+=("--quiet") 145 flags+=("-q") 146 147 must_have_one_flag=() 148 must_have_one_noun=() 149 150 # Implement call processing to here 151 __gobgp_q_neighbor 152 } 153 ``` 154 155 1. implement the handle processing 156 157 If you want to add the completion following the "gobgp neighbor \<neighbor 158 address\>" command, you need to add a handler for _gobgp_neighbor_addr() 159 like below to "__handle_gobgp_command()" function in 160 **gobgp-dynamic-completion.bash**. 161 162 ```bash 163 case "${last_command}" in 164 # Control after dynamic completion of bgp neighbor command 165 gobgp_neighbor ) 166 next_command="_${last_command}_addr" 167 ;; 168 esac 169 ``` 170 171 "next_command" variable above indicates a function to be called after 172 "gobgp neighbor", and the function name of next command is supposed to be 173 "_gobgp_neighbor_addr" in the example above. 174 Therefore the actual "gobgp_neighbor_addr" function needs to be 175 implemented in **gobgp-dynamic-completion.bash**. 176 177 ```bash 178 _gobgp_neighbor_addr() 179 { 180 last_command="gobgp_neighbor_addr" 181 182 commands=() 183 commands+=("local") 184 commands+=("adj-in") 185 commands+=("adj-out") 186 commands+=("reset") 187 commands+=("softreset") 188 commands+=("softresetin") 189 commands+=("softresetout") 190 commands+=("shutdown") 191 commands+=("enable") 192 commands+=("disable") 193 commands+=("policy") 194 195 flags=() 196 two_word_flags=() 197 flags_with_completion=() 198 flags_completion=() 199 200 flags+=("--address-family=") 201 two_word_flags+=("-a") 202 flags+=("--bash-cmpl-file=") 203 flags+=("--debug") 204 flags+=("-d") 205 flags+=("--gen-cmpl") 206 flags+=("-c") 207 flags+=("--host=") 208 two_word_flags+=("-u") 209 flags+=("--json") 210 flags+=("-j") 211 flags+=("--port=") 212 two_word_flags+=("-p") 213 flags+=("--quiet") 214 flags+=("-q") 215 216 must_have_one_flag=() 217 must_have_one_noun=() 218 } 219 ``` 220 221 1. delete the generated bash completion file. 222 223 ## Zsh completion 224 225 The described how to use of bash completion. 226 227 ### How to use 228 229 zsh completion for gobgp works by adding the path of gobgp zsh completion 230 directory to $fpath and enabling zsh completion like below: 231 232 ```bash 233 % vi ~/.zshrc 234 235 GOBGP_COMP=$GOPATH/src/github.com/osrg/gobgp/tools/completion/zsh 236 fpath=($GOBGP_COMP $fpath) 237 238 autoload -Uz compinit 239 compinit 240 ```