Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
bc6938dc
Commit
bc6938dc
authored
Nov 03, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Nov 04, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Cleaned up argument validation
parent
f6c38882
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
26 deletions
+49
-26
commands/command.go
commands/command.go
+49
-26
No files found.
commands/command.go
View file @
bc6938dc
...
...
@@ -140,40 +140,37 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
}
func
(
c
*
Command
)
CheckArguments
(
req
Request
)
error
{
var
argDef
Argument
args
:=
req
.
Arguments
()
argDefs
:=
c
.
Arguments
var
length
int
if
len
(
args
)
>
len
(
c
.
Arguments
)
{
length
=
len
(
args
)
}
else
{
length
=
len
(
c
.
Argument
s
)
// if we have more arg values provided than argument definitions,
// and the last arg definition is not variadic (or there are no definitions), return an error
notVariadic
:=
len
(
argDefs
)
==
0
||
!
argDefs
[
len
(
argDefs
)
-
1
]
.
Variadic
if
notVariadic
&&
len
(
args
)
>
len
(
argDefs
)
{
return
fmt
.
Errorf
(
"Expected %v arguments, got %v"
,
len
(
argDefs
),
len
(
arg
s
)
)
}
for
i
:=
0
;
i
<
length
;
i
++
{
var
arg
interface
{}
if
len
(
args
)
>
i
{
arg
=
args
[
i
]
}
// iterate over the arg definitions
for
i
,
argDef
:=
range
c
.
Arguments
{
if
i
<
len
(
c
.
Arguments
)
{
ar
gDef
=
c
.
Arguments
[
i
]
}
else
if
!
argDef
.
Variadic
{
return
fmt
.
Errorf
(
"Expected %v arguments, got %v"
,
len
(
c
.
Arguments
),
len
(
args
))
// the value for this argument definition. can be nil if it wasn't provided by the caller
v
ar
v
interface
{}
if
i
<
len
(
args
)
{
v
=
args
[
i
]
}
if
argDef
.
Required
&&
arg
==
nil
{
return
fmt
.
Errorf
(
"Argument '%s' is required"
,
argDef
.
Name
)
err
:=
checkArgValue
(
v
,
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
)
// any additional values are for the variadic arg definition
if
argDef
.
Variadic
&&
i
<
len
(
args
)
-
1
{
for
_
,
val
:=
range
args
[
i
+
1
:
]
{
err
:=
checkArgValue
(
val
,
argDef
)
if
err
!=
nil
{
return
err
}
}
}
}
...
...
@@ -185,3 +182,29 @@ func (c *Command) CheckArguments(req Request) error {
func
(
c
*
Command
)
Subcommand
(
id
string
)
*
Command
{
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment