@@ -31,6 +31,7 @@ var CidCmd = &cmds.Command{
3131const (
3232 cidFormatOptionName = "f"
3333 cidVerisonOptionName = "v"
34+ cidCodecOptionName = "codec"
3435 cidMultibaseOptionName = "b"
3536)
3637
@@ -49,11 +50,13 @@ The optional format string is a printf style format string:
4950 Options : []cmds.Option {
5051 cmds .StringOption (cidFormatOptionName , "Printf style format string." ).WithDefault ("%s" ),
5152 cmds .StringOption (cidVerisonOptionName , "CID version to convert to." ),
53+ cmds .StringOption (cidCodecOptionName , "CID codec to convert to." ),
5254 cmds .StringOption (cidMultibaseOptionName , "Multibase to display CID in." ),
5355 },
5456 Run : func (req * cmds.Request , resp cmds.ResponseEmitter , env cmds.Environment ) error {
5557 fmtStr , _ := req .Options [cidFormatOptionName ].(string )
5658 verStr , _ := req .Options [cidVerisonOptionName ].(string )
59+ codecStr , _ := req .Options [cidCodecOptionName ].(string )
5760 baseStr , _ := req .Options [cidMultibaseOptionName ].(string )
5861
5962 opts := cidFormatOpts {}
@@ -63,10 +66,21 @@ The optional format string is a printf style format string:
6366 }
6467 opts .fmtStr = fmtStr
6568
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+
6677 switch verStr {
6778 case "" :
6879 // noop
6980 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+ }
7084 opts .verConv = toCidV0
7185 case "1" :
7286 opts .verConv = toCidV1
@@ -125,9 +139,10 @@ var base32Cmd = &cmds.Command{
125139}
126140
127141type 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
131146}
132147
133148type argumentIterator struct {
@@ -169,10 +184,11 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
169184 emitErr = resp .Emit (res )
170185 continue
171186 }
172- base := opts . newBase
173- if base == - 1 {
174- base , _ = cid .ExtractEncoding ( cidStr )
187+
188+ if opts . newCodec != 0 && opts . newCodec != c . Type () {
189+ c = cid .NewCidV1 ( opts . newCodec , c . Hash () )
175190 }
191+
176192 if opts .verConv != nil {
177193 c , err = opts .verConv (c )
178194 if err != nil {
@@ -181,6 +197,16 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
181197 continue
182198 }
183199 }
200+
201+ base := opts .newBase
202+ if base == - 1 {
203+ if c .Version () == 0 {
204+ base = mbase .Base58BTC
205+ } else {
206+ base , _ = cid .ExtractEncoding (cidStr )
207+ }
208+ }
209+
184210 str , err := cidutil .Format (opts .fmtStr , base , c )
185211 if _ , ok := err .(cidutil.FormatStringError ); ok {
186212 // no point in continuing if there is a problem with the format string
0 commit comments