github.com/osrg/gobgp@v2.0.0+incompatible/cmd/gobgp/root.go (about) 1 // Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package main 17 18 import ( 19 "context" 20 "fmt" 21 "net/http" 22 _ "net/http/pprof" 23 24 api "github.com/osrg/gobgp/api" 25 "github.com/spf13/cobra" 26 ) 27 28 var globalOpts struct { 29 Host string 30 Port int 31 Debug bool 32 Quiet bool 33 Json bool 34 GenCmpl bool 35 BashCmplFile string 36 PprofPort int 37 TLS bool 38 CaFile string 39 } 40 41 var client api.GobgpApiClient 42 var ctx context.Context 43 44 func newRootCmd() *cobra.Command { 45 cobra.EnablePrefixMatching = true 46 var cancel context.CancelFunc 47 rootCmd := &cobra.Command{ 48 Use: "gobgp", 49 PersistentPreRun: func(cmd *cobra.Command, args []string) { 50 if globalOpts.PprofPort > 0 { 51 go func() { 52 if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", globalOpts.PprofPort), nil); err != nil { 53 exitWithError(err) 54 } 55 }() 56 } 57 58 if !globalOpts.GenCmpl { 59 var err error 60 ctx = context.Background() 61 client, cancel, err = newClient(ctx) 62 if err != nil { 63 cancel() 64 exitWithError(err) 65 } 66 } 67 }, 68 Run: func(cmd *cobra.Command, args []string) { 69 if globalOpts.GenCmpl { 70 cmd.GenBashCompletionFile(globalOpts.BashCmplFile) 71 } else { 72 cmd.HelpFunc()(cmd, args) 73 } 74 }, 75 PersistentPostRun: func(cmd *cobra.Command, args []string) { 76 // if children declare their own, cancel is not called. Doesn't matter because the command will exit soon. 77 if cancel != nil { 78 cancel() 79 } 80 }, 81 } 82 83 rootCmd.PersistentFlags().StringVarP(&globalOpts.Host, "host", "u", "127.0.0.1", "host") 84 rootCmd.PersistentFlags().IntVarP(&globalOpts.Port, "port", "p", 50051, "port") 85 rootCmd.PersistentFlags().BoolVarP(&globalOpts.Json, "json", "j", false, "use json format to output format") 86 rootCmd.PersistentFlags().BoolVarP(&globalOpts.Debug, "debug", "d", false, "use debug") 87 rootCmd.PersistentFlags().BoolVarP(&globalOpts.Quiet, "quiet", "q", false, "use quiet") 88 rootCmd.PersistentFlags().BoolVarP(&globalOpts.GenCmpl, "gen-cmpl", "c", false, "generate completion file") 89 rootCmd.PersistentFlags().StringVarP(&globalOpts.BashCmplFile, "bash-cmpl-file", "", "gobgp-completion.bash", "bash cmpl filename") 90 rootCmd.PersistentFlags().IntVarP(&globalOpts.PprofPort, "pprof-port", "r", 0, "pprof port") 91 rootCmd.PersistentFlags().BoolVarP(&globalOpts.TLS, "tls", "", false, "connection uses TLS if true, else plain TCP") 92 rootCmd.PersistentFlags().StringVarP(&globalOpts.CaFile, "tls-ca-file", "", "", "The file containing the CA root cert file") 93 94 globalCmd := newGlobalCmd() 95 neighborCmd := newNeighborCmd() 96 vrfCmd := newVrfCmd() 97 policyCmd := newPolicyCmd() 98 monitorCmd := newMonitorCmd() 99 mrtCmd := newMrtCmd() 100 rpkiCmd := newRPKICmd() 101 bmpCmd := newBmpCmd() 102 rootCmd.AddCommand(globalCmd, neighborCmd, vrfCmd, policyCmd, monitorCmd, mrtCmd, rpkiCmd, bmpCmd) 103 return rootCmd 104 }