Skip to content

Commit 276f471

Browse files
committed
Merge pull request #882 from jbenet/ipfsObject
Ipfs object put from stdin
2 parents 5db973a + 2a0cd6d commit 276f471

File tree

5 files changed

+116
-12
lines changed

5 files changed

+116
-12
lines changed

core/commands/object.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ var ObjectCmd = &cmds.Command{
3535
'ipfs object' is a plumbing command used to manipulate DAG objects
3636
directly.`,
3737
Synopsis: `
38-
ipfs object get <key> - Get the DAG node named by <key>
39-
ipfs object put <data> <encoding> - Stores input, outputs its key
40-
ipfs object data <key> - Outputs raw bytes in an object
41-
ipfs object links <key> - Outputs links pointed to by object
42-
ipfs object stat <key> - Outputs statistics of object
38+
ipfs object get <key> - Get the DAG node named by <key>
39+
ipfs object put <data> - Stores input, outputs its key
40+
ipfs object data <key> - Outputs raw bytes in an object
41+
ipfs object links <key> - Outputs links pointed to by object
42+
ipfs object stat <key> - Outputs statistics of object
4343
`,
4444
},
4545

@@ -274,16 +274,18 @@ It reads from stdin, and the output is a base58 encoded multihash.
274274
'ipfs object put' is a plumbing command for storing DAG nodes.
275275
It reads from stdin, and the output is a base58 encoded multihash.
276276
277-
Data should be in the format specified by <encoding>.
278-
<encoding> may be one of the following:
277+
Data should be in the format specified by the --inputenc flag.
278+
--inputenc may be one of the following:
279279
* "protobuf"
280-
* "json"
280+
* "json" (default)
281281
`,
282282
},
283283

284284
Arguments: []cmds.Argument{
285-
cmds.FileArg("data", true, false, "Data to be stored as a DAG object"),
286-
cmds.StringArg("encoding", true, false, "Encoding type of <data>, either \"protobuf\" or \"json\""),
285+
cmds.FileArg("data", true, false, "Data to be stored as a DAG object").EnableStdin(),
286+
},
287+
Options: []cmds.Option{
288+
cmds.StringOption("inputenc", "Encoding type of input data, either \"protobuf\" or \"json\""),
287289
},
288290
Run: func(req cmds.Request, res cmds.Response) {
289291
n, err := req.Context().GetNode()
@@ -298,9 +300,16 @@ Data should be in the format specified by <encoding>.
298300
return
299301
}
300302

301-
encoding := req.Arguments()[0]
303+
inputenc, found, err := req.Option("inputenc").String()
304+
if err != nil {
305+
res.SetError(err, cmds.ErrNormal)
306+
return
307+
}
308+
if !found {
309+
inputenc = "json"
310+
}
302311

303-
output, err := objectPut(n, input, encoding)
312+
output, err := objectPut(n, input, inputenc)
304313
if err != nil {
305314
errType := cmds.ErrNormal
306315
if err == ErrUnknownObjectEnc {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Links": [],
3+
"Data": "\u0008\u0002\u0012\nHello Mars\u0018\n"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Data": "test json for sharness test"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
test json for sharness test

test/sharness/t0051-object.sh

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2015 Henry Bubert
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
7+
test_description="Test object command"
8+
9+
. lib/test-lib.sh
10+
11+
test_init_ipfs
12+
13+
test_expect_success "'ipfs add testData' succeeds" '
14+
printf "Hello Mars" >expected_in &&
15+
ipfs add expected_in >actual_Addout
16+
'
17+
18+
test_expect_success "'ipfs add testData' output looks good" '
19+
HASH="QmWkHFpYBZ9mpPRreRbMhhYWXfUhBAue3JkbbpFqwowSRb" &&
20+
echo "added $HASH expected_in" >expected_Addout &&
21+
test_cmp expected_Addout actual_Addout
22+
'
23+
24+
test_expect_success "'ipfs object get' succeeds" '
25+
ipfs object get $HASH >actual_getOut
26+
'
27+
28+
test_expect_success "'ipfs object get' output looks good" '
29+
test_cmp ../t0051-object-data/expected_getOut actual_getOut
30+
'
31+
32+
test_expect_success "'ipfs object stat' succeeds" '
33+
ipfs object stat $HASH >actual_stat
34+
'
35+
36+
test_expect_success "'ipfs object get' output looks good" '
37+
echo "NumLinks: 0" >> expected_stat &&
38+
echo "BlockSize: 18" >> expected_stat &&
39+
echo "LinksSize: 2" >> expected_stat &&
40+
echo "DataSize: 16" >> expected_stat &&
41+
echo "CumulativeSize: 18" >> expected_stat &&
42+
test_cmp expected_stat actual_stat
43+
'
44+
45+
test_expect_success "'ipfs object put file.json' succeeds" '
46+
ipfs object put ../t0051-object-data/testPut.json > actual_putOut
47+
'
48+
49+
test_expect_success "'ipfs object put file.json' output looks good" '
50+
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
51+
printf "added $HASH" > expected_putOut &&
52+
test_cmp expected_putOut actual_putOut
53+
'
54+
55+
test_expect_success "'ipfs object put file.pb' succeeds" '
56+
ipfs object put --inputenc=protobuf ../t0051-object-data/testPut.pb > actual_putOut
57+
'
58+
59+
test_expect_success "'ipfs object put file.pb' output looks good" '
60+
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
61+
printf "added $HASH" > expected_putOut &&
62+
test_cmp expected_putOut actual_putOut
63+
'
64+
65+
test_expect_success "'ipfs object put' from stdin succeeds" '
66+
cat ../t0051-object-data/testPut.json | ipfs object put > actual_putStdinOut
67+
'
68+
69+
test_expect_success "'ipfs object put' from stdin output looks good" '
70+
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
71+
printf "added $HASH" > expected_putStdinOut &&
72+
test_cmp expected_putStdinOut actual_putStdinOut
73+
'
74+
75+
test_expect_success "'ipfs object put' from stdin (pb) succeeds" '
76+
cat ../t0051-object-data/testPut.pb | ipfs object put --inputenc=protobuf > actual_putPbStdinOut
77+
'
78+
79+
test_expect_success "'ipfs object put' from stdin (pb) output looks good" '
80+
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
81+
printf "added $HASH" > expected_putStdinOut &&
82+
test_cmp expected_putStdinOut actual_putPbStdinOut
83+
'
84+
85+
86+
test_done

0 commit comments

Comments
 (0)