diff options
author | Satoshi Fujimoto <satoshi.fujimoto7@gmail.com> | 2018-03-15 15:01:19 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-03-28 10:07:59 +0900 |
commit | 107bfac9f44467aba39d3543ea2e57c7eecb995d (patch) | |
tree | e250845989f5b2456ce36731e4ca352e8aa25458 | |
parent | cab913cb45d09a4151263bb08ac7d671fdb2cf14 (diff) |
cmd/common: Specify number of expected arguments
In CLI operation, currently, unexpected argument names
(such as 'aspath' for 'gobgp neighbor add')
may pass the validations and return no errors.
This commit prevents accepting those argument names
by specifying the number of expected arguments for each argument names.
Signed-off-by: Satoshi Fujimoto <satoshi.fujimoto7@gmail.com>
-rw-r--r-- | gobgp/cmd/common.go | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/gobgp/cmd/common.go b/gobgp/cmd/common.go index 9c3f1732..99229008 100644 --- a/gobgp/cmd/common.go +++ b/gobgp/cmd/common.go @@ -82,6 +82,12 @@ const ( CMD_VALIDATION = "validation" ) +const ( + PARAM_FLAG = iota + PARAM_SINGLE + PARAM_LIST +) + var subOpts struct { AddressFamily string `short:"a" long:"address-family" description:"specifying an address family"` } @@ -154,11 +160,11 @@ func cidr2prefix(cidr string) string { return buffer.String()[:ones] } -func extractReserved(args, keys []string) map[string][]string { +func extractReserved(args []string, keys map[string]int) (map[string][]string, error) { m := make(map[string][]string, len(keys)) var k string isReserved := func(s string) bool { - for _, r := range keys { + for r := range keys { if s == r { return true } @@ -173,7 +179,26 @@ func extractReserved(args, keys []string) map[string][]string { m[k] = append(m[k], arg) } } - return m + for k, v := range m { + if k == "" { + continue + } + switch keys[k] { + case PARAM_FLAG: + if len(v) != 0 { + return nil, fmt.Errorf("%s should not have arguments", k) + } + case PARAM_SINGLE: + if len(v) != 1 { + return nil, fmt.Errorf("%s should have one argument", k) + } + case PARAM_LIST: + if len(v) == 0 { + return nil, fmt.Errorf("%s should have one or more arguments", k) + } + } + } + return m, nil } type neighbors []*config.Neighbor |