mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-21 19:50:56 +08:00
cmd2: Marshaller -> Marshaler (see golang/encoding)
Also: - map[cmds.EncodingType]cmds.Marshaller -> MarshalMap cc @mappum @maybebtc
This commit is contained in:
@ -177,7 +177,7 @@ func (i *cmdInvocation) Parse(args []string) error {
|
|||||||
// if no encoding was specified by user, default to plaintext encoding
|
// if no encoding was specified by user, default to plaintext encoding
|
||||||
// (if command doesn't support plaintext, use JSON instead)
|
// (if command doesn't support plaintext, use JSON instead)
|
||||||
if !i.req.Option("encoding").Found() {
|
if !i.req.Option("encoding").Found() {
|
||||||
if i.req.Command().Marshallers != nil && i.req.Command().Marshallers[cmds.Text] != nil {
|
if i.req.Command().Marshalers != nil && i.req.Command().Marshalers[cmds.Text] != nil {
|
||||||
i.req.SetOption("encoding", cmds.Text)
|
i.req.SetOption("encoding", cmds.Text)
|
||||||
} else {
|
} else {
|
||||||
i.req.SetOption("encoding", cmds.JSON)
|
i.req.SetOption("encoding", cmds.JSON)
|
||||||
|
@ -16,9 +16,13 @@ var log = u.Logger("command")
|
|||||||
// It reads from the Request, and writes results to the Response.
|
// It reads from the Request, and writes results to the Response.
|
||||||
type Function func(Request) (interface{}, error)
|
type Function func(Request) (interface{}, error)
|
||||||
|
|
||||||
// Marshaller is a function that takes in a Response, and returns a marshalled []byte
|
// Marshaler is a function that takes in a Response, and returns a marshalled []byte
|
||||||
// (or an error on failure)
|
// (or an error on failure)
|
||||||
type Marshaller func(Response) ([]byte, error)
|
type Marshaler func(Response) ([]byte, error)
|
||||||
|
|
||||||
|
// MarshalerMap is a map of Marshaler functions, keyed by EncodingType
|
||||||
|
// (or an error on failure)
|
||||||
|
type MarshalerMap map[EncodingType]Marshaler
|
||||||
|
|
||||||
// HelpText is a set of strings used to generate command help text. The help
|
// HelpText is a set of strings used to generate command help text. The help
|
||||||
// text follows formats similar to man pages, but not exactly the same.
|
// text follows formats similar to man pages, but not exactly the same.
|
||||||
@ -48,7 +52,7 @@ type Command struct {
|
|||||||
Options []Option
|
Options []Option
|
||||||
Arguments []Argument
|
Arguments []Argument
|
||||||
Run Function
|
Run Function
|
||||||
Marshallers map[EncodingType]Marshaller
|
Marshalers map[EncodingType]Marshaler
|
||||||
Helptext HelpText
|
Helptext HelpText
|
||||||
|
|
||||||
// Type describes the type of the output of the Command's Run Function.
|
// Type describes the type of the output of the Command's Run Function.
|
||||||
|
@ -40,7 +40,7 @@ const (
|
|||||||
// TODO: support more encoding types
|
// TODO: support more encoding types
|
||||||
)
|
)
|
||||||
|
|
||||||
var marshallers = map[EncodingType]Marshaller{
|
var marshallers = map[EncodingType]Marshaler{
|
||||||
JSON: func(res Response) ([]byte, error) {
|
JSON: func(res Response) ([]byte, error) {
|
||||||
if res.Error() != nil {
|
if res.Error() != nil {
|
||||||
return json.MarshalIndent(res.Error(), "", " ")
|
return json.MarshalIndent(res.Error(), "", " ")
|
||||||
@ -69,7 +69,7 @@ type Response interface {
|
|||||||
Output() interface{}
|
Output() interface{}
|
||||||
|
|
||||||
// Marshal marshals out the response into a buffer. It uses the EncodingType
|
// Marshal marshals out the response into a buffer. It uses the EncodingType
|
||||||
// on the Request to chose a Marshaller (Codec).
|
// on the Request to chose a Marshaler (Codec).
|
||||||
Marshal() ([]byte, error)
|
Marshal() ([]byte, error)
|
||||||
|
|
||||||
// Gets a io.Reader that reads the marshalled output
|
// Gets a io.Reader that reads the marshalled output
|
||||||
@ -122,9 +122,9 @@ func (r *response) Marshal() ([]byte, error) {
|
|||||||
return []byte(r.Error().Error()), nil
|
return []byte(r.Error().Error()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var marshaller Marshaller
|
var marshaller Marshaler
|
||||||
if r.req.Command() != nil && r.req.Command().Marshallers != nil {
|
if r.req.Command() != nil && r.req.Command().Marshalers != nil {
|
||||||
marshaller = r.req.Command().Marshallers[encType]
|
marshaller = r.req.Command().Marshalers[encType]
|
||||||
}
|
}
|
||||||
if marshaller == nil {
|
if marshaller == nil {
|
||||||
var ok bool
|
var ok bool
|
||||||
|
@ -171,7 +171,7 @@ remains to be implemented.
|
|||||||
//
|
//
|
||||||
// return &AddOutput{added}, nil
|
// return &AddOutput{added}, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
val, ok := res.Output().(*AddOutput)
|
val, ok := res.Output().(*AddOutput)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -121,7 +121,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
|
|||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
Type: &Block{},
|
Type: &Block{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
block := res.Output().(*Block)
|
block := res.Output().(*Block)
|
||||||
s := fmt.Sprintf("Block added (%v bytes): %s\n", block.Length, block.Key)
|
s := fmt.Sprintf("Block added (%v bytes): %s\n", block.Length, block.Key)
|
||||||
|
@ -32,7 +32,7 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
|
|||||||
},
|
},
|
||||||
|
|
||||||
Run: bootstrapListCmd.Run,
|
Run: bootstrapListCmd.Run,
|
||||||
Marshallers: bootstrapListCmd.Marshallers,
|
Marshalers: bootstrapListCmd.Marshalers,
|
||||||
Type: bootstrapListCmd.Type,
|
Type: bootstrapListCmd.Type,
|
||||||
|
|
||||||
Subcommands: map[string]*cmds.Command{
|
Subcommands: map[string]*cmds.Command{
|
||||||
@ -77,11 +77,11 @@ in the bootstrap list).
|
|||||||
return &BootstrapOutput{added}, nil
|
return &BootstrapOutput{added}, nil
|
||||||
},
|
},
|
||||||
Type: &BootstrapOutput{},
|
Type: &BootstrapOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*BootstrapOutput)
|
v := res.Output().(*BootstrapOutput)
|
||||||
s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers))
|
s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers))
|
||||||
marshalled, err := bootstrapMarshaller(res)
|
marshalled, err := bootstrapMarshaler(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -124,11 +124,11 @@ var bootstrapRemoveCmd = &cmds.Command{
|
|||||||
return &BootstrapOutput{removed}, nil
|
return &BootstrapOutput{removed}, nil
|
||||||
},
|
},
|
||||||
Type: &BootstrapOutput{},
|
Type: &BootstrapOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*BootstrapOutput)
|
v := res.Output().(*BootstrapOutput)
|
||||||
s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers))
|
s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers))
|
||||||
marshalled, err := bootstrapMarshaller(res)
|
marshalled, err := bootstrapMarshaler(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -153,12 +153,12 @@ var bootstrapListCmd = &cmds.Command{
|
|||||||
return &BootstrapOutput{peers}, nil
|
return &BootstrapOutput{peers}, nil
|
||||||
},
|
},
|
||||||
Type: &BootstrapOutput{},
|
Type: &BootstrapOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: bootstrapMarshaller,
|
cmds.Text: bootstrapMarshaler,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func bootstrapMarshaller(res cmds.Response) ([]byte, error) {
|
func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
|
||||||
v, ok := res.Output().(*BootstrapOutput)
|
v, ok := res.Output().(*BootstrapOutput)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, u.ErrCast()
|
return nil, u.ErrCast()
|
||||||
|
@ -25,7 +25,7 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
|
|||||||
root := cmd2outputCmd("ipfs", root)
|
root := cmd2outputCmd("ipfs", root)
|
||||||
return &root, nil
|
return &root, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*Command)
|
v := res.Output().(*Command)
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
@ -80,7 +80,7 @@ Set the value of the 'datastore.path' key:
|
|||||||
return getConfig(filename, key)
|
return getConfig(filename, key)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
if len(res.Request().Arguments()) == 2 {
|
if len(res.Request().Arguments()) == 2 {
|
||||||
return nil, nil // dont output anything
|
return nil, nil // dont output anything
|
||||||
|
@ -85,7 +85,7 @@ connected peers and latencies between them.
|
|||||||
return &DiagnosticOutput{output}, nil
|
return &DiagnosticOutput{output}, nil
|
||||||
},
|
},
|
||||||
Type: &DiagnosticOutput{},
|
Type: &DiagnosticOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(r cmds.Response) ([]byte, error) {
|
cmds.Text: func(r cmds.Response) ([]byte, error) {
|
||||||
output, ok := r.Output().(*DiagnosticOutput)
|
output, ok := r.Output().(*DiagnosticOutput)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -49,8 +49,8 @@ output of a running daemon.
|
|||||||
log.Info(s)
|
log.Info(s)
|
||||||
return &MessageOutput{s}, nil
|
return &MessageOutput{s}, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: MessageTextMarshaller,
|
cmds.Text: MessageTextMarshaler,
|
||||||
},
|
},
|
||||||
Type: &MessageOutput{},
|
Type: &MessageOutput{},
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ it contains, with the following format:
|
|||||||
|
|
||||||
return &LsOutput{output}, nil
|
return &LsOutput{output}, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
s := ""
|
s := ""
|
||||||
output := res.Output().(*LsOutput).Objects
|
output := res.Output().(*LsOutput).Objects
|
||||||
|
@ -88,7 +88,7 @@ not be listable, as it is virtual. Accessing known paths directly.
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Type: &config.Mounts{},
|
Type: &config.Mounts{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*config.Mounts)
|
v := res.Output().(*config.Mounts)
|
||||||
s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS)
|
s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS)
|
||||||
|
@ -150,7 +150,7 @@ This command outputs data in the following encodings:
|
|||||||
return node, nil
|
return node, nil
|
||||||
},
|
},
|
||||||
Type: &Node{},
|
Type: &Node{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.EncodingType("protobuf"): func(res cmds.Response) ([]byte, error) {
|
cmds.EncodingType("protobuf"): func(res cmds.Response) ([]byte, error) {
|
||||||
object := res.Output().(*dag.Node)
|
object := res.Output().(*dag.Node)
|
||||||
return object.Marshal()
|
return object.Marshal()
|
||||||
|
@ -78,7 +78,7 @@ Publish a <ref> to another public key:
|
|||||||
k := n.Identity.PrivKey()
|
k := n.Identity.PrivKey()
|
||||||
return publish(n, k, ref)
|
return publish(n, k, ref)
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*IpnsEntry)
|
v := res.Output().(*IpnsEntry)
|
||||||
s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
|
s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
|
||||||
|
@ -65,7 +65,7 @@ Note: list all refs recursively with -r.
|
|||||||
return getRefs(n, paths, unique, recursive)
|
return getRefs(n, paths, unique, recursive)
|
||||||
},
|
},
|
||||||
Type: &RefsOutput{},
|
Type: &RefsOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
output := res.Output().(*RefsOutput)
|
output := res.Output().(*RefsOutput)
|
||||||
s := ""
|
s := ""
|
||||||
|
@ -73,7 +73,7 @@ Resolve te value of another name:
|
|||||||
|
|
||||||
return output, nil
|
return output, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
output := res.Output().(string)
|
output := res.Output().(string)
|
||||||
return []byte(output), nil
|
return []byte(output), nil
|
||||||
|
@ -79,6 +79,6 @@ type MessageOutput struct {
|
|||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
|
func MessageTextMarshaler(res cmds.Response) ([]byte, error) {
|
||||||
return []byte(res.Output().(*MessageOutput).Message), nil
|
return []byte(res.Output().(*MessageOutput).Message), nil
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ var updateCmd = &cmds.Command{
|
|||||||
"check": updateCheckCmd,
|
"check": updateCheckCmd,
|
||||||
"log": updateLogCmd,
|
"log": updateLogCmd,
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*UpdateOutput)
|
v := res.Output().(*UpdateOutput)
|
||||||
s := ""
|
s := ""
|
||||||
@ -65,7 +65,7 @@ Nothing will be downloaded or installed.
|
|||||||
return updateCheck(n)
|
return updateCheck(n)
|
||||||
},
|
},
|
||||||
Type: &UpdateOutput{},
|
Type: &UpdateOutput{},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*UpdateOutput)
|
v := res.Output().(*UpdateOutput)
|
||||||
s := ""
|
s := ""
|
||||||
|
@ -23,7 +23,7 @@ var versionCmd = &cmds.Command{
|
|||||||
Version: config.CurrentVersionNumber,
|
Version: config.CurrentVersionNumber,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*VersionOutput)
|
v := res.Output().(*VersionOutput)
|
||||||
s := ""
|
s := ""
|
||||||
|
Reference in New Issue
Block a user