Make rule parsing more robust
- check args length before popping - ensure rule has name
This commit is contained in:
@ -176,16 +176,15 @@ func (c *Config) parseUnknownFlag(option string, arg flags.SplitArgument, args [
|
|||||||
// Parse rules in the format "rule.<name>.<param>"
|
// Parse rules in the format "rule.<name>.<param>"
|
||||||
parts := strings.Split(option, ".")
|
parts := strings.Split(option, ".")
|
||||||
if len(parts) == 3 && parts[0] == "rule" {
|
if len(parts) == 3 && parts[0] == "rule" {
|
||||||
// Get or create rule
|
// Ensure there is a name
|
||||||
rule, ok := c.Rules[parts[1]]
|
name := parts[1]
|
||||||
if !ok {
|
if len(name) == 0 {
|
||||||
rule = NewRule()
|
return args, errors.New("route name is required")
|
||||||
c.Rules[parts[1]] = rule
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get value, or pop the next arg
|
// Get value, or pop the next arg
|
||||||
val, ok := arg.Value()
|
val, ok := arg.Value()
|
||||||
if !ok {
|
if !ok && len(args) > 1 {
|
||||||
val = args[0]
|
val = args[0]
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
@ -204,6 +203,13 @@ func (c *Config) parseUnknownFlag(option string, arg flags.SplitArgument, args [
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get or create rule
|
||||||
|
rule, ok := c.Rules[name]
|
||||||
|
if !ok {
|
||||||
|
rule = NewRule()
|
||||||
|
c.Rules[name] = rule
|
||||||
|
}
|
||||||
|
|
||||||
// Add param value to rule
|
// Add param value to rule
|
||||||
switch parts[2] {
|
switch parts[2] {
|
||||||
case "action":
|
case "action":
|
||||||
|
@ -98,6 +98,29 @@ func TestConfigParseUnknownFlags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigParseRuleError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// Rule without name
|
||||||
|
_, err := NewConfig([]string{
|
||||||
|
"--rule..action=auth",
|
||||||
|
})
|
||||||
|
if assert.Error(err) {
|
||||||
|
assert.Equal("route name is required", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rule without value
|
||||||
|
c, err := NewConfig([]string{
|
||||||
|
"--rule.one.action=",
|
||||||
|
})
|
||||||
|
if assert.Error(err) {
|
||||||
|
assert.Equal("route param value is required", err.Error())
|
||||||
|
}
|
||||||
|
// Check rules
|
||||||
|
assert.Equal(map[string]*Rule{}, c.Rules)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestConfigFlagBackwardsCompatability(t *testing.T) {
|
func TestConfigFlagBackwardsCompatability(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
c, err := NewConfig([]string{
|
c, err := NewConfig([]string{
|
||||||
|
Reference in New Issue
Block a user