@cryptotaxi247 / kubo / commits / 3e30093c2

core/commands2: Updated commands to new Run function API

Matt Bell committed Nov 10, 2014 at 16:33 UTC 3e30093c2b4207fbd035144360c8f179fafd07a9
21 files changed +137 -264
cmd/ipfs2/daemon.go
+8 -11
@@ -21,33 +21,29 @@ var daemonCmd = &cmds.Command{
21 Run: daemonFunc,
22 }
23
24 -func daemonFunc(res cmds.Response, req cmds.Request) {
24 +func daemonFunc(req cmds.Request) (interface{}, error) {
25 ctx := req.Context()
26
27 lock, err := daemon.Lock(ctx.ConfigRoot)
28 if err != nil {
29 - res.SetError(fmt.Errorf("Couldn't obtain lock. Is another daemon already running?"), cmds.ErrNormal)
30 - return
29 + return nil, fmt.Errorf("Couldn't obtain lock. Is another daemon already running?")
30 }
31 defer lock.Close()
32
33 node, err := core.NewIpfsNode(ctx.Config, true)
34 if err != nil {
36 - res.SetError(err, cmds.ErrNormal)
37 - return
35 + return nil, err
36 }
37 ctx.Node = node
38
39 addr, err := ma.NewMultiaddr(ctx.Config.Addresses.API)
40 if err != nil {
43 - res.SetError(err, cmds.ErrNormal)
44 - return
41 + return nil, err
42 }
43
44 _, host, err := manet.DialArgs(addr)
45 if err != nil {
49 - res.SetError(err, cmds.ErrNormal)
50 - return
46 + return nil, err
47 }
48
49 cmdHandler := cmdsHttp.NewHandler(*ctx, commands.Root)
@@ -60,7 +56,8 @@ func daemonFunc(res cmds.Response, req cmds.Request) {
56
57 err = http.ListenAndServe(host, nil)
58 if err != nil {
63 - res.SetError(err, cmds.ErrNormal)
64 - return
59 + return nil, err
60 }
61 +
62 + return nil, nil
63 }
cmd/ipfs2/init.go
+5 -12
@@ -29,36 +29,29 @@ var initCmd = &cmds.Command{
29 cmds.Option{[]string{"datastore", "d"}, cmds.String,
30 "Location for the IPFS data store"},
31 },
32 - Run: func(res cmds.Response, req cmds.Request) {
32 + Run: func(req cmds.Request) (interface{}, error) {
33
34 arg, found := req.Option("d")
35 dspath, ok := arg.(string)
36 if found && !ok {
37 - res.SetError(errors.New("failed to parse datastore flag"), cmds.ErrNormal)
38 - return
37 + return nil, errors.New("failed to parse datastore flag")
38 }
39
40 arg, found = req.Option("f")
41 force, ok := arg.(bool) // TODO param
42 if found && !ok {
44 - res.SetError(errors.New("failed to parse force flag"), cmds.ErrNormal)
45 - return
43 + return nil, errors.New("failed to parse force flag")
44 }
45
46 arg, found = req.Option("b")
47 nBitsForKeypair, ok := arg.(int) // TODO param
48 if found && !ok {
51 - res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
52 - return
49 + return nil, errors.New("failed to get bits flag")
50 } else if !found {
51 nBitsForKeypair = 4096
52 }
53
57 - err := doInit(req.Context().ConfigRoot, dspath, force, nBitsForKeypair)
58 - if err != nil {
59 - res.SetError(err, cmds.ErrNormal)
60 - return
61 - }
54 + return nil, doInit(req.Context().ConfigRoot, dspath, force, nBitsForKeypair)
55 },
56 }
57
cmd/ipfs2/tour.go
+13 -14
@@ -30,14 +30,13 @@ IPFS very quickly. To start, run:
30 "next": cmdIpfsTourNext,
31 "restart": cmdIpfsTourRestart,
32 },
33 - Run: func(res cmds.Response, req cmds.Request) {
33 + Run: func(req cmds.Request) (interface{}, error) {
34
35 out := new(bytes.Buffer)
36 cfg := req.Context().Config
37 strs, err := internal.CastToStrings(req.Arguments())
38 if err != nil {
39 - res.SetError(err, cmds.ErrNormal)
40 - return
39 + return nil, err
40 }
41
42 topic := tour.TopicID(cfg.Tour.Last)
@@ -47,26 +46,24 @@ IPFS very quickly. To start, run:
46
47 err = tourShow(out, topic)
48 if err != nil {
50 - res.SetError(err, cmds.ErrNormal)
51 - return
49 + return nil, err
50 }
51
54 - res.SetOutput(out)
52 + return out, nil
53 },
54 }
55
56 var cmdIpfsTourNext = &cmds.Command{
57 Description: "Show the next IPFS Tour topic",
58
61 - Run: func(res cmds.Response, req cmds.Request) {
59 + Run: func(req cmds.Request) (interface{}, error) {
60 var w bytes.Buffer
61 cfg := req.Context().Config
62 path := req.Context().ConfigRoot
63
64 topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
65 if err := tourShow(&w, topic); err != nil {
68 - res.SetError(err, cmds.ErrNormal)
69 - return
66 + return nil, err
67 }
68
69 // topic changed, not last. write it out.
@@ -74,37 +71,39 @@ var cmdIpfsTourNext = &cmds.Command{
71 cfg.Tour.Last = string(topic)
72 err := writeConfig(path, cfg)
73 if err != nil {
77 - res.SetError(err, cmds.ErrNormal)
78 - return
74 + return nil, err
75 }
76 }
77
78 w.WriteTo(os.Stdout) // TODO write to res.SetValue
79 + return nil, nil
80 },
81 }
82
83 var cmdIpfsTourRestart = &cmds.Command{
84 Description: "Restart the IPFS Tour",
85
89 - Run: func(res cmds.Response, req cmds.Request) {
86 + Run: func(req cmds.Request) (interface{}, error) {
87 path := req.Context().ConfigRoot
88 cfg := req.Context().Config
89
90 cfg.Tour.Last = ""
91 err := writeConfig(path, cfg)
92 if err != nil {
96 - res.SetError(err, cmds.ErrNormal)
93 + return nil, err
94 }
95 + return nil, nil
96 },
97 }
98
99 var cmdIpfsTourList = &cmds.Command{
100 Description: "Show a list of IPFS Tour topics",
101
104 - Run: func(res cmds.Response, req cmds.Request) {
102 + Run: func(req cmds.Request) (interface{}, error) {
103 var w bytes.Buffer
104 tourListCmd(&w, req.Context().Config)
105 w.WriteTo(os.Stdout) // TODO use res.SetOutput(output)
106 + return nil, nil
107 },
108 }
109
core/commands2/add.go
+5 -8
@@ -34,19 +34,17 @@ var addCmd = &cmds.Command{
34 MerkleDAG. A smarter partial add with a staging area (like git)
35 remains to be implemented.
36 `,
37 - Run: func(res cmds.Response, req cmds.Request) {
37 + Run: func(req cmds.Request) (interface{}, error) {
38 n := req.Context().Node
39
40 readers, err := internal.CastToReaders(req.Arguments())
41 if err != nil {
42 - res.SetError(err, cmds.ErrNormal)
43 - return
42 + return nil, err
43 }
44
45 dagnodes, err := add(n, readers)
46 if err != nil {
48 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
49 - return
47 + return nil, errors.New("cast error")
48 }
49
50 // TODO: include fs paths in output (will need a way to specify paths in underlying filearg system)
@@ -54,14 +52,13 @@ var addCmd = &cmds.Command{
52 for _, dagnode := range dagnodes {
53 object, err := getOutput(dagnode)
54 if err != nil {
57 - res.SetError(err, cmds.ErrNormal)
58 - return
55 + return nil, err
56 }
57
58 added = append(added, object)
59 }
60
64 - res.SetOutput(&AddOutput{added})
61 + return &AddOutput{added}, nil
62 },
63 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
64 cmds.Text: func(res cmds.Response) ([]byte, error) {
core/commands2/block.go
+12 -19
@@ -40,35 +40,31 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
40 Arguments: []cmds.Argument{
41 cmds.Argument{"key", cmds.ArgString, true, false, "The base58 multihash of an existing block to get"},
42 },
43 - Run: func(res cmds.Response, req cmds.Request) {
43 + Run: func(req cmds.Request) (interface{}, error) {
44 n := req.Context().Node
45
46 key, ok := req.Arguments()[0].(string)
47 if !ok {
48 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
49 - return
48 + return nil, errors.New("cast error")
49 }
50
51 if !u.IsValidHash(key) {
53 - res.SetError(errors.New("Not a valid hash"), cmds.ErrClient)
54 - return
52 + return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
53 }
54
55 h, err := mh.FromB58String(key)
56 if err != nil {
59 - res.SetError(err, cmds.ErrNormal)
60 - return
57 + return nil, err
58 }
59
60 k := u.Key(h)
61 ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
62 b, err := n.Blocks.GetBlock(ctx, k)
63 if err != nil {
67 - res.SetError(err, cmds.ErrNormal)
68 - return
64 + return nil, err
65 }
66
71 - res.SetOutput(bytes.NewReader(b.Data))
67 + return bytes.NewReader(b.Data), nil
68 },
69 }
70
@@ -80,19 +76,17 @@ It reads from stdin, and <key> is a base58 encoded multihash.`,
76 Arguments: []cmds.Argument{
77 cmds.Argument{"data", cmds.ArgFile, true, false, "The data to be stored as an IPFS block"},
78 },
83 - Run: func(res cmds.Response, req cmds.Request) {
79 + Run: func(req cmds.Request) (interface{}, error) {
80 n := req.Context().Node
81
82 in, ok := req.Arguments()[0].(io.Reader)
83 if !ok {
88 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
89 - return
84 + return nil, errors.New("cast error")
85 }
86
87 data, err := ioutil.ReadAll(in)
88 if err != nil {
94 - res.SetError(err, cmds.ErrNormal)
95 - return
89 + return nil, err
90 }
91
92 b := blocks.NewBlock(data)
@@ -100,14 +94,13 @@ It reads from stdin, and <key> is a base58 encoded multihash.`,
94
95 k, err := n.Blocks.AddBlock(b)
96 if err != nil {
103 - res.SetError(err, cmds.ErrNormal)
104 - return
97 + return nil, err
98 }
99
107 - res.SetOutput(&Block{
100 + return &Block{
101 Key: k.String(),
102 Length: len(data),
110 - })
103 + }, nil
104 },
105 Type: &Block{},
106 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
core/commands2/bootstrap.go
+12 -18
@@ -41,26 +41,23 @@ in the bootstrap list).
41 Arguments: []cmds.Argument{
42 cmds.Argument{"peer", cmds.ArgString, true, true, peerOptionDesc},
43 },
44 - Run: func(res cmds.Response, req cmds.Request) {
44 + Run: func(req cmds.Request) (interface{}, error) {
45 input, err := bootstrapInputToPeers(req.Arguments())
46 if err != nil {
47 - res.SetError(err, cmds.ErrNormal)
48 - return
47 + return nil, err
48 }
49
50 filename, err := config.Filename(req.Context().ConfigRoot)
51 if err != nil {
53 - res.SetError(err, cmds.ErrNormal)
54 - return
52 + return nil, err
53 }
54
55 added, err := bootstrapAdd(filename, req.Context().Config, input)
56 if err != nil {
59 - res.SetError(err, cmds.ErrNormal)
60 - return
57 + return nil, err
58 }
59
63 - res.SetOutput(&BootstrapOutput{added})
60 + return &BootstrapOutput{added}, nil
61 },
62 Type: &BootstrapOutput{},
63 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
@@ -84,26 +81,23 @@ var bootstrapRemoveCmd = &cmds.Command{
81 Arguments: []cmds.Argument{
82 cmds.Argument{"peer", cmds.ArgString, true, true, peerOptionDesc},
83 },
87 - Run: func(res cmds.Response, req cmds.Request) {
84 + Run: func(req cmds.Request) (interface{}, error) {
85 input, err := bootstrapInputToPeers(req.Arguments())
86 if err != nil {
90 - res.SetError(err, cmds.ErrNormal)
91 - return
87 + return nil, err
88 }
89
90 filename, err := config.Filename(req.Context().ConfigRoot)
91 if err != nil {
96 - res.SetError(err, cmds.ErrNormal)
97 - return
92 + return nil, err
93 }
94
95 removed, err := bootstrapRemove(filename, req.Context().Config, input)
96 if err != nil {
102 - res.SetError(err, cmds.ErrNormal)
103 - return
97 + return nil, err
98 }
99
106 - res.SetOutput(&BootstrapOutput{removed})
100 + return &BootstrapOutput{removed}, nil
101 },
102 Type: &BootstrapOutput{},
103 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
@@ -124,9 +118,9 @@ var bootstrapListCmd = &cmds.Command{
118 Help: `Peers are output in the format '<multiaddr>/<peerID>'.
119 `,
120
127 - Run: func(res cmds.Response, req cmds.Request) {
121 + Run: func(req cmds.Request) (interface{}, error) {
122 peers := req.Context().Config.Bootstrap
129 - res.SetOutput(&BootstrapOutput{peers})
123 + return &BootstrapOutput{peers}, nil
124 },
125 Type: &BootstrapOutput{},
126 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
core/commands2/cat.go
+4 -6
@@ -19,24 +19,22 @@ it contains.
19 cmds.Argument{"ipfs-path", cmds.ArgString, true, true,
20 "The path to the IPFS object(s) to be outputted"},
21 },
22 - Run: func(res cmds.Response, req cmds.Request) {
22 + Run: func(req cmds.Request) (interface{}, error) {
23 node := req.Context().Node
24 readers := make([]io.Reader, 0, len(req.Arguments()))
25
26 paths, err := internal.CastToStrings(req.Arguments())
27 if err != nil {
28 - res.SetError(err, cmds.ErrNormal)
29 - return
28 + return nil, err
29 }
30
31 readers, err = cat(node, paths)
32 if err != nil {
34 - res.SetError(err, cmds.ErrNormal)
35 - return
33 + return nil, err
34 }
35
36 reader := io.MultiReader(readers...)
39 - res.SetOutput(reader)
37 + return reader, nil
38 },
39 }
40
core/commands2/commands.go
+2 -2
@@ -17,9 +17,9 @@ var commandsCmd = &cmds.Command{
17 Help: `Lists all available commands (and subcommands) and exits.
18 `,
19
20 - Run: func(res cmds.Response, req cmds.Request) {
20 + Run: func(req cmds.Request) (interface{}, error) {
21 root := outputCommand("ipfs", Root)
22 - res.SetOutput(&root)
22 + return &root, nil
23 },
24 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
25 cmds.Text: func(res cmds.Response) ([]byte, error) {
core/commands2/config.go
+12 -41
@@ -36,19 +36,17 @@ var configCmd = &cmds.Command{
36 cmds.Argument{"value", cmds.ArgString, false, false,
37 "The value to set the config entry to"},
38 },
39 - Run: func(res cmds.Response, req cmds.Request) {
39 + Run: func(req cmds.Request) (interface{}, error) {
40 args := req.Arguments()
41
42 key, ok := args[0].(string)
43 if !ok {
44 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
45 - return
44 + return nil, errors.New("cast error")
45 }
46
47 filename, err := config.Filename(req.Context().ConfigRoot)
48 if err != nil {
50 - res.SetError(err, cmds.ErrNormal)
51 - return
49 + return nil, err
50 }
51
52 var value string
@@ -56,28 +54,13 @@ var configCmd = &cmds.Command{
54 var ok bool
55 value, ok = args[1].(string)
56 if !ok {
59 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
60 - return
57 + return nil, errors.New("cast error")
58 }
59
63 - field, err := setConfig(filename, key, value)
64 - if err != nil {
65 - res.SetError(err, cmds.ErrNormal)
66 - return
67 - }
68 -
69 - res.SetOutput(field)
70 - return
60 + return setConfig(filename, key, value)
61
62 } else {
73 - field, err := getConfig(filename, key)
74 - if err != nil {
75 - res.SetError(err, cmds.ErrNormal)
76 - return
77 - }
78 -
79 - res.SetOutput(field)
80 - return
63 + return getConfig(filename, key)
64 }
65 },
66 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
@@ -111,20 +94,13 @@ var configShowCmd = &cmds.Command{
94 included in the output of this command.
95 `,
96
114 - Run: func(res cmds.Response, req cmds.Request) {
97 + Run: func(req cmds.Request) (interface{}, error) {
98 filename, err := config.Filename(req.Context().ConfigRoot)
99 if err != nil {
117 - res.SetError(err, cmds.ErrNormal)
118 - return
100 + return nil, err
101 }
102
121 - reader, err := showConfig(filename)
122 - if err != nil {
123 - res.SetError(err, cmds.ErrNormal)
124 - return
125 - }
126 -
127 - res.SetOutput(reader)
103 + return showConfig(filename)
104 },
105 }
106
@@ -134,18 +110,13 @@ var configEditCmd = &cmds.Command{
110 variable set to your preferred text editor.
111 `,
112
137 - Run: func(res cmds.Response, req cmds.Request) {
113 + Run: func(req cmds.Request) (interface{}, error) {
114 filename, err := config.Filename(req.Context().ConfigRoot)
115 if err != nil {
140 - res.SetError(err, cmds.ErrNormal)
141 - return
116 + return nil, err
117 }
118
144 - err = editConfig(filename)
145 - if err != nil {
146 - res.SetError(err, cmds.ErrNormal)
147 - return
148 - }
119 + return nil, editConfig(filename)
120 },
121 }
122
core/commands2/diag.go
+4 -7
@@ -1,7 +1,6 @@
1 package commands
2
3 import (
4 - "errors"
4 "fmt"
5 "io"
6 "time"
@@ -42,18 +41,16 @@ requesting a listing of data about them including number of
41 connected peers and latencies between them.
42 `,
43
45 - Run: func(res cmds.Response, req cmds.Request) {
44 + Run: func(req cmds.Request) (interface{}, error) {
45 n := req.Context().Node
46
47 if !n.OnlineMode() {
49 - res.SetError(errors.New("Cannot run diagnostic in offline mode!"), cmds.ErrNormal)
50 - return
48 + return nil, errNotOnline
49 }
50
51 info, err := n.Diagnostics.GetDiagnostic(time.Second * 20)
52 if err != nil {
55 - res.SetError(err, cmds.ErrNormal)
56 - return
53 + return nil, err
54 }
55
56 output := make([]DiagnosticPeer, len(info))
@@ -75,7 +72,7 @@ connected peers and latencies between them.
72 }
73 }
74
78 - res.SetOutput(&DiagnosticOutput{output})
75 + return &DiagnosticOutput{output}, nil
76 },
77 Type: &DiagnosticOutput{},
78 }
core/commands2/log.go
+3 -4
@@ -19,16 +19,15 @@ output of a running daemon.
19 cmds.Argument{"level", cmds.ArgString, true, false,
20 "one of: debug, info, notice, warning, error, critical"},
21 },
22 - Run: func(res cmds.Response, req cmds.Request) {
22 + Run: func(req cmds.Request) (interface{}, error) {
23 args := req.Arguments()
24 if err := u.SetLogLevel(args[0].(string), args[1].(string)); err != nil {
25 - res.SetError(err, cmds.ErrClient)
26 - return
25 + return nil, err
26 }
27
28 s := fmt.Sprintf("Changed log level of '%s' to '%s'", args[0], args[1])
29 log.Info(s)
31 - res.SetOutput(&MessageOutput{s})
30 + return &MessageOutput{s}, nil
31 },
32 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
33 cmds.Text: MessageTextMarshaller,
core/commands2/ls.go
+4 -6
@@ -34,21 +34,19 @@ it contains, with the following format:
34 cmds.Argument{"ipfs-path", cmds.ArgString, false, true,
35 "The path to the IPFS object(s) to list links from"},
36 },
37 - Run: func(res cmds.Response, req cmds.Request) {
37 + Run: func(req cmds.Request) (interface{}, error) {
38 node := req.Context().Node
39
40 paths, err := internal.CastToStrings(req.Arguments())
41 if err != nil {
42 - res.SetError(err, cmds.ErrNormal)
43 - return
42 + return nil, err
43 }
44
45 dagnodes := make([]*merkledag.Node, 0)
46 for _, path := range paths {
47 dagnode, err := node.Resolver.ResolvePath(path)
48 if err != nil {
50 - res.SetError(err, cmds.ErrNormal)
51 - return
49 + return nil, err
50 }
51 dagnodes = append(dagnodes, dagnode)
52 }
@@ -68,7 +66,7 @@ it contains, with the following format:
66 }
67 }
68
71 - res.SetOutput(&LsOutput{output})
69 + return &LsOutput{output}, nil
70 },
71 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
72 cmds.Text: func(res cmds.Response) ([]byte, error) {
core/commands2/mount_unix.go
+5 -8
@@ -35,18 +35,16 @@ not be listable, as it is virtual. Accessing known paths directly.
35 cmds.Option{[]string{"n"}, cmds.String,
36 "The path where IPNS should be mounted\n(default is '/ipns')"},
37 },
38 - Run: func(res cmds.Response, req cmds.Request) {
38 + Run: func(req cmds.Request) (interface{}, error) {
39 ctx := req.Context()
40
41 // error if we aren't running node in online mode
42 if ctx.Node.Network == nil {
43 - res.SetError(errNotOnline, cmds.ErrNormal)
44 - return
43 + return nil, errNotOnline
44 }
45
46 if err := platformFuseChecks(); err != nil {
48 - res.SetError(err, cmds.ErrNormal)
49 - return
47 + return nil, err
48 }
49
50 // update fsdir with flag.
@@ -74,11 +72,10 @@ not be listable, as it is virtual. Accessing known paths directly.
72 // mounted successfully, we timed out with no errors
73 case <-time.After(mountTimeout):
74 output := ctx.Config.Mounts
77 - res.SetOutput(&output)
78 - return
75 + return &output, nil
76 }
77
81 - res.SetError(err, cmds.ErrNormal)
78 + return nil, err
79 },
80 Type: &config.Mounts{},
81 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
core/commands2/mount_windows.go
+2 -2
@@ -10,7 +10,7 @@ var ipfsMount = &cmds.Command{
10 Description: "Not yet implemented on Windows",
11 Help: `Not yet implemented on Windows. :(`,
12
13 - Run: func(res cmds.Response, req cmds.Request) {
14 - res.SetError(errors.New("Mount isn't compatible with Windows yet"), cmds.ErrNormal)
13 + Run: func(req cmds.Request) (interface{}, error) {
14 + return errors.New("Mount isn't compatible with Windows yet"), nil
15 },
16 }
core/commands2/object.go
+15 -34
@@ -47,22 +47,15 @@ output is the raw data of the object.
47 cmds.Argument{"key", cmds.ArgString, true, false,
48 "Key of the object to retrieve, in base58-encoded multihash format"},
49 },
50 - Run: func(res cmds.Response, req cmds.Request) {
50 + Run: func(req cmds.Request) (interface{}, error) {
51 n := req.Context().Node
52
53 key, ok := req.Arguments()[0].(string)
54 if !ok {
55 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
56 - return
55 + return nil, errors.New("cast error")
56 }
57
59 - reader, err := objectData(n, key)
60 - if err != nil {
61 - res.SetError(err, cmds.ErrNormal)
62 - return
63 - }
64 -
65 - res.SetOutput(reader)
58 + return objectData(n, key)
59 },
60 }
61
@@ -75,22 +68,15 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
68 cmds.Argument{"key", cmds.ArgString, true, false,
69 "Key of the object to retrieve, in base58-encoded multihash format"},
70 },
78 - Run: func(res cmds.Response, req cmds.Request) {
71 + Run: func(req cmds.Request) (interface{}, error) {
72 n := req.Context().Node
73
74 key, ok := req.Arguments()[0].(string)
75 if !ok {
83 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
84 - return
85 - }
86 -
87 - output, err := objectLinks(n, key)
88 - if err != nil {
89 - res.SetError(err, cmds.ErrNormal)
90 - return
76 + return nil, errors.New("cast error")
77 }
78
93 - res.SetOutput(output)
79 + return objectLinks(n, key)
80 },
81 Type: &Object{},
82 }
@@ -111,19 +97,17 @@ This command outputs data in the following encodings:
97 cmds.Argument{"key", cmds.ArgString, true, false,
98 "Key of the object to retrieve\n(in base58-encoded multihash format)"},
99 },
114 - Run: func(res cmds.Response, req cmds.Request) {
100 + Run: func(req cmds.Request) (interface{}, error) {
101 n := req.Context().Node
102
103 key, ok := req.Arguments()[0].(string)
104 if !ok {
119 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
120 - return
105 + return nil, errors.New("cast error")
106 }
107
108 object, err := objectGet(n, key)
109 if err != nil {
125 - res.SetError(err, cmds.ErrNormal)
126 - return
110 + return nil, err
111 }
112
113 node := &Node{
@@ -139,7 +123,7 @@ This command outputs data in the following encodings:
123 }
124 }
125
142 - res.SetOutput(node)
126 + return node, nil
127 },
128 Type: &Node{},
129 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
@@ -167,19 +151,17 @@ Data should be in the format specified by <encoding>.
151 cmds.Argument{"encoding", cmds.ArgString, true, false,
152 "Encoding type of <data>, either \"protobuf\" or \"json\""},
153 },
170 - Run: func(res cmds.Response, req cmds.Request) {
154 + Run: func(req cmds.Request) (interface{}, error) {
155 n := req.Context().Node
156
157 input, ok := req.Arguments()[0].(io.Reader)
158 if !ok {
175 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
176 - return
159 + return nil, errors.New("cast error")
160 }
161
162 encoding, ok := req.Arguments()[1].(string)
163 if !ok {
181 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
182 - return
164 + return nil, errors.New("cast error")
165 }
166
167 output, err := objectPut(n, input, encoding)
@@ -188,11 +170,10 @@ Data should be in the format specified by <encoding>.
170 if err == ErrUnknownObjectEnc {
171 errType = cmds.ErrClient
172 }
191 - res.SetError(err, errType)
192 - return
173 + return nil, cmds.Error{err.Error(), errType}
174 }
175
195 - res.SetOutput(output)
176 + return output, nil
177 },
178 Type: &Object{},
179 }
core/commands2/pin.go
+8 -8
@@ -32,7 +32,7 @@ on disk.
32 cmds.Option{[]string{"recursive", "r"}, cmds.Bool,
33 "Recursively pin the object linked to by the specified object(s)"},
34 },
35 - Run: func(res cmds.Response, req cmds.Request) {
35 + Run: func(req cmds.Request) (interface{}, error) {
36 n := req.Context().Node
37
38 // set recursive flag
@@ -41,16 +41,16 @@ on disk.
41
42 paths, err := internal.CastToStrings(req.Arguments())
43 if err != nil {
44 - res.SetError(err, cmds.ErrNormal)
45 - return
44 + return nil, err
45 }
46
47 _, err = pin(n, paths, recursive)
48 if err != nil {
50 - res.SetError(err, cmds.ErrNormal)
49 + return nil, err
50 }
51
52 // TODO: create some output to show what got pinned
53 + return nil, nil
54 },
55 }
56
@@ -68,7 +68,7 @@ collected if needed.
68 cmds.Option{[]string{"recursive", "r"}, cmds.Bool,
69 "Recursively unpin the object linked to by the specified object(s)"},
70 },
71 - Run: func(res cmds.Response, req cmds.Request) {
71 + Run: func(req cmds.Request) (interface{}, error) {
72 n := req.Context().Node
73
74 // set recursive flag
@@ -77,16 +77,16 @@ collected if needed.
77
78 paths, err := internal.CastToStrings(req.Arguments())
79 if err != nil {
80 - res.SetError(err, cmds.ErrNormal)
81 - return
80 + return nil, err
81 }
82
83 _, err = unpin(n, paths, recursive)
84 if err != nil {
86 - res.SetError(err, cmds.ErrNormal)
85 + return nil, err
86 }
87
88 // TODO: create some output to show what got unpinned
89 + return nil, nil
90 },
91 }
92
core/commands2/publish.go
+5 -14
@@ -39,20 +39,18 @@ Publish a <ref> to another public key:
39 cmds.Argument{"ipfs-path", cmds.ArgString, true, false,
40 "IPFS path of the obejct to be published at <name>"},
41 },
42 - Run: func(res cmds.Response, req cmds.Request) {
42 + Run: func(req cmds.Request) (interface{}, error) {
43 log.Debug("Begin Publish")
44
45 n := req.Context().Node
46 args := req.Arguments()
47
48 if n.Network == nil {
49 - res.SetError(errNotOnline, cmds.ErrNormal)
50 - return
49 + return nil, errNotOnline
50 }
51
52 if n.Identity == nil {
54 - res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
55 - return
53 + return nil, errors.New("Identity not loaded!")
54 }
55
56 // name := ""
@@ -62,8 +60,7 @@ Publish a <ref> to another public key:
60 case 2:
61 // name = args[0]
62 ref = args[1].(string)
65 - res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
66 - return
63 + return nil, errors.New("keychains not yet implemented")
64 case 1:
65 // name = n.Identity.ID.String()
66 ref = args[0].(string)
@@ -71,13 +68,7 @@ Publish a <ref> to another public key:
68
69 // TODO n.Keychain.Get(name).PrivKey
70 k := n.Identity.PrivKey()
74 - publishOutput, err := publish(n, k, ref)
75 -
76 - if err != nil {
77 - res.SetError(err, cmds.ErrNormal)
78 - return
79 - }
80 - res.SetOutput(publishOutput)
71 + return publish(n, k, ref)
72 },
73 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
74 cmds.Text: func(res cmds.Response) ([]byte, error) {
core/commands2/refs.go
+3 -10
@@ -34,7 +34,7 @@ Note: list all refs recursively with -r.`,
34 cmds.Option{[]string{"recursive", "r"}, cmds.Bool,
35 "Recursively list links of child nodes"},
36 },
37 - Run: func(res cmds.Response, req cmds.Request) {
37 + Run: func(req cmds.Request) (interface{}, error) {
38 n := req.Context().Node
39
40 opt, found := req.Option("unique")
@@ -51,17 +51,10 @@ Note: list all refs recursively with -r.`,
51
52 paths, err := internal.CastToStrings(req.Arguments())
53 if err != nil {
54 - res.SetError(err, cmds.ErrNormal)
55 - return
56 - }
57 -
58 - output, err := getRefs(n, paths, unique, recursive)
59 - if err != nil {
60 - res.SetError(err, cmds.ErrNormal)
61 - return
54 + return nil, err
55 }
56
64 - res.SetOutput(output)
57 + return getRefs(n, paths, unique, recursive)
58 },
59 Type: &RefsOutput{},
60 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
core/commands2/resolve.go
+6 -10
@@ -31,20 +31,18 @@ Resolve te value of another name:
31 cmds.Argument{"name", cmds.ArgString, false, false,
32 "The IPNS name to resolve. Defaults to your node's peerID."},
33 },
34 - Run: func(res cmds.Response, req cmds.Request) {
34 + Run: func(req cmds.Request) (interface{}, error) {
35
36 n := req.Context().Node
37 var name string
38
39 if n.Network == nil {
40 - res.SetError(errNotOnline, cmds.ErrNormal)
41 - return
40 + return nil, errNotOnline
41 }
42
43 if len(req.Arguments()) == 0 {
44 if n.Identity == nil {
46 - res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
47 - return
45 + return nil, errors.New("Identity not loaded!")
46 }
47 name = n.Identity.ID().String()
48
@@ -52,18 +50,16 @@ Resolve te value of another name:
50 var ok bool
51 name, ok = req.Arguments()[0].(string)
52 if !ok {
55 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
56 - return
53 + return nil, errors.New("cast error")
54 }
55 }
56
57 output, err := n.Namesys.Resolve(name)
58 if err != nil {
62 - res.SetError(err, cmds.ErrNormal)
63 - return
59 + return nil, err
60 }
61
66 - res.SetOutput(output)
62 + return output, nil
63 },
64 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
65 cmds.Text: func(res cmds.Response) ([]byte, error) {
core/commands2/update.go
+6 -27
@@ -19,16 +19,9 @@ var updateCmd = &cmds.Command{
19 Help: `ipfs update is a utility command used to check for updates and apply them.
20 `,
21
22 - Run: func(res cmds.Response, req cmds.Request) {
22 + Run: func(req cmds.Request) (interface{}, error) {
23 n := req.Context().Node
24 -
25 - output, err := updateApply(n)
26 - if err != nil {
27 - res.SetError(err, cmds.ErrNormal)
28 - return
29 - }
30 -
31 - res.SetOutput(output)
24 + return updateApply(n)
25 },
26 Type: &UpdateOutput{},
27 Subcommands: map[string]*cmds.Command{
@@ -57,16 +50,9 @@ var updateCheckCmd = &cmds.Command{
50 Nothing will be downloaded or installed.
51 `,
52
60 - Run: func(res cmds.Response, req cmds.Request) {
53 + Run: func(req cmds.Request) (interface{}, error) {
54 n := req.Context().Node
62 -
63 - output, err := updateCheck(n)
64 - if err != nil {
65 - res.SetError(err, cmds.ErrNormal)
66 - return
67 - }
68 -
69 - res.SetOutput(output)
55 + return updateCheck(n)
56 },
57 Type: &UpdateOutput{},
58 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
@@ -89,16 +75,9 @@ var updateLogCmd = &cmds.Command{
75 Help: `This command is not yet implemented.
76 `,
77
92 - Run: func(res cmds.Response, req cmds.Request) {
78 + Run: func(req cmds.Request) (interface{}, error) {
79 n := req.Context().Node
94 -
95 - output, err := updateLog(n)
96 - if err != nil {
97 - res.SetError(err, cmds.ErrNormal)
98 - return
99 - }
100 -
101 - res.SetOutput(output)
80 + return updateLog(n)
81 },
82 }
83
core/commands2/version.go
+3 -3
@@ -20,10 +20,10 @@ var versionCmd = &cmds.Command{
20 cmds.Option{[]string{"number", "n"}, cmds.Bool,
21 "Only output the version number"},
22 },
23 - Run: func(res cmds.Response, req cmds.Request) {
24 - res.SetOutput(&VersionOutput{
23 + Run: func(req cmds.Request) (interface{}, error) {
24 + return &VersionOutput{
25 Version: config.CurrentVersionNumber,
26 - })
26 + }, nil
27 },
28 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
29 cmds.Text: func(res cmds.Response) ([]byte, error) {