Commit 072fed05 authored by Kevin Atkinson's avatar Kevin Atkinson

Add ability to change CID version.

parent 335a9b89
......@@ -13,7 +13,7 @@ import (
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [-b multibase-code] <fmt-str> <cid> ...\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "usage: %s [-b multibase-code] [-v cid-version] <fmt-str> <cid> ...\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "<fmt-str> is either 'prefix' or a printf style format string:\n%s", fmtRef)
os.Exit(1)
}
......@@ -46,16 +46,38 @@ func main() {
usage()
}
newBase := mb.Encoding(-1)
var verConv func(cid *c.Cid) (*c.Cid, error)
args := os.Args[1:]
if args[0] == "-b" {
outer:
for {
switch args[0] {
case "-b":
if len(args) < 2 {
usage()
}
if len(args[1]) != 1 {
fmt.Fprintf(os.Stderr, "Error: Invalid multibase code: %s\n", args[1])
os.Exit(1)
}
newBase = mb.Encoding(args[1][0])
args = args[2:]
case "-v":
if len(args) < 2 {
usage()
}
switch args[1] {
case "0":
verConv = toCidV0
case "1":
verConv = toCidV1
default:
fmt.Fprintf(os.Stderr, "Error: Invalid cid version: %s\n", args[1])
os.Exit(1)
}
args = args[2:]
default:
break outer
}
}
if len(args) < 2 {
usage()
......@@ -71,15 +93,24 @@ func main() {
}
for _, cidStr := range args[1:] {
base, cid, err := decode(cidStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s: %v\n", cidStr, err)
fmt.Fprintf(os.Stdout, "!INVALID_CID!\n")
// Don't abort on a bad cid
continue
}
if newBase != -1 {
base = newBase
}
if verConv != nil {
cid, err = verConv(cid)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s: %v\n", cidStr, err)
fmt.Fprintf(os.Stdout, "!INVALID_CID!\n")
// Don't abort on a bad cid
fmt.Fprintf(os.Stdout, "!CONVERSION_ERROR!\n")
// Don't abort on a bad conversion
continue
}
}
str, err := fmtCid(fmtStr, base, cid)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
......@@ -221,3 +252,14 @@ func encode(base mb.Encoding, data []byte, strip bool) string {
}
return str
}
func toCidV0(cid *c.Cid) (*c.Cid, error) {
if cid.Type() != c.DagProtobuf {
return nil, fmt.Errorf("can't convert non-protobuf nodes to cidv0")
}
return c.NewCidV0(cid.Hash()), nil
}
func toCidV1(cid *c.Cid) (*c.Cid, error) {
return c.NewCidV1(cid.Type(), cid.Hash()), nil
}
......@@ -71,3 +71,40 @@ func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, re
t.Error(fmt.Sprintf("expected: %s; but got: %s", result, str))
}
}
func TestCidConv(t *testing.T) {
cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
cidv1 := "zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi"
_, cid, err := decode(cidv0)
if err != nil {
t.Fatal(err)
}
cid, err = toCidV1(cid)
if err != nil {
t.Fatal(err)
}
if cid.String() != cidv1 {
t.Fatal("conversion failure")
}
cid, err = toCidV0(cid)
if err != nil {
t.Fatal(err)
}
cidStr := cid.String()
if cidStr != cidv0 {
t.Error(fmt.Sprintf("conversion failure, expected: %s; but got: %s", cidv0, cidStr))
}
}
func TestBadCidConv(t *testing.T) {
// this cid is a raw leaf and should not be able to convert to cidv0
cidv1 := "zb2rhhzX7uSKrtQ2ZZXFAabKiKFYZrJqKY2KE1cJ8yre2GSWZ"
_, cid, err := decode(cidv1)
if err != nil {
t.Fatal(err)
}
cid, err = toCidV0(cid)
if err == nil {
t.Fatal("expected failure")
}
}
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