Skip to content

Commit

Permalink
Merge pull request #6817 from ipfs/feat/cid-codec-convert
Browse files Browse the repository at this point in the history
make it possible to change the codec with the `ipfs cid` subcommand
  • Loading branch information
Stebalien authored Jan 8, 2020
2 parents ca2767a + e58a32a commit 7d2f39b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
38 changes: 32 additions & 6 deletions core/commands/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var CidCmd = &cmds.Command{
const (
cidFormatOptionName = "f"
cidVerisonOptionName = "v"
cidCodecOptionName = "codec"
cidMultibaseOptionName = "b"
)

Expand All @@ -49,11 +50,13 @@ The optional format string is a printf style format string:
Options: []cmds.Option{
cmds.StringOption(cidFormatOptionName, "Printf style format string.").WithDefault("%s"),
cmds.StringOption(cidVerisonOptionName, "CID version to convert to."),
cmds.StringOption(cidCodecOptionName, "CID codec to convert to."),
cmds.StringOption(cidMultibaseOptionName, "Multibase to display CID in."),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
fmtStr, _ := req.Options[cidFormatOptionName].(string)
verStr, _ := req.Options[cidVerisonOptionName].(string)
codecStr, _ := req.Options[cidCodecOptionName].(string)
baseStr, _ := req.Options[cidMultibaseOptionName].(string)

opts := cidFormatOpts{}
Expand All @@ -63,10 +66,21 @@ The optional format string is a printf style format string:
}
opts.fmtStr = fmtStr

if codecStr != "" {
codec, ok := cid.Codecs[codecStr]
if !ok {
return fmt.Errorf("unknown IPLD codec: %s", codecStr)
}
opts.newCodec = codec
} // otherwise, leave it as 0 (not a valid IPLD codec)

switch verStr {
case "":
// noop
case "0":
if opts.newCodec != 0 && opts.newCodec != cid.DagProtobuf {
return fmt.Errorf("cannot convert to CIDv0 with any codec other than DagPB")
}
opts.verConv = toCidV0
case "1":
opts.verConv = toCidV1
Expand Down Expand Up @@ -125,9 +139,10 @@ var base32Cmd = &cmds.Command{
}

type cidFormatOpts struct {
fmtStr string
newBase mbase.Encoding
verConv func(cid cid.Cid) (cid.Cid, error)
fmtStr string
newBase mbase.Encoding
verConv func(cid cid.Cid) (cid.Cid, error)
newCodec uint64
}

type argumentIterator struct {
Expand Down Expand Up @@ -169,10 +184,11 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
emitErr = resp.Emit(res)
continue
}
base := opts.newBase
if base == -1 {
base, _ = cid.ExtractEncoding(cidStr)

if opts.newCodec != 0 && opts.newCodec != c.Type() {
c = cid.NewCidV1(opts.newCodec, c.Hash())
}

if opts.verConv != nil {
c, err = opts.verConv(c)
if err != nil {
Expand All @@ -181,6 +197,16 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
continue
}
}

base := opts.newBase
if base == -1 {
if c.Version() == 0 {
base = mbase.Base58BTC
} else {
base, _ = cid.ExtractEncoding(cidStr)
}
}

str, err := cidutil.Format(opts.fmtStr, base, c)
if _, ok := err.(cidutil.FormatStringError); ok {
// no point in continuing if there is a problem with the format string
Expand Down
23 changes: 23 additions & 0 deletions test/sharness/t0290-cid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ CIDv0="QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv"
CIDv1="zdj7WZAAFKPvYPPzyJLso2hhxo8a7ZACFQ4DvvfrNXTHidofr"
CIDb32="bafybeibxm2nsadl3fnxv2sxcxmxaco2jl53wpeorjdzidjwf5aqdg7wa6u"

CIDbase="QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6"
CIDb32pb="bafybeievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"
CIDb32raw="bafkreievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"

test_expect_success "cid base32 works" '
echo $CIDb32 > expected &&
ipfs cid base32 $CIDv0 > actual1 &&
Expand All @@ -26,6 +30,12 @@ test_expect_success "cid format -v 1 -b base58btc" '
test_cmp expected actual2
'

test_expect_success "cid format -v 0" '
echo $CIDv0 > expected &&
ipfs cid format -v 0 $CIDb32 > actual &&
test_cmp expected actual
'

cat <<EOF > various_cids
QmZZRTyhDpL5Jgift1cHbAhexeE1m2Hw8x8g7rTcPahDvo
QmPhk6cJkRcFfZCdYam4c9MKYjFG9V29LswUnbrFNhtk2S
Expand Down Expand Up @@ -228,4 +238,17 @@ test_expect_success "cid hashes --numeric" '
test_cmp hashes_expect actual
'

test_expect_success "cid format -c raw" '
echo $CIDb32raw > expected &&
ipfs cid format --codec raw -b base32 $CIDb32pb > actual &&
test_cmp actual expected
'

test_expect_success "cid format -c protobuf -v 0" '
echo $CIDbase > expected &&
ipfs cid format --codec protobuf -v 0 $CIDb32raw > actual &&
test_cmp actual expected
'


test_done

0 comments on commit 7d2f39b

Please sign in to comment.