mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-26 15:42:21 +08:00
commands: Cleaned up argument validation
This commit is contained in:

committed by
Juan Batiz-Benet

parent
f6c38882f0
commit
bc6938dc08
@ -140,40 +140,37 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) CheckArguments(req Request) error {
|
func (c *Command) CheckArguments(req Request) error {
|
||||||
var argDef Argument
|
|
||||||
args := req.Arguments()
|
args := req.Arguments()
|
||||||
|
argDefs := c.Arguments
|
||||||
|
|
||||||
var length int
|
// if we have more arg values provided than argument definitions,
|
||||||
if len(args) > len(c.Arguments) {
|
// and the last arg definition is not variadic (or there are no definitions), return an error
|
||||||
length = len(args)
|
notVariadic := len(argDefs) == 0 || !argDefs[len(argDefs)-1].Variadic
|
||||||
} else {
|
if notVariadic && len(args) > len(argDefs) {
|
||||||
length = len(c.Arguments)
|
return fmt.Errorf("Expected %v arguments, got %v", len(argDefs), len(args))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < length; i++ {
|
// iterate over the arg definitions
|
||||||
var arg interface{}
|
for i, argDef := range c.Arguments {
|
||||||
if len(args) > i {
|
|
||||||
arg = args[i]
|
// the value for this argument definition. can be nil if it wasn't provided by the caller
|
||||||
|
var v interface{}
|
||||||
|
if i < len(args) {
|
||||||
|
v = args[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
if i < len(c.Arguments) {
|
err := checkArgValue(v, argDef)
|
||||||
argDef = c.Arguments[i]
|
if err != nil {
|
||||||
} else if !argDef.Variadic {
|
return err
|
||||||
return fmt.Errorf("Expected %v arguments, got %v", len(c.Arguments), len(args))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if argDef.Required && arg == nil {
|
// any additional values are for the variadic arg definition
|
||||||
return fmt.Errorf("Argument '%s' is required", argDef.Name)
|
if argDef.Variadic && i < len(args)-1 {
|
||||||
|
for _, val := range args[i+1:] {
|
||||||
|
err := checkArgValue(val, argDef)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if argDef.Type == ArgFile {
|
|
||||||
_, ok := arg.(io.Reader)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Argument '%s' isn't valid", argDef.Name)
|
|
||||||
}
|
|
||||||
} else if argDef.Type == ArgString {
|
|
||||||
_, ok := arg.(string)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Argument '%s' must be a string", argDef.Name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,3 +182,29 @@ func (c *Command) CheckArguments(req Request) error {
|
|||||||
func (c *Command) Subcommand(id string) *Command {
|
func (c *Command) Subcommand(id string) *Command {
|
||||||
return c.Subcommands[id]
|
return c.Subcommands[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkArgValue returns an error if a given arg value is not valid for the given Argument
|
||||||
|
func checkArgValue(v interface{}, def Argument) error {
|
||||||
|
if v == nil {
|
||||||
|
if def.Required {
|
||||||
|
return fmt.Errorf("Argument '%s' is required", def.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if def.Type == ArgFile {
|
||||||
|
_, ok := v.(io.Reader)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Argument '%s' isn't valid", def.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if def.Type == ArgString {
|
||||||
|
_, ok := v.(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Argument '%s' must be a string", def.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user