diff --git a/core/commands/object/object.go b/core/commands/object/object.go index 5f9675645f9..b1e30c51bf3 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -204,12 +204,23 @@ This command outputs data in the following encodings: * "protobuf" * "json" * "xml" -(Specified by the "--encoding" or "--enc" flag)`, +(Specified by the "--encoding" or "--enc" flag) + +The encoding of the object's data field can be specifed by using the +--data-encoding flag + +Supported values are: + * "text" (default) + * "base64" +`, }, Arguments: []cmdkit.Argument{ cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(), }, + Options: []cmdkit.Option{ + cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"), + }, Run: func(req oldcmds.Request, res oldcmds.Response) { n, err := req.InvocContext().GetNode() if err != nil { @@ -219,6 +230,12 @@ This command outputs data in the following encodings: fpath := path.Path(req.Arguments()[0]) + datafieldenc, _, err := req.Option("data-encoding").String() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath) if err != nil { res.SetError(err, cmdkit.ErrNormal) @@ -231,9 +248,15 @@ This command outputs data in the following encodings: return } + data, err := encodeData(pbo.Data(), datafieldenc) + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + node := &Node{ Links: make([]Link, len(object.Links())), - Data: string(pbo.Data()), + Data: data, } for i, link := range object.Links() { @@ -702,3 +725,14 @@ func unwrapOutput(i interface{}) (interface{}, error) { return <-ch, nil } + +func encodeData(data []byte, encoding string) (string, error) { + switch encoding { + case "text": + return string(data), nil + case "base64": + return base64.StdEncoding.EncodeToString(data), nil + } + + return "", fmt.Errorf("unkown data field encoding") +} diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index 9661407e509..9df03a210d5 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -47,6 +47,23 @@ test_object_cmd() { test_cmp ../t0051-object-data/expected_getOut actual_getOut ' + test_expect_success "'ipfs object get' can specify data encoding as base64" ' + ipfs object get --data-encoding base64 $HASH > obj_out && + echo "{\"Links\":[],\"Data\":\"CAISCkhlbGxvIE1hcnMYCg==\"}" > obj_exp && + test_cmp obj_out obj_exp + ' + + test_expect_success "'ipfs object get' can specify data encoding as text" ' + echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" | ipfs object put && + ipfs object get --data-encoding text QmS3hVY6eYrMQ6L22agwrx3YHBEsc3LJxVXCtyQHqRBukH > obj_out && + echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" > obj_exp && + test_cmp obj_out obj_exp + ' + + test_expect_failure "'ipfs object get' requires known data encoding" ' + ipfs object get --data-encoding nonsensical-encoding $HASH + ' + test_expect_success "'ipfs object stat' succeeds" ' ipfs object stat $HASH >actual_stat '