Skip to content

Commit

Permalink
Clean up cid-fmt util code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Aug 1, 2018
1 parent 056eac1 commit 36bab48
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions cid-fmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ outer:
}
}
str, err := fmtCid(fmtStr, base, cid)
if err != nil {
switch err.(type) {
case FormatStringError:
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
// An error here means a bad format string, no point in continuing
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)
}
Expand Down Expand Up @@ -159,13 +166,26 @@ func decode(v string) (mb.Encoding, *c.Cid, 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) {
p := cid.Prefix()
out := new(bytes.Buffer)
var err error
encoder, err := mb.NewEncoder(base)
if err != nil {
return ERR_STR, err
return "", err
}
for i := 0; i < len(fmtStr); i++ {
if fmtStr[i] != '%' {
Expand All @@ -174,7 +194,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
}
i++
if i >= len(fmtStr) {
return "", fmt.Errorf("premature end of format string")
return "", FormatStringError{"premature end of format string", ""}
}
switch fmtStr[i] {
case '%':
Expand Down Expand Up @@ -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
dec, err := mh.Decode(cid.Hash())
if err != nil {
out.WriteString(ERR_STR)
errorMsg("%v", err)
continue
return "", err
}
out.WriteString(encode(encoder, dec.Digest, fmtStr[i] == 'D'))
case 's': // cid string encoded in base %b
str, err := cid.StringOfBase(base)
if err != nil {
out.WriteString(ERR_STR)
errorMsg("%v", err)
continue
return "", err
}
out.WriteString(str)
case 'S': // cid string without base prefix
Expand All @@ -225,7 +241,7 @@ func fmtCid(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
p.MhLength,
)
default:
return "", fmt.Errorf("unrecognized specifier in format string: %c", fmtStr[i])
return "", FormatStringError{"unrecognized specifier in format string", fmtStr[i-1 : i+1]}
}

}
Expand Down

0 comments on commit 36bab48

Please sign in to comment.