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-dms3
Commits
f95476c1
Commit
f95476c1
authored
10 years ago
by
Matt Bell
Committed by
Juan Batiz-Benet
10 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Allow overriding marshaller for any encoding type
parent
0afd3391
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
54 additions
and
42 deletions
+54
-42
cmd/ipfs2/main.go
cmd/ipfs2/main.go
+1
-1
core/commands2/add.go
core/commands2/add.go
+13
-11
core/commands2/commands.go
core/commands2/commands.go
+6
-4
core/commands2/log.go
core/commands2/log.go
+4
-2
core/commands2/ls.go
core/commands2/ls.go
+16
-14
core/commands2/publish.go
core/commands2/publish.go
+6
-4
core/commands2/root.go
core/commands2/root.go
+8
-6
No files found.
cmd/ipfs2/main.go
View file @
f95476c1
...
...
@@ -58,7 +58,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command) {
ctx
.
Config
=
conf
if
_
,
found
:=
options
.
Option
(
"encoding"
);
!
found
{
if
req
.
Command
()
.
Format
!=
nil
{
if
req
.
Command
()
.
Marshallers
!=
nil
&&
req
.
Command
()
.
Marshallers
[
cmds
.
Text
]
!=
nil
{
req
.
SetOption
(
"encoding"
,
cmds
.
Text
)
}
else
{
req
.
SetOption
(
"encoding"
,
cmds
.
JSON
)
...
...
This diff is collapsed.
Click to expand it.
core/commands2/add.go
View file @
f95476c1
...
...
@@ -55,18 +55,20 @@ var addCmd = &cmds.Command{
res
.
SetOutput
(
&
AddOutput
{
added
})
},
Format
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
AddOutput
)
.
Added
if
len
(
v
)
==
1
{
s
:=
fmt
.
Sprintf
(
"Added object: %s
\n
"
,
v
[
0
]
.
Hash
)
return
[]
byte
(
s
),
nil
}
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
AddOutput
)
.
Added
if
len
(
v
)
==
1
{
s
:=
fmt
.
Sprintf
(
"Added object: %s
\n
"
,
v
[
0
]
.
Hash
)
return
[]
byte
(
s
),
nil
}
s
:=
fmt
.
Sprintf
(
"Added %v objects:
\n
"
,
len
(
v
))
for
_
,
obj
:=
range
v
{
s
+=
fmt
.
Sprintf
(
"- %s
\n
"
,
obj
.
Hash
)
}
return
[]
byte
(
s
),
nil
s
:=
fmt
.
Sprintf
(
"Added %v objects:
\n
"
,
len
(
v
))
for
_
,
obj
:=
range
v
{
s
+=
fmt
.
Sprintf
(
"- %s
\n
"
,
obj
.
Hash
)
}
return
[]
byte
(
s
),
nil
},
},
Type
:
&
AddOutput
{},
}
...
...
This diff is collapsed.
Click to expand it.
core/commands2/commands.go
View file @
f95476c1
...
...
@@ -18,10 +18,12 @@ var commandsCmd = &cmds.Command{
root
:=
outputCommand
(
"ipfs"
,
Root
)
res
.
SetOutput
(
&
root
)
},
Format
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
Command
)
s
:=
formatCommand
(
v
,
0
)
return
[]
byte
(
s
),
nil
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
Command
)
s
:=
formatCommand
(
v
,
0
)
return
[]
byte
(
s
),
nil
},
},
Type
:
&
Command
{},
}
...
...
This diff is collapsed.
Click to expand it.
core/commands2/log.go
View file @
f95476c1
...
...
@@ -23,6 +23,8 @@ var logCmd = &cmds.Command{
s
:=
fmt
.
Sprintf
(
"Changed log level of '%s' to '%s'"
,
args
[
0
],
args
[
1
])
res
.
SetOutput
(
&
MessageOutput
{
s
})
},
Format
:
MessageMarshaller
,
Type
:
&
MessageOutput
{},
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
MessageTextMarshaller
,
},
Type
:
&
MessageOutput
{},
}
This diff is collapsed.
Click to expand it.
core/commands2/ls.go
View file @
f95476c1
...
...
@@ -52,25 +52,27 @@ var lsCmd = &cmds.Command{
res
.
SetOutput
(
&
LsOutput
{
output
})
},
Format
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
s
:=
""
output
:=
res
.
Output
()
.
(
*
LsOutput
)
.
Objects
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
s
:=
""
output
:=
res
.
Output
()
.
(
*
LsOutput
)
.
Objects
for
_
,
object
:=
range
output
{
if
len
(
output
)
>
1
{
s
+=
fmt
.
Sprintf
(
"%s:
\n
"
,
object
.
Hash
)
}
for
_
,
object
:=
range
output
{
if
len
(
output
)
>
1
{
s
+=
fmt
.
Sprintf
(
"%s:
\n
"
,
object
.
Hash
)
}
for
_
,
link
:=
range
object
.
Links
{
s
+=
fmt
.
Sprintf
(
"-> %s %s (%v bytes)
\n
"
,
link
.
Name
,
link
.
Hash
,
link
.
Size
)
}
for
_
,
link
:=
range
object
.
Links
{
s
+=
fmt
.
Sprintf
(
"-> %s %s (%v bytes)
\n
"
,
link
.
Name
,
link
.
Hash
,
link
.
Size
)
}
if
len
(
output
)
>
1
{
s
+=
"
\n
"
if
len
(
output
)
>
1
{
s
+=
"
\n
"
}
}
}
return
[]
byte
(
s
),
nil
return
[]
byte
(
s
),
nil
},
},
Type
:
&
LsOutput
{},
}
This diff is collapsed.
Click to expand it.
core/commands2/publish.go
View file @
f95476c1
...
...
@@ -61,10 +61,12 @@ var publishCmd = &cmds.Command{
Value
:
ref
,
})
},
Format
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
PublishOutput
)
s
:=
fmt
.
Sprintf
(
"Published name %s to %s
\n
"
,
v
.
Name
,
v
.
Value
)
return
[]
byte
(
s
),
nil
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
PublishOutput
)
s
:=
fmt
.
Sprintf
(
"Published name %s to %s
\n
"
,
v
.
Name
,
v
.
Value
)
return
[]
byte
(
s
),
nil
},
},
Type
:
&
PublishOutput
{},
}
This diff is collapsed.
Click to expand it.
core/commands2/root.go
View file @
f95476c1
...
...
@@ -71,11 +71,13 @@ var rootSubcommands = map[string]*cmds.Command{
log
.
Info
(
"beep"
)
res
.
SetOutput
(
v
)
},
Format
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
TestOutput
)
s
:=
fmt
.
Sprintf
(
"Foo: %s
\n
"
,
v
.
Foo
)
s
+=
fmt
.
Sprintf
(
"Bar: %v
\n
"
,
v
.
Bar
)
return
[]
byte
(
s
),
nil
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
TestOutput
)
s
:=
fmt
.
Sprintf
(
"Foo: %s
\n
"
,
v
.
Foo
)
s
+=
fmt
.
Sprintf
(
"Bar: %v
\n
"
,
v
.
Bar
)
return
[]
byte
(
s
),
nil
},
},
Type
:
&
TestOutput
{},
},
...
...
@@ -120,6 +122,6 @@ type MessageOutput struct {
Message
string
}
func
MessageMarshaller
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
func
Message
Text
Marshaller
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
return
[]
byte
(
res
.
Output
()
.
(
*
MessageOutput
)
.
Message
),
nil
}
This diff is collapsed.
Click to expand it.
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