diff options
author | Satoshi Fujimoto <satoshi.fujimoto7@gmail.com> | 2018-03-15 16:44:05 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-03-28 10:07:59 +0900 |
commit | 0fe2bf4656f3b0a4184a21d2db3768e3bb19a20b (patch) | |
tree | 4ea8ffac1f2a86b933ed5b9a023d1e24ab120117 | |
parent | 107bfac9f44467aba39d3543ea2e57c7eecb995d (diff) |
cmd/*: Pass number of expected args to extractReserved()
Signed-off-by: Satoshi Fujimoto <satoshi.fujimoto7@gmail.com>
-rw-r--r-- | gobgp/cmd/common_test.go | 9 | ||||
-rw-r--r-- | gobgp/cmd/global.go | 81 | ||||
-rw-r--r-- | gobgp/cmd/neighbor.go | 13 | ||||
-rw-r--r-- | gobgp/cmd/vrf.go | 7 |
4 files changed, 93 insertions, 17 deletions
diff --git a/gobgp/cmd/common_test.go b/gobgp/cmd/common_test.go index ed4bfe4a..c8d351f3 100644 --- a/gobgp/cmd/common_test.go +++ b/gobgp/cmd/common_test.go @@ -25,8 +25,13 @@ import ( func Test_ExtractReserved(t *testing.T) { assert := assert.New(t) args := strings.Split("10 rt 100:100 med 10 nexthop 10.0.0.1 aigp metric 10 local-pref 100", " ") - keys := []string{"rt", "med", "nexthop", "aigp", "local-pref"} - m := extractReserved(args, keys) + keys := map[string]int{ + "rt": PARAM_LIST, + "med": PARAM_SINGLE, + "nexthop": PARAM_SINGLE, + "aigp": PARAM_LIST, + "local-pref": PARAM_SINGLE} + m, _ := extractReserved(args, keys) fmt.Println(m) assert.True(len(m["rt"]) == 1) assert.True(len(m["med"]) == 1) diff --git a/gobgp/cmd/global.go b/gobgp/cmd/global.go index c84b0910..cbdd44c8 100644 --- a/gobgp/cmd/global.go +++ b/gobgp/cmd/global.go @@ -335,7 +335,14 @@ func ParseFlowSpecArgs(rf bgp.RouteFamily, args []string) (bgp.AddrPrefixInterfa if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"match", "then", "rd", "rt"}) + m, err := extractReserved(args, map[string]int{ + "match": PARAM_LIST, + "then": PARAM_LIST, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST}) + if err != nil { + return nil, nil, err + } if len(m["match"]) == 0 { return nil, nil, fmt.Errorf("specify filtering rules with keyword 'match'") } @@ -395,7 +402,17 @@ func ParseEvpnEthernetAutoDiscoveryArgs(args []string) (bgp.AddrPrefixInterface, if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"esi", "etag", "label", "rd", "rt", "encap", "esi-label"}) + m, err := extractReserved(args, map[string]int{ + "esi": PARAM_LIST, + "etag": PARAM_SINGLE, + "label": PARAM_SINGLE, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "encap": PARAM_SINGLE, + "esi-label": PARAM_SINGLE}) + if err != nil { + return nil, nil, err + } for _, f := range []string{"esi", "etag", "label", "rd"} { for len(m[f]) == 0 { return nil, nil, fmt.Errorf("specify %s", f) @@ -457,7 +474,16 @@ func ParseEvpnMacAdvArgs(args []string) (bgp.AddrPrefixInterface, []string, erro if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"esi", "etag", "label", "rd", "rt", "encap"}) + m, err := extractReserved(args, map[string]int{ + "esi": PARAM_LIST, + "etag": PARAM_SINGLE, + "label": PARAM_SINGLE, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "encap": PARAM_SINGLE}) + if err != nil { + return nil, nil, err + } if len(m[""]) < 2 { return nil, nil, fmt.Errorf("specify mac and ip address") } @@ -563,7 +589,14 @@ func ParseEvpnMulticastArgs(args []string) (bgp.AddrPrefixInterface, []string, e if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"etag", "rd", "rt", "encap"}) + m, err := extractReserved(args, map[string]int{ + "etag": PARAM_SINGLE, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "encap": PARAM_SINGLE}) + if err != nil { + return nil, nil, err + } if len(m[""]) < 1 { return nil, nil, fmt.Errorf("specify ip address") } @@ -628,7 +661,14 @@ func ParseEvpnEthernetSegmentArgs(args []string) (bgp.AddrPrefixInterface, []str if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"esi", "rd", "rt", "encap"}) + m, err := extractReserved(args, map[string]int{ + "esi": PARAM_LIST, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "encap": PARAM_SINGLE}) + if err != nil { + return nil, nil, err + } if len(m[""]) < 1 { return nil, nil, fmt.Errorf("specify ip address") } @@ -685,7 +725,18 @@ func ParseEvpnIPPrefixArgs(args []string) (bgp.AddrPrefixInterface, []string, er if len(args) < req { return nil, nil, fmt.Errorf("%d args required at least, but got %d", req, len(args)) } - m := extractReserved(args, []string{"gw", "esi", "etag", "label", "rd", "rt", "encap", "router-mac"}) + m, err := extractReserved(args, map[string]int{ + "gw": PARAM_SINGLE, + "esi": PARAM_LIST, + "etag": PARAM_SINGLE, + "label": PARAM_SINGLE, + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "encap": PARAM_SINGLE, + "router-mac": PARAM_SINGLE}) + if err != nil { + return nil, nil, err + } if len(m[""]) < 1 { return nil, nil, fmt.Errorf("specify prefix") } @@ -1155,7 +1206,12 @@ func ParsePath(rf bgp.RouteFamily, args []string) (*table.Path, error) { case bgp.RF_FS_IPv4_UC, bgp.RF_FS_IPv4_VPN, bgp.RF_FS_IPv6_UC, bgp.RF_FS_IPv6_VPN, bgp.RF_FS_L2_VPN: nlri, extcomms, err = ParseFlowSpecArgs(rf, args) case bgp.RF_OPAQUE: - m := extractReserved(args, []string{"key", "value"}) + m, err := extractReserved(args, map[string]int{ + "key": PARAM_SINGLE, + "value": PARAM_SINGLE}) + if err != nil { + return nil, err + } if len(m["key"]) != 1 { return nil, fmt.Errorf("opaque nlri key missing") } @@ -1424,10 +1480,13 @@ func showGlobalConfig() error { } func modGlobalConfig(args []string) error { - m := extractReserved(args, []string{"as", "router-id", "listen-port", - "listen-addresses", "use-multipath"}) - - if len(m["as"]) != 1 || len(m["router-id"]) != 1 { + m, err := extractReserved(args, map[string]int{ + "as": PARAM_SINGLE, + "router-id": PARAM_SINGLE, + "listen-port": PARAM_SINGLE, + "listen-addresses": PARAM_LIST, + "use-multipath": PARAM_FLAG}) + if err != nil || len(m["as"]) != 1 || len(m["router-id"]) != 1 { return fmt.Errorf("usage: gobgp global as <VALUE> router-id <VALUE> [use-multipath] [listen-port <VALUE>] [listen-addresses <VALUE>...]") } asn, err := strconv.ParseUint(m["as"][0], 10, 32) diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 4a081035..7414c17c 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -973,13 +973,22 @@ func modNeighborPolicy(remoteIP, policyType, cmdType string, args []string) erro } func modNeighbor(cmdType string, args []string) error { - m := extractReserved(args, []string{"interface", "as", "family", "vrf", "route-reflector-client", "route-server-client", "allow-own-as", "remove-private-as", "replace-peer-as"}) + params := map[string]int{"interface": PARAM_SINGLE} usage := fmt.Sprintf("usage: gobgp neighbor %s [ <neighbor-address> | interface <neighbor-interface> ]", cmdType) if cmdType == CMD_ADD { + params["as"] = PARAM_SINGLE + params["family"] = PARAM_SINGLE + params["vrf"] = PARAM_SINGLE + params["route-reflector-client"] = PARAM_SINGLE + params["route-server-client"] = PARAM_FLAG + params["allow-own-as"] = PARAM_SINGLE + params["remove-private-as"] = PARAM_SINGLE + params["replace-peer-as"] = PARAM_FLAG usage += " as <VALUE> [ family <address-families-list> | vrf <vrf-name> | route-reflector-client [<cluster-id>] | route-server-client | allow-own-as <num> | remove-private-as (all|replace) | replace-peer-as ]" } - if (len(m[""]) != 1 && len(m["interface"]) != 1) || len(m["as"]) > 1 || len(m["family"]) > 1 || len(m["vrf"]) > 1 || len(m["route-reflector-client"]) > 1 || len(m["allow-own-as"]) > 1 || len(m["remove-private-as"]) > 1 { + m, err := extractReserved(args, params) + if err != nil || (len(m[""]) != 1 && len(m["interface"]) != 1) { return fmt.Errorf("%s", usage) } unnumbered := len(m["interface"]) > 0 diff --git a/gobgp/cmd/vrf.go b/gobgp/cmd/vrf.go index 5aebf903..32e73882 100644 --- a/gobgp/cmd/vrf.go +++ b/gobgp/cmd/vrf.go @@ -93,8 +93,11 @@ func modVrf(typ string, args []string) error { var err error switch typ { case CMD_ADD: - a := extractReserved(args, []string{"rd", "rt", "id"}) - if len(a[""]) != 1 || len(a["rd"]) != 1 || len(a["rt"]) < 2 || len(a["id"]) > 1 { + a, err := extractReserved(args, map[string]int{ + "rd": PARAM_SINGLE, + "rt": PARAM_LIST, + "id": PARAM_SINGLE}) + if err != nil || len(a[""]) != 1 || len(a["rd"]) != 1 || len(a["rt"]) < 2 { return fmt.Errorf("Usage: gobgp vrf add <vrf name> [ id <id> ] rd <rd> rt { import | export | both } <rt>...") } name := a[""][0] |