summaryrefslogtreecommitdiffhomepage
path: root/tools/completion/README.md
blob: f33965e97ec84b135e5cb1d6383b72fd11d6698d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Completion

This page explains completion for gobgp client.

## Bash completion

The described how to use and how to customize of bash completion.

### How to use

1. install bash-completion as follows:

 ```
 % sudo apt-get install bash-completion
 ```

1. add gobgp's path to PATH environment variable

 If you run 'go get github.com/osrg/gobgp/gobgp', gobgp command is installed in $GOPATH/bin.
 ```
 % export PATH=$PATH:$GOPATH/bin
 ```

1. load completion file

 ```
 % source $GOPATH/src/github.com/osrg/gobgp/tools/completion/gobgp-completion.bash
 ```

You can use tab completion for gobgp after loading gobgp-completion.bash.

### How to customize
 In order to customize the bash completion, please follow steps below:

1. generate bash completion file

 Generate the bash completion file by using binary of gobgp client.
 This generating function uses [cobra bash completion](https://github.com/spf13/cobra#generating-bash-completions-for-your-command) internally.
 ```
 % gobgp --gen-cmpl --bash-cmpl-file=<specifying a file name>
 ```

 The following function is generated if added the "gobgp neighbor" command  to gobgp client.

 ```
 _gobgp_neighbor()
 {
     last_command="gobgp_neighbor"
     commands=()

     flags=()
     two_word_flags=()
     flags_with_completion=()
     flags_completion=()

     flags+=("--bash-cmpl-file=")
     flags+=("--debug")
     flags+=("-d")
     flags+=("--gen-cmpl")
     flags+=("-c")
     flags+=("--host=")
     two_word_flags+=("-u")
     flags+=("--json")
     flags+=("-j")
     flags+=("--port=")
     two_word_flags+=("-p")
     flags+=("--quiet")
     flags+=("-q")

     must_have_one_flag=()
     must_have_one_noun=()
 }
  ```

1. copy the generated functions

 Copy the above function to **gogbp-static-completion.bash**.

1. implement a dynamic completion

 If you want to add dynamic completion, you need to implement that part yourself.
 For example, if you want to add the neighbor address after the "gobgp neighbor" command dynamically, you can achieve it by implementing the specific internal command in **gobgp-dynamic-completion.bash**.

 1. implement command to get a list of the neighbor address

	You need to add the processing function of the following like below to **gobgp-dynamic-completion.bash**.
    ```
    # Get bgp neighbors by using gobgp command.
    __gobgp_q_neighbor()
    {
        local neighbors=( $(__gobgp_q $url $port --quiet neighbor) )
        case "${neighbors[*]}" in
            "grpc: timed out"* | "rpc error:"* )
                req_faild="True"
                return
            ;;
        esac
        for n in ${neighbors[*]}; do
            commands+=($n)
        done
        searched="True"
    }
    ```

 1. add a call to "__gobgp_q_neighbor"

	You can call the above functions by implementing as follows to **gobgp-static-completion.bash**:
    ```
    _gobgp_neighbor()
    {
        last_command="gobgp_neighbor"
        commands=()

        flags=()
        two_word_flags=()
        flags_with_completion=()
        flags_completion=()

        flags+=("--bash-cmpl-file=")
        flags+=("--debug")
        flags+=("-d")
        flags+=("--gen-cmpl")
        flags+=("-c")
        flags+=("--host=")
        two_word_flags+=("-u")
        flags+=("--json")
        flags+=("-j")
        flags+=("--port=")
        two_word_flags+=("-p")
        flags+=("--quiet")
        flags+=("-q")

        must_have_one_flag=()
        must_have_one_noun=()

        # Implement call processing to here
        __gobgp_q_neighbor
    }
    ```

 1. implement the handle processing

	If you want to add the completion following the "gobgp neighbor \<neighbor address\>" command, you need to add a handler for _gobgp_neighbor_addr() like below to "__handle_gobgp_command()" function in **gobgp-dynamic-completion.bash**.

    ```
    case "${last_command}" in
        # Control after dynamic completion of bgp neighbor command
        gobgp_neighbor )
            next_command="_${last_command}_addr"
        ;;
    esac
    ```

	"next_command" variable above indicates a function to be called after "gobgp neighbor", and the function name of next command is supposed to be "_gobgp_neighbor_addr" in the example above.
     Therefore the actual "gobgp_neighbor_addr" function needs to be implemented in **gobgp-dynamic-completion.bash**.
    ```
    _gobgp_neighbor_addr()
    {
        last_command="gobgp_neighbor_addr"

        commands=()
        commands+=("local")
        commands+=("adj-in")
        commands+=("adj-out")
        commands+=("reset")
        commands+=("softreset")
        commands+=("softresetin")
        commands+=("softresetout")
        commands+=("shutdown")
        commands+=("enable")
        commands+=("disable")
        commands+=("policy")

        flags=()
        two_word_flags=()
        flags_with_completion=()
        flags_completion=()

        flags+=("--address-family=")
        two_word_flags+=("-a")
        flags+=("--bash-cmpl-file=")
        flags+=("--debug")
        flags+=("-d")
        flags+=("--gen-cmpl")
        flags+=("-c")
        flags+=("--host=")
        two_word_flags+=("-u")
        flags+=("--json")
        flags+=("-j")
        flags+=("--port=")
        two_word_flags+=("-p")
        flags+=("--quiet")
        flags+=("-q")

        must_have_one_flag=()
        must_have_one_noun=()
    }
    ```

1. delete the generated bash completion file.

## Zsh completion

The described how to use of bash completion.

### How to use

zsh completion for gobgp works by adding the path of gobgp zsh completion directory to $fpath and enabling zsh completion like below:

 ```
 % vi ~/.zshrc

 GOBGP_COMP=$GOPATH/src/github.com/osrg/gobgp/tools/completion/zsh
 fpath=($GOBGP_COMP $fpath)

 autoload -Uz compinit
 compinit

 ```