diff options
-rw-r--r-- | gobgp/cmd/bmp.go | 4 | ||||
-rw-r--r-- | gobgp/cmd/common.go | 16 | ||||
-rw-r--r-- | gobgp/cmd/global.go | 25 | ||||
-rw-r--r-- | gobgp/cmd/monitor.go | 40 | ||||
-rw-r--r-- | gobgp/cmd/mrt.go | 78 | ||||
-rw-r--r-- | gobgp/cmd/neighbor.go | 25 | ||||
-rw-r--r-- | gobgp/cmd/policy.go | 22 | ||||
-rw-r--r-- | gobgp/cmd/rpki.go | 15 | ||||
-rw-r--r-- | gobgp/cmd/vrf.go | 13 |
9 files changed, 86 insertions, 152 deletions
diff --git a/gobgp/cmd/bmp.go b/gobgp/cmd/bmp.go index 316f4ded..9a29070f 100644 --- a/gobgp/cmd/bmp.go +++ b/gobgp/cmd/bmp.go @@ -22,7 +22,6 @@ import ( "github.com/spf13/cobra" "golang.org/x/net/context" "net" - "os" "strconv" ) @@ -82,8 +81,7 @@ func NewBmpCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modBmpServer(cmd.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } diff --git a/gobgp/cmd/common.go b/gobgp/cmd/common.go index e039c5ff..f91ee975 100644 --- a/gobgp/cmd/common.go +++ b/gobgp/cmd/common.go @@ -17,6 +17,7 @@ package cmd import ( "bytes" + "encoding/json" "fmt" "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/packet" @@ -387,8 +388,7 @@ func connGrpc() *grpc.ClientConn { target := net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port)) conn, err := grpc.Dial(target, timeout, grpc.WithBlock(), grpc.WithInsecure()) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } return conn } @@ -431,3 +431,15 @@ func checkAddressFamily(def bgp.RouteFamily) (bgp.RouteFamily, error) { } return rf, e } + +func exitWithError(err error) { + if globalOpts.Json { + j, _ := json.Marshal(struct { + Error string `json:"error"` + }{Error: err.Error()}) + fmt.Println(string(j)) + } else { + fmt.Println(err) + } + os.Exit(1) +} diff --git a/gobgp/cmd/global.go b/gobgp/cmd/global.go index cfa66619..3ef1f64a 100644 --- a/gobgp/cmd/global.go +++ b/gobgp/cmd/global.go @@ -22,7 +22,6 @@ import ( "github.com/spf13/cobra" "golang.org/x/net/context" "net" - "os" "regexp" "strconv" "strings" @@ -741,8 +740,7 @@ func NewGlobalCmd() *cobra.Command { err = showGlobalConfig(args) } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -751,8 +749,7 @@ func NewGlobalCmd() *cobra.Command { Use: CMD_RIB, Run: func(cmd *cobra.Command, args []string) { if err := showGlobalRib(args); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -765,8 +762,7 @@ func NewGlobalCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modPath(api.Resource_GLOBAL, "", cmd.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -778,8 +774,7 @@ func NewGlobalCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { family, err := checkAddressFamily(bgp.RouteFamily(0)) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } arg := &api.ModPathArguments{ Operation: api.Operation_DEL_ALL, @@ -788,8 +783,7 @@ func NewGlobalCmd() *cobra.Command { } _, err = client.ModPath(context.Background(), arg) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -802,8 +796,7 @@ func NewGlobalCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { for _, v := range []string{CMD_IMPORT, CMD_EXPORT} { if err := showNeighborPolicy(nil, v, 4); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } } }, @@ -814,8 +807,7 @@ func NewGlobalCmd() *cobra.Command { Use: v, Run: func(cmd *cobra.Command, args []string) { if err := showNeighborPolicy(nil, cmd.Use, 0); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -826,8 +818,7 @@ func NewGlobalCmd() *cobra.Command { Run: func(subcmd *cobra.Command, args []string) { err := modNeighborPolicy(nil, cmd.Use, subcmd.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } diff --git a/gobgp/cmd/monitor.go b/gobgp/cmd/monitor.go index 88142866..f432ce45 100644 --- a/gobgp/cmd/monitor.go +++ b/gobgp/cmd/monitor.go @@ -24,7 +24,6 @@ import ( "golang.org/x/net/context" "io" "net" - "os" "strconv" "time" ) @@ -35,8 +34,7 @@ func NewMonitorCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { family, err := checkAddressFamily(bgp.RouteFamily(0)) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } arg := &gobgpapi.Arguments{ Resource: gobgpapi.Resource_GLOBAL, @@ -45,21 +43,18 @@ func NewMonitorCmd() *cobra.Command { stream, err := client.MonitorBestChanged(context.Background(), arg) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } for { d, err := stream.Recv() if err == io.EOF { break } else if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } p, err := ApiStruct2Path(d.Paths[0]) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } if globalOpts.Json { @@ -93,16 +88,14 @@ func NewMonitorCmd() *cobra.Command { stream, err := client.MonitorPeerState(context.Background(), arg) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } for { s, err := stream.Recv() if err == io.EOF { break } else if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } if globalOpts.Json { j, _ := json.Marshal(s) @@ -119,16 +112,14 @@ func NewMonitorCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { stream, err := client.MonitorROAValidation(context.Background(), &gobgpapi.Arguments{}) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } for { s, err := stream.Recv() if err == io.EOF { break } else if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } if globalOpts.Json { j, _ := json.Marshal(s) @@ -171,15 +162,13 @@ func NewMonitorCmd() *cobra.Command { if len(args) > 0 { remoteIP := net.ParseIP(args[0]) if remoteIP == nil { - fmt.Println("invalid ip address: %s", args[0]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[0])) } name = args[0] } family, err := checkAddressFamily(bgp.RouteFamily(0)) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } arg := &gobgpapi.Table{ Type: gobgpapi.Resource_ADJ_IN, @@ -189,21 +178,18 @@ func NewMonitorCmd() *cobra.Command { stream, err := client.MonitorRib(context.Background(), arg) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } for { d, err := stream.Recv() if err == io.EOF { break } else if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } p, err := ApiStruct2Path(d.Paths[0]) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } if globalOpts.Json { diff --git a/gobgp/cmd/mrt.go b/gobgp/cmd/mrt.go index 3c87ce46..bc28adee 100644 --- a/gobgp/cmd/mrt.go +++ b/gobgp/cmd/mrt.go @@ -41,28 +41,24 @@ func printMrtMsgs(data []byte) { if err == io.EOF { break } else if err != nil { - fmt.Println("failed to read:", err) - os.Exit(1) + exitWithError(fmt.Errorf("failed to read: %s", err)) } h := &bgp.MRTHeader{} err = h.DecodeFromBytes(buf) if err != nil { - fmt.Println("failed to parse") - os.Exit(1) + exitWithError(fmt.Errorf("failed to parse")) } buf = make([]byte, h.Len) _, err = buffer.Read(buf) if err != nil { - fmt.Println("failed to read") - os.Exit(1) + exitWithError(fmt.Errorf("failed to read")) } msg, err := bgp.ParseMRTBody(h, buf) if err != nil { - fmt.Println("failed to parse:", err) - os.Exit(1) + exitWithError(fmt.Errorf("failed to parse: %s", err)) } fmt.Println(msg) @@ -131,8 +127,7 @@ func dumpRib(r string, remoteIP net.IP, args []string) error { stream, err := client.GetMrt(context.Background(), arg) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } var fileformat string @@ -150,8 +145,7 @@ func dumpRib(r string, remoteIP net.IP, args []string) error { if err == io.EOF { break } else if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } if globalOpts.Debug { @@ -180,8 +174,7 @@ func dumpRib(r string, remoteIP net.IP, args []string) error { err = ioutil.WriteFile(filename, s.Data, 0600) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } fmt.Println("mrt dump:", filepath.Clean(filename)) @@ -218,28 +211,24 @@ func injectMrt(r string, filename string, count int, skip int) error { if err == io.EOF { break } else if err != nil { - fmt.Println("failed to read:", err) - os.Exit(1) + exitWithError(fmt.Errorf("failed to read: %s", err)) } h := &bgp.MRTHeader{} err = h.DecodeFromBytes(buf) if err != nil { - fmt.Println("failed to parse") - os.Exit(1) + exitWithError(fmt.Errorf("failed to parse")) } buf = make([]byte, h.Len) _, err = file.Read(buf) if err != nil { - fmt.Println("failed to read") - os.Exit(1) + exitWithError(fmt.Errorf("failed to read")) } msg, err := bgp.ParseMRTBody(h, buf) if err != nil { - fmt.Println("failed to parse:", err) - os.Exit(1) + exitWithError(fmt.Errorf("failed to parse: %s", err)) } if globalOpts.Debug { @@ -258,13 +247,11 @@ func injectMrt(r string, filename string, count int, skip int) error { case bgp.RIB_IPV6_UNICAST: rf = bgp.RF_IPv6_UC default: - fmt.Println("unsupported subType:", subType) - os.Exit(1) + exitWithError(fmt.Errorf("unsupported subType: %s", subType)) } if peers == nil { - fmt.Println("not found PEER_INDEX_TABLE") - os.Exit(1) + exitWithError(fmt.Errorf("not found PEER_INDEX_TABLE")) } rib := msg.Body.(*bgp.Rib) @@ -274,8 +261,7 @@ func injectMrt(r string, filename string, count int, skip int) error { for _, e := range rib.Entries { if len(peers) < int(e.PeerIndex) { - fmt.Printf("invalid peer index: %d (PEER_INDEX_TABLE has only %d peers)\n", e.PeerIndex, len(peers)) - os.Exit(1) + exitWithError(fmt.Errorf("invalid peer index: %d (PEER_INDEX_TABLE has only %d peers)\n", e.PeerIndex, len(peers))) } path := &api.Path{ @@ -346,8 +332,7 @@ func NewMrtCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := dumpRib(CMD_GLOBAL, net.IP{}, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -356,18 +341,15 @@ func NewMrtCmd() *cobra.Command { Use: CMD_NEIGHBOR, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { - fmt.Println("usage: gobgp mrt dump neighbor <neighbor address> [<interval>]") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt dump neighbor <neighbor address> [<interval>]")) } remoteIP := net.ParseIP(args[0]) if remoteIP == nil { - fmt.Println("invalid ip address:", args[0]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[0])) } err := dumpRib(CMD_LOCAL, remoteIP, args[1:]) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -389,8 +371,7 @@ func NewMrtCmd() *cobra.Command { Use: CMD_GLOBAL, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { - fmt.Println("usage: gobgp mrt inject global <filename> [<count> [<skip>]]") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt inject global <filename> [<count> [<skip>]]")) } filename := args[0] count := -1 @@ -399,21 +380,18 @@ func NewMrtCmd() *cobra.Command { var err error count, err = strconv.Atoi(args[1]) if err != nil { - fmt.Println("invalid count value:", args[1]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid count value: %s", args[1])) } if len(args) > 2 { skip, err = strconv.Atoi(args[2]) if err != nil { - fmt.Println("invalid skip value:", args[2]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid skip value: %s", args[2])) } } } err := injectMrt(CMD_GLOBAL, filename, count, skip) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -435,8 +413,7 @@ func NewMrtCmd() *cobra.Command { Use: CMD_ENABLE, Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - fmt.Println("usage: gobgp mrt update enable <filename>") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt update enable <filename>")) } modMrt(api.Operation_ADD, args[0]) }, @@ -446,8 +423,7 @@ func NewMrtCmd() *cobra.Command { Use: CMD_DISABLE, Run: func(cmd *cobra.Command, args []string) { if len(args) != 0 { - fmt.Println("usage: gobgp mrt update disable") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt update disable")) } modMrt(api.Operation_DEL, "") }, @@ -457,8 +433,7 @@ func NewMrtCmd() *cobra.Command { Use: CMD_ROTATE, Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - fmt.Println("usage: gobgp mrt update rotate <filename>") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt update rotate <filename>")) } modMrt(api.Operation_REPLACE, args[0]) }, @@ -468,8 +443,7 @@ func NewMrtCmd() *cobra.Command { Use: CMD_RESET, Run: func(cmd *cobra.Command, args []string) { if len(args) > 0 { - fmt.Println("usage: gobgp mrt update reset") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp mrt update reset")) } modMrt(api.Operation_REPLACE, "") }, diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 1eeba541..2e1cc1e5 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -25,7 +25,6 @@ import ( "golang.org/x/net/context" "io" "net" - "os" "sort" "strings" ) @@ -740,15 +739,13 @@ func NewNeighborCmd() *cobra.Command { if addr == "" { remoteIP := net.ParseIP(args[len(args)-1]) if remoteIP == nil { - fmt.Println("invalid ip address:", args[len(args)-1]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[len(args)-1])) } addr = remoteIP.String() } err := f(cmd.Use, addr, args[:len(args)-1]) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -761,14 +758,12 @@ func NewNeighborCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { remoteIP := net.ParseIP(args[0]) if remoteIP == nil { - fmt.Println("invalid ip address:", args[0]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[0])) } for _, v := range []string{CMD_IN, CMD_IMPORT, CMD_EXPORT} { if err := showNeighborPolicy(remoteIP, v, 4); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } } }, @@ -786,8 +781,7 @@ func NewNeighborCmd() *cobra.Command { err = showNeighborPolicy(remoteIP, cmd.Use, 0) } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, @@ -800,13 +794,11 @@ func NewNeighborCmd() *cobra.Command { remoteIP := net.ParseIP(args[len(args)-1]) args = args[:len(args)-1] if remoteIP == nil { - fmt.Println("invalid ip address:", args[len(args)-1]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[len(args)-1])) } err := modNeighborPolicy(remoteIP, cmd.Use, subcmd.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -838,8 +830,7 @@ func NewNeighborCmd() *cobra.Command { err = neighborCmdImpl.Execute() } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index c2c1ec70..c8dcb2e8 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -26,7 +26,6 @@ import ( "golang.org/x/net/context" "io" "net" - "os" "regexp" "sort" "strconv" @@ -912,8 +911,7 @@ func NewPolicyCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := showPolicy(args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -923,8 +921,7 @@ func NewPolicyCmd() *cobra.Command { Use: v, Run: func(cmd *cobra.Command, args []string) { if err := showDefinedSet(cmd.Use, args); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -933,8 +930,7 @@ func NewPolicyCmd() *cobra.Command { Use: w, Run: func(c *cobra.Command, args []string) { if err := modDefinedSet(cmd.Use, c.Use, args); err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -961,8 +957,7 @@ func NewPolicyCmd() *cobra.Command { err = modAction(name, cmd.Use, args) } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -983,8 +978,7 @@ func NewPolicyCmd() *cobra.Command { err = stmtCmdImpl.Execute() } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -994,8 +988,7 @@ func NewPolicyCmd() *cobra.Command { Run: func(c *cobra.Command, args []string) { err := modStatement(c.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -1009,8 +1002,7 @@ func NewPolicyCmd() *cobra.Command { Run: func(c *cobra.Command, args []string) { err := modPolicy(c.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } diff --git a/gobgp/cmd/rpki.go b/gobgp/cmd/rpki.go index ae56bc69..a0844461 100644 --- a/gobgp/cmd/rpki.go +++ b/gobgp/cmd/rpki.go @@ -23,7 +23,6 @@ import ( "golang.org/x/net/context" "io" "net" - "os" "strconv" "time" ) @@ -90,8 +89,7 @@ func showRPKIServer(args []string) error { func showRPKITable(args []string) error { family, err := checkAddressFamily(bgp.RouteFamily(0)) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } arg := &api.Arguments{ Family: uint32(family), @@ -152,13 +150,11 @@ func NewRPKICmd() *cobra.Command { showRPKIServer(args) return } else if len(args) != 2 { - fmt.Println("usage: gobgp rpki server <ip address> [reset|softreset|enable]") - os.Exit(1) + exitWithError(fmt.Errorf("usage: gobgp rpki server <ip address> [reset|softreset|enable]")) } addr := net.ParseIP(args[0]) if addr == nil { - fmt.Println("invalid ip address:", args[0]) - os.Exit(1) + exitWithError(fmt.Errorf("invalid ip address: %s", args[0])) } var op api.Operation switch args[1] { @@ -171,12 +167,11 @@ func NewRPKICmd() *cobra.Command { case "enable": op = api.Operation_ENABLE default: - fmt.Println("unknown operation:", args[1]) - os.Exit(1) + exitWithError(fmt.Errorf("unknown operation: %s", args[1])) } err := modRPKI(op, addr.String()) if err != nil { - fmt.Println(err) + exitWithError(err) } }, } diff --git a/gobgp/cmd/vrf.go b/gobgp/cmd/vrf.go index d36dc04f..ad65214d 100644 --- a/gobgp/cmd/vrf.go +++ b/gobgp/cmd/vrf.go @@ -23,7 +23,6 @@ import ( "github.com/spf13/cobra" "golang.org/x/net/context" "io" - "os" "sort" "strings" ) @@ -185,8 +184,7 @@ func NewVrfCmd() *cobra.Command { err = fmt.Errorf("usage: gobgp vrf <vrf-name> rib") } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -197,8 +195,7 @@ func NewVrfCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modPath(api.Resource_VRF, args[len(args)-1], cmd.Use, args[:len(args)-1]) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -221,8 +218,7 @@ func NewVrfCmd() *cobra.Command { err = vrfCmdImpl.Execute() } if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } @@ -233,8 +229,7 @@ func NewVrfCmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { err := modVrf(cmd.Use, args) if err != nil { - fmt.Println(err) - os.Exit(1) + exitWithError(err) } }, } |