Commit 01620693 authored by Kevin Atkinson's avatar Kevin Atkinson

Clean up cid-fmt util code.

parent 7734dbbe
...@@ -114,10 +114,17 @@ outer: ...@@ -114,10 +114,17 @@ outer:
} }
} }
str, err := fmtCid(fmtStr, base, cid) str, err := fmtCid(fmtStr, base, cid)
if err != nil { switch err.(type) {
case FormatStringError:
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
// An error here means a bad format string, no point in continuing
os.Exit(2) os.Exit(2)
default:
fmt.Fprintf(os.Stdout, "!ERROR!\n")
errorMsg("%s: %v", cidStr, err)
// Don't abort on cid specific errors
continue
case nil:
// no error
} }
fmt.Fprintf(os.Stdout, "%s\n", str) fmt.Fprintf(os.Stdout, "%s\n", str)
} }
...@@ -159,13 +166,26 @@ func decode(v string) (mb.Encoding, *c.Cid, error) { ...@@ -159,13 +166,26 @@ func decode(v string) (mb.Encoding, *c.Cid, error) {
const ERR_STR = "!ERROR!" const ERR_STR = "!ERROR!"
type FormatStringError struct {
Message string
Specifier string
}
func (e FormatStringError) Error() string {
if e.Specifier == "" {
return e.Message
} else {
return fmt.Sprintf("%s: %s", e.Message, e.Specifier)
}
}
func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { 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) encoder, err := mb.NewEncoder(base)
if err != nil { if err != nil {
return ERR_STR, err return "", err
} }
for i := 0; i < len(fmtStr); i++ { for i := 0; i < len(fmtStr); i++ {
if fmtStr[i] != '%' { if fmtStr[i] != '%' {
...@@ -174,7 +194,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -174,7 +194,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
} }
i++ i++
if i >= len(fmtStr) { if i >= len(fmtStr) {
return "", fmt.Errorf("premature end of format string") return "", FormatStringError{"premature end of format string", ""}
} }
switch fmtStr[i] { switch fmtStr[i] {
case '%': case '%':
...@@ -202,17 +222,13 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -202,17 +222,13 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
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 {
out.WriteString(ERR_STR) return "", err
errorMsg("%v", err)
continue
} }
out.WriteString(encode(encoder, 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 {
out.WriteString(ERR_STR) return "", err
errorMsg("%v", err)
continue
} }
out.WriteString(str) out.WriteString(str)
case 'S': // cid string without base prefix case 'S': // cid string without base prefix
...@@ -225,7 +241,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) { ...@@ -225,7 +241,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
p.MhLength, p.MhLength,
) )
default: default:
return "", fmt.Errorf("unrecognized specifier in format string: %c", fmtStr[i]) return "", FormatStringError{"unrecognized specifier in format string", fmtStr[i-1 : i+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