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
7673ce6f
Commit
7673ce6f
authored
Oct 20, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt, lint, + vet commands/
parent
a9fa767b
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
130 additions
and
117 deletions
+130
-117
commands/cli/parse.go
commands/cli/parse.go
+52
-50
commands/cli/parse_test.go
commands/cli/parse_test.go
+37
-37
commands/command.go
commands/command.go
+8
-5
commands/option.go
commands/option.go
+3
-2
commands/response.go
commands/response.go
+28
-21
commands/response_test.go
commands/response_test.go
+2
-2
No files found.
commands/cli/parse.go
View file @
7673ce6f
...
...
@@ -7,6 +7,8 @@ import (
"github.com/jbenet/go-ipfs/commands"
)
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
func
Parse
(
input
[]
string
,
root
*
commands
.
Command
)
(
*
commands
.
Request
,
error
)
{
path
,
input
,
err
:=
parsePath
(
input
,
root
)
if
err
!=
nil
{
...
...
@@ -46,7 +48,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro
// returns the parsed options as strings, along with the CLI args
func
parseOptions
(
input
[]
string
)
(
map
[
string
]
interface
{},
[]
string
,
error
)
{
opts
:=
make
(
map
[
string
]
interface
{})
args
:=
make
(
[]
string
,
0
)
args
:=
[]
string
{}
for
i
:=
0
;
i
<
len
(
input
);
i
++
{
blob
:=
input
[
i
]
...
...
commands/cli/parse_test.go
View file @
7673ce6f
...
...
@@ -10,12 +10,12 @@ import (
func
TestOptionParsing
(
t
*
testing
.
T
)
{
cmd
:=
&
commands
.
Command
{
Options
:
[]
commands
.
Option
{
commands
.
Option
{
[]
string
{
"b"
},
commands
.
String
},
commands
.
Option
{
Names
:
[]
string
{
"b"
},
Type
:
commands
.
String
},
},
}
cmd
.
Register
(
"test"
,
&
commands
.
Command
{})
opts
,
input
,
err
:=
parseOptions
([]
string
{
"--beep"
,
"-boop=lol"
,
"test2"
,
"-c"
,
"beep"
,
"--foo=5"
})
opts
,
input
,
err
:=
parseOptions
([]
string
{
"--beep"
,
"-boop=lol"
,
"test2"
,
"-c"
,
"beep"
,
"--foo=5"
})
/*for k, v := range opts {
fmt.Printf("%s: %s\n", k, v)
}
...
...
@@ -30,12 +30,12 @@ func TestOptionParsing(t *testing.T) {
t
.
Error
(
"Returned input was different than expected: %v"
,
input
)
}
_
,
_
,
err
=
parseOptions
([]
string
{
"-beep=1"
,
"-boop=2"
,
"-beep=3"
})
_
,
_
,
err
=
parseOptions
([]
string
{
"-beep=1"
,
"-boop=2"
,
"-beep=3"
})
if
err
==
nil
{
t
.
Error
(
"Should have failed (duplicate option name)"
)
}
path
,
args
,
err
:=
parsePath
([]
string
{
"test"
,
"beep"
,
"boop"
},
cmd
)
path
,
args
,
err
:=
parsePath
([]
string
{
"test"
,
"beep"
,
"boop"
},
cmd
)
if
err
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
...
...
commands/command.go
View file @
7673ce6f
...
...
@@ -6,6 +6,7 @@ import (
"strings"
)
// Command is an object that defines a command.
type
Command
struct
{
Help
string
Options
[]
Option
...
...
@@ -13,7 +14,8 @@ type Command struct {
subcommands
map
[
string
]
*
Command
}
var
NotCallableError
=
errors
.
New
(
"This command can't be called directly. Try one of its subcommands."
)
// ErrNotCallable signals a command that cannot be called.
var
ErrNotCallable
=
errors
.
New
(
"This command can't be called directly. Try one of its subcommands."
)
// Register adds a subcommand
func
(
c
*
Command
)
Register
(
id
string
,
sub
*
Command
)
error
{
...
...
@@ -44,25 +46,25 @@ func (c *Command) Call(req *Request) *Response {
cmds
,
err
:=
c
.
Resolve
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
res
.
SetError
(
err
,
Err
Client
)
return
res
}
cmd
:=
cmds
[
len
(
cmds
)
-
1
]
if
cmd
.
f
==
nil
{
res
.
SetError
(
NotCallableErr
or
,
Client
)
res
.
SetError
(
Err
NotCallable
,
ErrClient
)
return
res
}
options
,
err
:=
c
.
GetOptions
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
res
.
SetError
(
err
,
Err
Client
)
return
res
}
err
=
req
.
convertOptions
(
options
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
res
.
SetError
(
err
,
Err
Client
)
return
res
}
...
...
@@ -91,6 +93,7 @@ func (c *Command) Resolve(path []string) ([]*Command, error) {
return
cmds
,
nil
}
// Get resolves and returns the Command addressed by path
func
(
c
*
Command
)
Get
(
path
[]
string
)
(
*
Command
,
error
)
{
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
...
...
commands/option.go
View file @
7673ce6f
...
...
@@ -2,6 +2,7 @@ package commands
import
"reflect"
// Types of Command options
const
(
Invalid
=
reflect
.
Invalid
Bool
=
reflect
.
Bool
...
...
@@ -22,11 +23,11 @@ type Option struct {
}
// options that are used by this package
var
globalOptions
[]
Option
=
[]
Option
{
var
globalOptions
=
[]
Option
{
Option
{[]
string
{
"enc"
,
"encoding"
},
String
},
}
// the above array of Options, wrapped in a Command
var
globalCommand
*
Command
=
&
Command
{
var
globalCommand
=
&
Command
{
Options
:
globalOptions
,
}
commands/response.go
View file @
7673ce6f
...
...
@@ -7,11 +7,13 @@ import (
"strings"
)
// ErrorType signfies a category of errors
type
ErrorType
uint
// ErrorTypes convey what category of error ocurred
const
(
Normal
ErrorType
=
iota
// general errors
Client
// error was caused by the client, (e.g. invalid CLI usage)
Err
Normal
ErrorType
=
iota
// general errors
Err
Client
// error was caused by the client, (e.g. invalid CLI usage)
// TODO: add more types of errors for better error-specific handling
)
...
...
@@ -21,37 +23,44 @@ type Error struct {
Code
ErrorType
}
func
(
e
*
Error
)
Error
()
string
{
return
fmt
.
Sprintf
(
"%d error: %s"
,
e
.
Code
,
e
.
Message
)
}
// EncodingType defines a supported encoding
type
EncodingType
string
// Supported EncodingType constants.
const
(
J
son
=
"json"
X
ml
=
"xml"
J
SON
=
"json"
X
ML
=
"xml"
// TODO: support more encoding types
)
// Marshaller is a function used by coding types.
// TODO this should just be a `coding.Codec`
type
Marshaller
func
(
v
interface
{})
([]
byte
,
error
)
var
marshallers
=
map
[
EncodingType
]
Marshaller
{
J
son
:
json
.
Marshal
,
X
ml
:
xml
.
Marshal
,
J
SON
:
json
.
Marshal
,
X
ML
:
xml
.
Marshal
,
}
// Response is the result of a command request. Handlers write to the response,
// setting Error or Value. Response is returned to the client.
type
Response
struct
{
req
*
Request
Error
error
ErrorType
ErrorType
Error
*
Error
Value
interface
{}
}
func
(
r
*
Response
)
SetError
(
err
error
,
errType
ErrorType
)
{
r
.
Error
=
err
r
.
ErrorType
=
errType
}
func
(
r
*
Response
)
FormatError
()
Error
{
return
Error
{
r
.
Error
.
Error
(),
r
.
ErrorType
}
// SetError updates the response Error.
func
(
r
*
Response
)
SetError
(
err
error
,
code
ErrorType
)
{
r
.
Error
=
&
Error
{
Message
:
err
.
Error
(),
Code
:
code
}
}
// Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaller (Codec).
func
(
r
*
Response
)
Marshal
()
([]
byte
,
error
)
{
if
r
.
Error
==
nil
&&
r
.
Value
==
nil
{
return
nil
,
fmt
.
Errorf
(
"No error or value set, there is nothing to marshal"
)
...
...
@@ -69,9 +78,7 @@ func (r *Response) Marshal() ([]byte, error) {
}
if
r
.
Error
!=
nil
{
err
:=
r
.
FormatError
()
return
marshaller
(
err
)
}
else
{
return
marshaller
(
r
.
Value
)
return
marshaller
(
r
.
Error
)
}
return
marshaller
(
r
.
Value
)
}
commands/response_test.go
View file @
7673ce6f
...
...
@@ -30,7 +30,7 @@ func TestMarshalling(t *testing.T) {
t
.
Error
(
"Should have failed (no encoding type specified in request)"
)
}
req
.
SetOption
(
"enc"
,
J
son
)
req
.
SetOption
(
"enc"
,
J
SON
)
req
.
convertOptions
(
options
)
bytes
,
err
:=
res
.
Marshal
()
...
...
@@ -42,7 +42,7 @@ func TestMarshalling(t *testing.T) {
t
.
Error
(
"Incorrect JSON output"
)
}
res
.
SetError
(
fmt
.
Errorf
(
"You broke something!"
),
Client
)
res
.
SetError
(
fmt
.
Errorf
(
"You broke something!"
),
Err
Client
)
bytes
,
err
=
res
.
Marshal
()
if
err
!=
nil
{
t
.
Error
(
"Should have passed"
)
...
...
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