diff --git a/core/commands/object/patch.go b/core/commands/object/patch.go index c22b1172c06..837424e007b 100644 --- a/core/commands/object/patch.go +++ b/core/commands/object/patch.go @@ -79,7 +79,13 @@ the limit will not be respected by the network. return } - data, err := ioutil.ReadAll(req.Files()) + fi, err := req.Files().NextFile() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + data, err := ioutil.ReadAll(fi) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -102,7 +108,16 @@ the limit will not be respected by the network. } var patchSetDataCmd = &cmds.Command{ - Helptext: cmds.HelpText{}, + Helptext: cmds.HelpText{ + Tagline: "set data field of an ipfs object", + ShortDescription: ` +Set the data of an ipfs object from stdin or with the contents of a file + +EXAMPLE: + + $ echo "my data" | ipfs object patch $MYHASH set-data +`, + }, Arguments: []cmds.Argument{ cmds.StringArg("root", true, false, "the hash of the node to modify"), cmds.FileArg("data", true, false, "data fill with").EnableStdin(), @@ -126,7 +141,13 @@ var patchSetDataCmd = &cmds.Command{ return } - data, err := ioutil.ReadAll(req.Files()) + fi, err := req.Files().NextFile() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + data, err := ioutil.ReadAll(fi) if err != nil { res.SetError(err, cmds.ErrNormal) return diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index 89164e92f8a..00bdff0e134 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -241,7 +241,28 @@ test_object_cmd() { test_patch_create_path $BLANK a $FILE test_expect_success "create bad path fails" ' - test_must_fail ipfs object patch --create $EMPTY add-link / $FILE + test_must_fail ipfs object patch $EMPTY add-link --create / $FILE + ' + + test_expect_success "patch set-data works" ' + EMPTY=$(ipfs object new) && + HASH=$(printf "foo" | ipfs object patch $EMPTY set-data) + ' + + test_expect_success "output looks good" ' + echo "{\"Links\":[],\"Data\":\"foo\"}" > exp_data_set && + ipfs object get $HASH > actual_data_set && + test_cmp exp_data_set actual_data_set + ' + + test_expect_success "patch append-data works" ' + HASH=$(printf "bar" | ipfs object patch $HASH append-data) + ' + + test_expect_success "output looks good" ' + echo "{\"Links\":[],\"Data\":\"foobar\"}" > exp_data_append && + ipfs object get $HASH > actual_data_append && + test_cmp exp_data_append actual_data_append ' }