mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-19 18:05:32 +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 command doesn't support plaintext, use JSON instead)
|
||||
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)
|
||||
} else {
|
||||
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.
|
||||
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)
|
||||
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
|
||||
// text follows formats similar to man pages, but not exactly the same.
|
||||
@ -48,7 +52,7 @@ type Command struct {
|
||||
Options []Option
|
||||
Arguments []Argument
|
||||
Run Function
|
||||
Marshallers map[EncodingType]Marshaller
|
||||
Marshalers map[EncodingType]Marshaler
|
||||
Helptext HelpText
|
||||
|
||||
// Type describes the type of the output of the Command's Run Function.
|
||||
|
@ -40,7 +40,7 @@ const (
|
||||
// TODO: support more encoding types
|
||||
)
|
||||
|
||||
var marshallers = map[EncodingType]Marshaller{
|
||||
var marshallers = map[EncodingType]Marshaler{
|
||||
JSON: func(res Response) ([]byte, error) {
|
||||
if res.Error() != nil {
|
||||
return json.MarshalIndent(res.Error(), "", " ")
|
||||
@ -69,7 +69,7 @@ type Response interface {
|
||||
Output() interface{}
|
||||
|
||||
// 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)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
var marshaller Marshaller
|
||||
if r.req.Command() != nil && r.req.Command().Marshallers != nil {
|
||||
marshaller = r.req.Command().Marshallers[encType]
|
||||
var marshaller Marshaler
|
||||
if r.req.Command() != nil && r.req.Command().Marshalers != nil {
|
||||
marshaller = r.req.Command().Marshalers[encType]
|
||||
}
|
||||
if marshaller == nil {
|
||||
var ok bool
|
||||
|
@ -171,7 +171,7 @@ remains to be implemented.
|
||||
//
|
||||
// return &AddOutput{added}, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
val, ok := res.Output().(*AddOutput)
|
||||
if !ok {
|
||||
|
@ -121,7 +121,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
|
||||
}, nil
|
||||
},
|
||||
Type: &Block{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
block := res.Output().(*Block)
|
||||
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,
|
||||
Marshallers: bootstrapListCmd.Marshallers,
|
||||
Marshalers: bootstrapListCmd.Marshalers,
|
||||
Type: bootstrapListCmd.Type,
|
||||
|
||||
Subcommands: map[string]*cmds.Command{
|
||||
@ -77,11 +77,11 @@ in the bootstrap list).
|
||||
return &BootstrapOutput{added}, nil
|
||||
},
|
||||
Type: &BootstrapOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*BootstrapOutput)
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -124,11 +124,11 @@ var bootstrapRemoveCmd = &cmds.Command{
|
||||
return &BootstrapOutput{removed}, nil
|
||||
},
|
||||
Type: &BootstrapOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*BootstrapOutput)
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -153,12 +153,12 @@ var bootstrapListCmd = &cmds.Command{
|
||||
return &BootstrapOutput{peers}, nil
|
||||
},
|
||||
Type: &BootstrapOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
cmds.Text: bootstrapMarshaller,
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: bootstrapMarshaler,
|
||||
},
|
||||
}
|
||||
|
||||
func bootstrapMarshaller(res cmds.Response) ([]byte, error) {
|
||||
func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
|
||||
v, ok := res.Output().(*BootstrapOutput)
|
||||
if !ok {
|
||||
return nil, u.ErrCast()
|
||||
|
@ -25,7 +25,7 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
|
||||
root := cmd2outputCmd("ipfs", root)
|
||||
return &root, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*Command)
|
||||
var buf bytes.Buffer
|
||||
|
@ -80,7 +80,7 @@ Set the value of the 'datastore.path' key:
|
||||
return getConfig(filename, key)
|
||||
}
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
if len(res.Request().Arguments()) == 2 {
|
||||
return nil, nil // dont output anything
|
||||
|
@ -85,7 +85,7 @@ connected peers and latencies between them.
|
||||
return &DiagnosticOutput{output}, nil
|
||||
},
|
||||
Type: &DiagnosticOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(r cmds.Response) ([]byte, error) {
|
||||
output, ok := r.Output().(*DiagnosticOutput)
|
||||
if !ok {
|
||||
|
@ -49,8 +49,8 @@ output of a running daemon.
|
||||
log.Info(s)
|
||||
return &MessageOutput{s}, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
cmds.Text: MessageTextMarshaller,
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: MessageTextMarshaler,
|
||||
},
|
||||
Type: &MessageOutput{},
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ it contains, with the following format:
|
||||
|
||||
return &LsOutput{output}, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
s := ""
|
||||
output := res.Output().(*LsOutput).Objects
|
||||
|
@ -88,7 +88,7 @@ not be listable, as it is virtual. Accessing known paths directly.
|
||||
}
|
||||
},
|
||||
Type: &config.Mounts{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*config.Mounts)
|
||||
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
|
||||
},
|
||||
Type: &Node{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.EncodingType("protobuf"): func(res cmds.Response) ([]byte, error) {
|
||||
object := res.Output().(*dag.Node)
|
||||
return object.Marshal()
|
||||
|
@ -78,7 +78,7 @@ Publish a <ref> to another public key:
|
||||
k := n.Identity.PrivKey()
|
||||
return publish(n, k, ref)
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*IpnsEntry)
|
||||
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)
|
||||
},
|
||||
Type: &RefsOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
output := res.Output().(*RefsOutput)
|
||||
s := ""
|
||||
|
@ -73,7 +73,7 @@ Resolve te value of another name:
|
||||
|
||||
return output, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
output := res.Output().(string)
|
||||
return []byte(output), nil
|
||||
|
@ -79,6 +79,6 @@ type MessageOutput struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
|
||||
func MessageTextMarshaler(res cmds.Response) ([]byte, error) {
|
||||
return []byte(res.Output().(*MessageOutput).Message), nil
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ var updateCmd = &cmds.Command{
|
||||
"check": updateCheckCmd,
|
||||
"log": updateLogCmd,
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*UpdateOutput)
|
||||
s := ""
|
||||
@ -65,7 +65,7 @@ Nothing will be downloaded or installed.
|
||||
return updateCheck(n)
|
||||
},
|
||||
Type: &UpdateOutput{},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*UpdateOutput)
|
||||
s := ""
|
||||
|
@ -23,7 +23,7 @@ var versionCmd = &cmds.Command{
|
||||
Version: config.CurrentVersionNumber,
|
||||
}, nil
|
||||
},
|
||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||
v := res.Output().(*VersionOutput)
|
||||
s := ""
|
||||
|
Reference in New Issue
Block a user