Skip to content

Commit 3cdaea6

Browse files
committed
feat: Allow specifing how object data is encoded
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]>
1 parent d643c04 commit 3cdaea6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

core/commands/object/object.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,23 @@ This command outputs data in the following encodings:
204204
* "protobuf"
205205
* "json"
206206
* "xml"
207-
(Specified by the "--encoding" or "--enc" flag)`,
207+
(Specified by the "--encoding" or "--enc" flag)
208+
209+
The encoding of the object's data field can be specifed by using the
210+
--data-encoding flag
211+
212+
Supported values are:
213+
* "text" (default)
214+
* "base64"
215+
`,
208216
},
209217

210218
Arguments: []cmdkit.Argument{
211219
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
212220
},
221+
Options: []cmdkit.Option{
222+
cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
223+
},
213224
Run: func(req oldcmds.Request, res oldcmds.Response) {
214225
n, err := req.InvocContext().GetNode()
215226
if err != nil {
@@ -219,6 +230,12 @@ This command outputs data in the following encodings:
219230

220231
fpath := path.Path(req.Arguments()[0])
221232

233+
datafieldenc, _, err := req.Option("data-encoding").String()
234+
if err != nil {
235+
res.SetError(err, cmdkit.ErrNormal)
236+
return
237+
}
238+
222239
object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
223240
if err != nil {
224241
res.SetError(err, cmdkit.ErrNormal)
@@ -231,9 +248,15 @@ This command outputs data in the following encodings:
231248
return
232249
}
233250

251+
data, err := encodeData(pbo.Data(), datafieldenc)
252+
if err != nil {
253+
res.SetError(err, cmdkit.ErrNormal)
254+
return
255+
}
256+
234257
node := &Node{
235258
Links: make([]Link, len(object.Links())),
236-
Data: string(pbo.Data()),
259+
Data: data,
237260
}
238261

239262
for i, link := range object.Links() {
@@ -702,3 +725,14 @@ func unwrapOutput(i interface{}) (interface{}, error) {
702725

703726
return <-ch, nil
704727
}
728+
729+
func encodeData(data []byte, encoding string) (string, error) {
730+
switch encoding {
731+
case "text":
732+
return string(data), nil
733+
case "base64":
734+
return base64.StdEncoding.EncodeToString(data), nil
735+
}
736+
737+
return "", fmt.Errorf("unkown data field encoding")
738+
}

test/sharness/t0051-object.sh

+17
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ test_object_cmd() {
4747
test_cmp ../t0051-object-data/expected_getOut actual_getOut
4848
'
4949

50+
test_expect_success "'ipfs object get' can specify data encoding as base64" '
51+
ipfs object get --data-encoding base64 $HASH > obj_out &&
52+
echo "{\"Links\":[],\"Data\":\"CAISCkhlbGxvIE1hcnMYCg==\"}" > obj_exp &&
53+
test_cmp obj_out obj_exp
54+
'
55+
56+
test_expect_success "'ipfs object get' can specify data encoding as text" '
57+
echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" | ipfs object put &&
58+
ipfs object get --data-encoding text QmS3hVY6eYrMQ6L22agwrx3YHBEsc3LJxVXCtyQHqRBukH > obj_out &&
59+
echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" > obj_exp &&
60+
test_cmp obj_out obj_exp
61+
'
62+
63+
test_expect_failure "'ipfs object get' requires known data encoding" '
64+
ipfs object get --data-encoding nonsensical-encoding $HASH
65+
'
66+
5067
test_expect_success "'ipfs object stat' succeeds" '
5168
ipfs object stat $HASH >actual_stat
5269
'

0 commit comments

Comments
 (0)