From f95476c19aa6bcfe240c1c3bf6fc6fec3a54135b Mon Sep 17 00:00:00 2001
From: Matt Bell <mappum@gmail.com>
Date: Mon, 3 Nov 2014 23:17:39 -0800
Subject: [PATCH] commands: Allow overriding marshaller for any encoding type

---
 cmd/ipfs2/main.go          |  2 +-
 core/commands2/add.go      | 24 +++++++++++++-----------
 core/commands2/commands.go | 10 ++++++----
 core/commands2/log.go      |  6 ++++--
 core/commands2/ls.go       | 30 ++++++++++++++++--------------
 core/commands2/publish.go  | 10 ++++++----
 core/commands2/root.go     | 14 ++++++++------
 7 files changed, 54 insertions(+), 42 deletions(-)

diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go
index 5d30a265a..64e6a51b6 100644
--- a/cmd/ipfs2/main.go
+++ b/cmd/ipfs2/main.go
@@ -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)
diff --git a/core/commands2/add.go b/core/commands2/add.go
index aacb5c9e1..3dbce4430 100644
--- a/core/commands2/add.go
+++ b/core/commands2/add.go
@@ -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{},
 }
diff --git a/core/commands2/commands.go b/core/commands2/commands.go
index bc40faabe..0f102b2c4 100644
--- a/core/commands2/commands.go
+++ b/core/commands2/commands.go
@@ -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{},
 }
diff --git a/core/commands2/log.go b/core/commands2/log.go
index 534c8a07f..461107b1d 100644
--- a/core/commands2/log.go
+++ b/core/commands2/log.go
@@ -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{},
 }
diff --git a/core/commands2/ls.go b/core/commands2/ls.go
index a100f6884..1f67510cb 100644
--- a/core/commands2/ls.go
+++ b/core/commands2/ls.go
@@ -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{},
 }
diff --git a/core/commands2/publish.go b/core/commands2/publish.go
index 08c75c0c1..af12c1006 100644
--- a/core/commands2/publish.go
+++ b/core/commands2/publish.go
@@ -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{},
 }
diff --git a/core/commands2/root.go b/core/commands2/root.go
index 37df2c899..ee99e2ceb 100644
--- a/core/commands2/root.go
+++ b/core/commands2/root.go
@@ -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 MessageTextMarshaller(res cmds.Response) ([]byte, error) {
 	return []byte(res.Output().(*MessageOutput).Message), nil
 }
-- 
GitLab