/
root.go
104 lines (95 loc) · 3.34 KB
/
root.go
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
// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
api "github.com/osrg/gobgp/api"
"github.com/spf13/cobra"
)
var globalOpts struct {
Host string
Port int
Debug bool
Quiet bool
Json bool
GenCmpl bool
BashCmplFile string
PprofPort int
TLS bool
CaFile string
}
var client api.GobgpApiClient
var ctx context.Context
func newRootCmd() *cobra.Command {
cobra.EnablePrefixMatching = true
var cancel context.CancelFunc
rootCmd := &cobra.Command{
Use: "gobgp",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if globalOpts.PprofPort > 0 {
go func() {
if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", globalOpts.PprofPort), nil); err != nil {
exitWithError(err)
}
}()
}
if !globalOpts.GenCmpl {
var err error
ctx = context.Background()
client, cancel, err = newClient(ctx)
if err != nil {
cancel()
exitWithError(err)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
if globalOpts.GenCmpl {
cmd.GenBashCompletionFile(globalOpts.BashCmplFile)
} else {
cmd.HelpFunc()(cmd, args)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// if children declare their own, cancel is not called. Doesn't matter because the command will exit soon.
if cancel != nil {
cancel()
}
},
}
rootCmd.PersistentFlags().StringVarP(&globalOpts.Host, "host", "u", "127.0.0.1", "host")
rootCmd.PersistentFlags().IntVarP(&globalOpts.Port, "port", "p", 50051, "port")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Json, "json", "j", false, "use json format to output format")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Debug, "debug", "d", false, "use debug")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Quiet, "quiet", "q", false, "use quiet")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.GenCmpl, "gen-cmpl", "c", false, "generate completion file")
rootCmd.PersistentFlags().StringVarP(&globalOpts.BashCmplFile, "bash-cmpl-file", "", "gobgp-completion.bash", "bash cmpl filename")
rootCmd.PersistentFlags().IntVarP(&globalOpts.PprofPort, "pprof-port", "r", 0, "pprof port")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.TLS, "tls", "", false, "connection uses TLS if true, else plain TCP")
rootCmd.PersistentFlags().StringVarP(&globalOpts.CaFile, "tls-ca-file", "", "", "The file containing the CA root cert file")
globalCmd := newGlobalCmd()
neighborCmd := newNeighborCmd()
vrfCmd := newVrfCmd()
policyCmd := newPolicyCmd()
monitorCmd := newMonitorCmd()
mrtCmd := newMrtCmd()
rpkiCmd := newRPKICmd()
bmpCmd := newBmpCmd()
rootCmd.AddCommand(globalCmd, neighborCmd, vrfCmd, policyCmd, monitorCmd, mrtCmd, rpkiCmd, bmpCmd)
return rootCmd
}