Skip to content

Commit

Permalink
feat: Allow specifing how object data is encoded
Browse files Browse the repository at this point in the history
Adds a --data-encoding flag to `ipfs object get` to let the user
specify base64 encoding for object data.

License: MIT
Signed-off-by: Alex Potsides <[email protected]>
  • Loading branch information
achingbrain committed Jun 19, 2018
1 parent d643c04 commit f2375db
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
37 changes: 35 additions & 2 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,22 @@ 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 data field can be specifed by 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 {
Expand All @@ -219,6 +229,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)
Expand All @@ -231,9 +247,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() {
Expand Down Expand Up @@ -702,3 +724,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")
}
6 changes: 6 additions & 0 deletions test/sharness/t0051-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ test_object_cmd() {
test_cmp ../t0051-object-data/expected_getOut actual_getOut
'

test_expect_success "can specify data encoding as base64" '
ipfs object get --data-encoding base64 $HASH > obj_out &&
echo "{\"Links\":[],\"Data\":\"dGVzdCBqc29uIGZvciBzaGFybmVzcyB0ZXN0\"}" > obj_exp &&
test_cmp obj_out obj_exp
'

test_expect_success "'ipfs object stat' succeeds" '
ipfs object stat $HASH >actual_stat
'
Expand Down

0 comments on commit f2375db

Please sign in to comment.