diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index 2c90cb01245ef931f1566a3235d84b5aef5269e3..782491e56427df4f11ae5790245f64eb97672e43 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -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) diff --git a/commands/command.go b/commands/command.go index 2ce2cd3825800527d4aabe048e2c5d1a850415c6..0301e9d1b47fe471a2a49321b9b4221be65bf55b 100644 --- a/commands/command.go +++ b/commands/command.go @@ -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. @@ -45,11 +49,11 @@ type HelpText struct { // Command is a runnable command, with input arguments and options (flags). // It can also have Subcommands, to group units of work into sets. type Command struct { - Options []Option - Arguments []Argument - Run Function - Marshallers map[EncodingType]Marshaller - Helptext HelpText + Options []Option + Arguments []Argument + Run Function + Marshalers map[EncodingType]Marshaler + Helptext HelpText // Type describes the type of the output of the Command's Run Function. // In precise terms, the value of Type is an instance of the return type of diff --git a/commands/response.go b/commands/response.go index 74b6b5e075993f0ef405cf9a7ae84fd22d6dafe3..7e12e5922ad29c20a2df3bc7b08739d9b1ee7336 100644 --- a/commands/response.go +++ b/commands/response.go @@ -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 diff --git a/core/commands2/add.go b/core/commands2/add.go index 8dfd26ad24686298aabc51ea347b3f1515ccbc8c..4af5aeb3a827ff6d1733bc6a85f3cecb800d6173 100644 --- a/core/commands2/add.go +++ b/core/commands2/add.go @@ -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 { diff --git a/core/commands2/block.go b/core/commands2/block.go index 3a5c5b22526452cc9a888746526b08b2c0238e6d..84a3c304e810ab9cef8bfe25395502cddb9d9eae 100644 --- a/core/commands2/block.go +++ b/core/commands2/block.go @@ -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) diff --git a/core/commands2/bootstrap.go b/core/commands2/bootstrap.go index 738dcb2a4e4634400dd94f66e34177ce42894ac2..226b1f1690c4765ecb719868e11317e8be063dae 100644 --- a/core/commands2/bootstrap.go +++ b/core/commands2/bootstrap.go @@ -21,7 +21,7 @@ var peerOptionDesc = "A peer to add to the bootstrap list (in the format '<multi var bootstrapCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Show or edit the list of bootstrap peers", - Synopsis: ` + Synopsis: ` ipfs bootstrap list - Show peers in the bootstrap list ipfs bootstrap add <peer>... - Add peers to the bootstrap list ipfs bootstrap remove <peer>... - Removes peers from the bootstrap list @@ -31,9 +31,9 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'. ` + bootstrapSecurityWarning, }, - Run: bootstrapListCmd.Run, - Marshallers: bootstrapListCmd.Marshallers, - Type: bootstrapListCmd.Type, + Run: bootstrapListCmd.Run, + Marshalers: bootstrapListCmd.Marshalers, + Type: bootstrapListCmd.Type, Subcommands: map[string]*cmds.Command{ "list": bootstrapListCmd, @@ -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() diff --git a/core/commands2/commands.go b/core/commands2/commands.go index 8ce2b0e06f70be6d9e338e4d43b9182016865178..63124861d1bf48c119f7d221d4efeb3ab73e2ac8 100644 --- a/core/commands2/commands.go +++ b/core/commands2/commands.go @@ -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 diff --git a/core/commands2/config.go b/core/commands2/config.go index 5f7cfdd931e897dbdb37a3aa37ca7793b757d6fc..8a4b1bdb6f1ce67e201fba37e76503c589a411f6 100644 --- a/core/commands2/config.go +++ b/core/commands2/config.go @@ -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 diff --git a/core/commands2/diag.go b/core/commands2/diag.go index 8fd3db284c177dd73066d300aba9467a0124256b..8a68a02baf5bd19ea1c02c1a60900e4c417ee620 100644 --- a/core/commands2/diag.go +++ b/core/commands2/diag.go @@ -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 { diff --git a/core/commands2/log.go b/core/commands2/log.go index 157bdca27fd4162294544d744ba5d86286fe5936..ace008d9f0af4876513ff67b71d96c191b57440a 100644 --- a/core/commands2/log.go +++ b/core/commands2/log.go @@ -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{}, } diff --git a/core/commands2/ls.go b/core/commands2/ls.go index d948f635eacc1d9449ebb1f1ebfd1e91641bc39e..d07a207e887e463014793c3ad93dd88e0cd89251 100644 --- a/core/commands2/ls.go +++ b/core/commands2/ls.go @@ -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 diff --git a/core/commands2/mount_unix.go b/core/commands2/mount_unix.go index 490bce154c1fd1e2a7b8c24555b81912242e3aa6..bc6e625416fb6aa4eeafd96fa05fa3ed4d84d7c2 100644 --- a/core/commands2/mount_unix.go +++ b/core/commands2/mount_unix.go @@ -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) diff --git a/core/commands2/object.go b/core/commands2/object.go index dc5be0a0f1dffc64844334ebc1a84d9b629d7708..bf4a7cec69d289fed7a6cb63ea4a13b31b109aa7 100644 --- a/core/commands2/object.go +++ b/core/commands2/object.go @@ -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() diff --git a/core/commands2/publish.go b/core/commands2/publish.go index ab0e8153657e571b1f48342cf5f1ebd3a8612ee9..f7e76ba0a62d6169148550ef3e9ba76f387159a3 100644 --- a/core/commands2/publish.go +++ b/core/commands2/publish.go @@ -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) diff --git a/core/commands2/refs.go b/core/commands2/refs.go index 00cb5e7b44bae98854c48a3f545b8a705a821730..d3c243405c47ea022c8ea5da54380c956dc753a3 100644 --- a/core/commands2/refs.go +++ b/core/commands2/refs.go @@ -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 := "" diff --git a/core/commands2/resolve.go b/core/commands2/resolve.go index 0f9287303958bafaeec9f103349484cd537c1ca5..ebd47ed96fceca858368ac5be78cb8cf4c032305 100644 --- a/core/commands2/resolve.go +++ b/core/commands2/resolve.go @@ -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 diff --git a/core/commands2/root.go b/core/commands2/root.go index d2372dcc51d4680020b8d33d784c3303cb7d02d5..798bcd7cffd6ea012ce7fd39464dea7126668fab 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -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 } diff --git a/core/commands2/update.go b/core/commands2/update.go index 8dbfb3e547db0baab0b3554bf9c07e9592a7e196..6a3c77a4e0b73f0529be2f9762a203d1ca314095 100644 --- a/core/commands2/update.go +++ b/core/commands2/update.go @@ -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 := "" diff --git a/core/commands2/version.go b/core/commands2/version.go index 575f57f6c51f8a83c3238acc541ccd93e91a238c..f9f5a33f3a16a7b69f3ffca7b853bf7023037ebb 100644 --- a/core/commands2/version.go +++ b/core/commands2/version.go @@ -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 := ""