mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
commands: Check for option name collisions
This commit is contained in:

committed by
Juan Batiz-Benet

parent
dd682963a2
commit
5b18844c06
@ -19,7 +19,13 @@ func (c *Command) Register(id string, sub *Command) error {
|
|||||||
c.subcommands = make(map[string]*Command)
|
c.subcommands = make(map[string]*Command)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check for duplicate option names
|
// check for duplicate option names (only checks downwards)
|
||||||
|
names := make(map[string]bool)
|
||||||
|
c.checkOptions(names)
|
||||||
|
err := sub.checkOptions(names)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := c.subcommands[id]; ok {
|
if _, ok := c.subcommands[id]; ok {
|
||||||
return fmt.Errorf("There is already a subcommand registered with id '%s'", id)
|
return fmt.Errorf("There is already a subcommand registered with id '%s'", id)
|
||||||
@ -83,3 +89,23 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
|
|||||||
func (c *Command) Sub(id string) *Command {
|
func (c *Command) Sub(id string) *Command {
|
||||||
return c.subcommands[id]
|
return c.subcommands[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) checkOptions(names map[string]bool) error {
|
||||||
|
for _, opt := range c.Options {
|
||||||
|
for _, name := range opt.Names {
|
||||||
|
if _, ok := names[name]; ok {
|
||||||
|
return fmt.Errorf("Multiple options are using the same name ('%s')", name)
|
||||||
|
}
|
||||||
|
names[name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cmd := range c.subcommands {
|
||||||
|
err := cmd.checkOptions(names)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user