feat(commands/cid): add a flag for setting the codec
This will be useful when testing `refs local, `repo gc`, and `repo verify` commands once we store blocks by multihash instead of by CID. At that point, these commands will return raw v1 CIDs as the blockstore won't actually remember the codec used to store the block. Flags choice: * Ideally, we'd use the `-f, --format` flags like every other command but we're already using `-f` (format) for the format string. * Alternatively, I'd like to use `-c`. However, we're using _that_ for a global `--config` flag (bit of a waste given that it doesn't work...). `--codec` will have to do for now.
Steven Allen committed
Jan 7, 2020 at 19:32 UTC
e58a32ab1e18d8a9562d1908a08c5a84863593ef
2 files changed
+39
-3
core/commands/cid.go
+22
-3
@@ -31,6 +31,7 @@ var CidCmd = &cmds.Command{
31
const (
32
cidFormatOptionName = "f"
33
cidVerisonOptionName = "v"
34
+ cidCodecOptionName = "codec"
35
cidMultibaseOptionName = "b"
36
)
37
@@ -49,11 +50,13 @@ The optional format string is a printf style format string:
50
Options: []cmds.Option{
51
cmds.StringOption(cidFormatOptionName, "Printf style format string.").WithDefault("%s"),
52
cmds.StringOption(cidVerisonOptionName, "CID version to convert to."),
53
+ cmds.StringOption(cidCodecOptionName, "CID codec to convert to."),
54
cmds.StringOption(cidMultibaseOptionName, "Multibase to display CID in."),
55
},
56
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
57
fmtStr, _ := req.Options[cidFormatOptionName].(string)
58
verStr, _ := req.Options[cidVerisonOptionName].(string)
59
+ codecStr, _ := req.Options[cidCodecOptionName].(string)
60
baseStr, _ := req.Options[cidMultibaseOptionName].(string)
61
62
opts := cidFormatOpts{}
@@ -63,10 +66,21 @@ The optional format string is a printf style format string:
66
}
67
opts.fmtStr = fmtStr
68
69
+ if codecStr != "" {
70
+ codec, ok := cid.Codecs[codecStr]
71
+ if !ok {
72
+ return fmt.Errorf("unknown IPLD codec: %s", codecStr)
73
+ }
74
+ opts.newCodec = codec
75
+ } // otherwise, leave it as 0 (not a valid IPLD codec)
76
+
77
switch verStr {
78
case "":
79
// noop
80
case "0":
81
+ if opts.newCodec != 0 && opts.newCodec != cid.DagProtobuf {
82
+ return fmt.Errorf("cannot convert to CIDv0 with any codec other than DagPB")
83
+ }
84
opts.verConv = toCidV0
85
case "1":
86
opts.verConv = toCidV1
@@ -125,9 +139,10 @@ var base32Cmd = &cmds.Command{
139
}
140
141
type cidFormatOpts struct {
128
- fmtStr string
129
- newBase mbase.Encoding
130
- verConv func(cid cid.Cid) (cid.Cid, error)
142
+ fmtStr string
143
+ newBase mbase.Encoding
144
+ verConv func(cid cid.Cid) (cid.Cid, error)
145
+ newCodec uint64
146
}
147
148
type argumentIterator struct {
@@ -170,6 +185,10 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
185
continue
186
}
187
188
+ if opts.newCodec != 0 && opts.newCodec != c.Type() {
189
+ c = cid.NewCidV1(opts.newCodec, c.Hash())
190
+ }
191
+
192
if opts.verConv != nil {
193
c, err = opts.verConv(c)
194
if err != nil {
test/sharness/t0290-cid.sh
+17
@@ -10,6 +10,10 @@ CIDv0="QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv"
10
CIDv1="zdj7WZAAFKPvYPPzyJLso2hhxo8a7ZACFQ4DvvfrNXTHidofr"
11
CIDb32="bafybeibxm2nsadl3fnxv2sxcxmxaco2jl53wpeorjdzidjwf5aqdg7wa6u"
12
13
+CIDbase="QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6"
14
+CIDb32pb="bafybeievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"
15
+CIDb32raw="bafkreievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"
16
+
17
test_expect_success "cid base32 works" '
18
echo $CIDb32 > expected &&
19
ipfs cid base32 $CIDv0 > actual1 &&
@@ -234,4 +238,17 @@ test_expect_success "cid hashes --numeric" '
238
test_cmp hashes_expect actual
239
'
240
241
+test_expect_success "cid format -c raw" '
242
+ echo $CIDb32raw > expected &&
243
+ ipfs cid format --codec raw -b base32 $CIDb32pb > actual &&
244
+ test_cmp actual expected
245
+'
246
+
247
+test_expect_success "cid format -c protobuf -v 0" '
248
+ echo $CIDbase > expected &&
249
+ ipfs cid format --codec protobuf -v 0 $CIDb32raw > actual &&
250
+ test_cmp actual expected
251
+'
252
+
253
+
254
test_done