Merge pull request #5139 from achingbrain/feat/specify-object-data-encoding

feat: Allow specifing how object data is encoded
This commit is contained in:
Whyrusleeping 2018-07-16 16:49:31 +02:00 committed by GitHub
commit 4f1391f477
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 2 deletions

View File

@ -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")
}

View File

@ -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
'