fixed BlockGet and cleaned up logging
Henry committed
Oct 14, 2014 at 00:31 UTC
073ec1152985ec3e14e7903a61ca0e9e545e0d4f
1 file changed
+22
-14
core/commands/block.go
+22
-14
@@ -1,44 +1,52 @@
1
package commands
2
3
import (
4
+ "fmt"
5
"io"
6
"io/ioutil"
7
"os"
8
9
+ mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
"github.com/jbenet/go-ipfs/blocks"
11
"github.com/jbenet/go-ipfs/core"
12
u "github.com/jbenet/go-ipfs/util"
13
)
14
15
+// BlockGet retrives a raw ipfs block from the node's BlockService
16
func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
14
- k := u.Key(args[0])
15
- u.PErr("Getting block[%s]\n", k)
17
18
+ if !u.IsValidHash(args[0]) {
19
+ return fmt.Errorf("block get: not a valid hash")
20
+ }
21
+
22
+ h, err := mh.FromB58String(args[0])
23
+ if err != nil {
24
+ return fmt.Errorf("block get: %v", err)
25
+ }
26
+
27
+ k := u.Key(h)
28
+ log.Debug("BlockGet key: '%q'", k)
29
b, err := n.Blocks.GetBlock(k)
30
if err != nil {
19
- return err
31
+ return fmt.Errorf("block get: %v", err)
32
}
33
22
- out.Write(b.Data)
23
- return nil
34
+ _, err = out.Write(b.Data)
35
+ return err
36
}
37
38
+// BlockPut reads everything from conn and saves the data to the nodes BlockService
39
func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
27
-
40
data, err := ioutil.ReadAll(os.Stdin)
41
if err != nil {
42
return err
43
}
44
45
b := blocks.NewBlock(data)
34
- u.PErr("Putting block[%s]\n", b.Key())
35
-
36
- key, err := n.Blocks.AddBlock(b)
37
- if err != nil {
38
- return err
39
- }
46
+ log.Debug("BlockPut key: '%q'", b.Key())
47
41
- u.PErr("Done. Key: %s\n", key)
48
+ _, err = n.Blocks.AddBlock(b)
49
+ log.Debug("BlockPut Done. Err: %q", err)
50
43
- return nil
51
+ return err
52
}