@cryptotaxi247 / kubo / commits / 38318a6c2

core/commands2: Added output for 'add' command

Matt Bell committed Nov 2, 2014 at 19:15 UTC 38318a6c28f637271848cc83e07b8fc29aeb72d9
1 file changed +31 -2
core/commands2/add.go
+31 -2
@@ -13,6 +13,10 @@ import (
13 // Error indicating the max depth has been exceded.
14 var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
15
16 +type AddOutput struct {
17 + Added []Object
18 +}
19 +
20 var addCmd = &cmds.Command{
21 Options: []cmds.Option{
22 cmds.Option{[]string{"recursive", "r"}, cmds.Bool},
@@ -29,16 +33,41 @@ var addCmd = &cmds.Command{
33 //if r, _ := opt.(bool); found && r {
34 //}
35
36 + added := make([]Object, len(req.Arguments()))
37 +
38 // add every path in args
33 - for _, arg := range req.Arguments() {
39 + for i, arg := range req.Arguments() {
40 // Add the file
41 node, err := add(n, arg.(io.Reader))
42 if err != nil {
43 res.SetError(err, cmds.ErrNormal)
44 + return
45 + }
46 +
47 + k, err := node.Key()
48 + if err != nil {
49 + res.SetError(err, cmds.ErrNormal)
50 + return
51 }
39 - fmt.Println(node.Key())
52 +
53 + added[i] = Object{k.String(), nil}
54 + }
55 +
56 + res.SetValue(&AddOutput{added})
57 + },
58 + Format: func(res cmds.Response) (string, error) {
59 + v := res.Value().(*AddOutput).Added
60 + if len(v) == 1 {
61 + return fmt.Sprintf("Added object: %s\n", v[0].Hash), nil
62 + }
63 +
64 + s := fmt.Sprintf("Added %v objects:\n", len(v))
65 + for _, obj := range v {
66 + s += fmt.Sprintf("- %s\n", obj.Hash)
67 }
68 + return s, nil
69 },
70 + Type: &AddOutput{},
71 }
72
73 func add(n *core.IpfsNode, in io.Reader) (*dag.Node, error) {