@cryptotaxi247 / kubo / commits / 613508a0d

add raw support to the dag put command.

`dag get` still doesn't properly support raw output but that's a bigger issue. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Oct 5, 2017 at 22:59 UTC 613508a0d3d81111754e8844955445d73623b0bc
3 files changed +45
core/coredag/dagtransl.go
+2
@@ -37,6 +37,8 @@ var defaultRawParsers = FormatParsers{
37
38 "protobuf": dagpbRawParser,
39 "dag-pb": dagpbRawParser,
40 +
41 + "raw": rawRawParser,
42 }
43
44 var defaultCborParsers = FormatParsers{
core/coredag/raw.go new
+37
@@ -0,0 +1,37 @@
1 +package coredag
2 +
3 +import (
4 + "io"
5 + "io/ioutil"
6 + "math"
7 +
8 + "github.com/ipfs/go-ipfs/merkledag"
9 +
10 + cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
11 + node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
12 + block "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
13 + mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
14 +)
15 +
16 +func rawRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
17 + if mhType == math.MaxUint64 {
18 + mhType = mh.SHA2_256
19 + }
20 +
21 + data, err := ioutil.ReadAll(r)
22 + if err != nil {
23 + return nil, err
24 + }
25 +
26 + h, err := mh.Sum(data, mhType, mhLen)
27 + if err != nil {
28 + return nil, err
29 + }
30 + c := cid.NewCidV1(cid.Raw, h)
31 + blk, err := block.NewBlockWithCid(data, c)
32 + if err != nil {
33 + return nil, err
34 + }
35 + nd := &merkledag.RawNode{Block: blk}
36 + return []node.Node{nd}, nil
37 +}
test/sharness/t0053-dag.sh
+6
@@ -167,6 +167,12 @@ test_dag_cmd() {
167 test_cmp dag_put_exp dag_put_out
168 '
169
170 + test_expect_success "dag put with raw node works" '
171 + echo "foo bar" > raw_node_in &&
172 + HASH=$(ipfs dag put --format=raw --input-enc=raw -- raw_node_in) &&
173 + ipfs block get "$HASH" > raw_node_out &&
174 + test_cmp raw_node_in raw_node_out'
175 +
176 test_expect_success "dag put multiple files" '
177 printf {\"foo\":\"bar\"} > a.json &&
178 printf {\"foo\":\"baz\"} > b.json &&