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 <alex@achingbrain.net>
achingbrain committed
Jun 19, 2018 at 12:12 UTC
3cdaea663c3740f91972b88f04759335d1f9ef9f
2 files changed
+53
-2
core/commands/object/object.go
+36
-2
@@ -204,12 +204,23 @@ This command outputs data in the following encodings:
204
* "protobuf"
205
* "json"
206
* "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
+`,
216
},
217
218
Arguments: []cmdkit.Argument{
219
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
220
},
221
+ Options: []cmdkit.Option{
222
+ cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
223
+ },
224
Run: func(req oldcmds.Request, res oldcmds.Response) {
225
n, err := req.InvocContext().GetNode()
226
if err != nil {
@@ -219,6 +230,12 @@ This command outputs data in the following encodings:
230
231
fpath := path.Path(req.Arguments()[0])
232
233
+ datafieldenc, _, err := req.Option("data-encoding").String()
234
+ if err != nil {
235
+ res.SetError(err, cmdkit.ErrNormal)
236
+ return
237
+ }
238
+
239
object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
240
if err != nil {
241
res.SetError(err, cmdkit.ErrNormal)
@@ -231,9 +248,15 @@ This command outputs data in the following encodings:
248
return
249
}
250
251
+ data, err := encodeData(pbo.Data(), datafieldenc)
252
+ if err != nil {
253
+ res.SetError(err, cmdkit.ErrNormal)
254
+ return
255
+ }
256
+
257
node := &Node{
258
Links: make([]Link, len(object.Links())),
236
- Data: string(pbo.Data()),
259
+ Data: data,
260
}
261
262
for i, link := range object.Links() {
@@ -702,3 +725,14 @@ func unwrapOutput(i interface{}) (interface{}, error) {
725
726
return <-ch, nil
727
}
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
@@ -47,6 +47,23 @@ test_object_cmd() {
47
test_cmp ../t0051-object-data/expected_getOut actual_getOut
48
'
49
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
+
67
test_expect_success "'ipfs object stat' succeeds" '
68
ipfs object stat $HASH >actual_stat
69
'