Commit 0abc0a2f authored by Kevin Atkinson's avatar Kevin Atkinson

Use new Encoder in multibase package.

This allows multibase codes to be specified by the name rather than just
the prefix char.
parent 06d2604d
...@@ -55,11 +55,12 @@ outer: ...@@ -55,11 +55,12 @@ outer:
if len(args) < 2 { if len(args) < 2 {
usage() usage()
} }
if len(args[1]) != 1 { encoder, err := mb.EncoderByName(args[1])
fmt.Fprintf(os.Stderr, "Error: Invalid multibase code: %s\n", args[1]) if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(2) os.Exit(2)
} }
newBase = mb.Encoding(args[1][0]) newBase = encoder.Encoding()
args = args[2:] args = args[2:]
case "-v": case "-v":
if len(args) < 2 { if len(args) < 2 {
...@@ -162,6 +163,10 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -162,6 +163,10 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
p := cid.Prefix() p := cid.Prefix()
out := new(bytes.Buffer) out := new(bytes.Buffer)
var err error var err error
encoder, err := mb.NewEncoder(base)
if err != nil {
return ERR_STR, err
}
for i := 0; i < len(fmtStr); i++ { for i := 0; i < len(fmtStr); i++ {
if fmtStr[i] != '%' { if fmtStr[i] != '%' {
out.WriteByte(fmtStr[i]) out.WriteByte(fmtStr[i])
...@@ -193,7 +198,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -193,7 +198,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
case 'L': // hash length case 'L': // hash length
fmt.Fprintf(out, "%d", p.MhLength) fmt.Fprintf(out, "%d", p.MhLength)
case 'm', 'M': // multihash encoded in base %b case 'm', 'M': // multihash encoded in base %b
out.WriteString(encode(base, cid.Hash(), fmtStr[i] == 'M')) out.WriteString(encode(encoder, cid.Hash(), fmtStr[i] == 'M'))
case 'd', 'D': // hash digest encoded in base %b case 'd', 'D': // hash digest encoded in base %b
dec, err := mh.Decode(cid.Hash()) dec, err := mh.Decode(cid.Hash())
if err != nil { if err != nil {
...@@ -201,7 +206,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -201,7 +206,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
errorMsg("%v", err) errorMsg("%v", err)
continue continue
} }
out.WriteString(encode(base, dec.Digest, fmtStr[i] == 'D')) out.WriteString(encode(encoder, dec.Digest, fmtStr[i] == 'D'))
case 's': // cid string encoded in base %b case 's': // cid string encoded in base %b
str, err := cid.StringOfBase(base) str, err := cid.StringOfBase(base)
if err != nil { if err != nil {
...@@ -211,7 +216,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -211,7 +216,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
} }
out.WriteString(str) out.WriteString(str)
case 'S': // cid string without base prefix case 'S': // cid string without base prefix
out.WriteString(encode(base, cid.Bytes(), true)) out.WriteString(encode(encoder, cid.Bytes(), true))
case 'P': // prefix case 'P': // prefix
fmt.Fprintf(out, "cidv%d-%s-%s-%d", fmt.Fprintf(out, "cidv%d-%s-%s-%d",
p.Version, p.Version,
...@@ -251,12 +256,8 @@ func hashToString(num uint64) string { ...@@ -251,12 +256,8 @@ func hashToString(num uint64) string {
return name return name
} }
func encode(base mb.Encoding, data []byte, strip bool) string { func encode(base mb.Encoder, data []byte, strip bool) string {
str, err := mb.Encode(base, data) str := base.Encode(data)
if err != nil {
errorMsg("%v", err)
return ERR_STR
}
if strip { if strip {
return str[1:] return str[1:]
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment