Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-cidutil
Commits
01620693
Commit
01620693
authored
Jul 31, 2018
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up cid-fmt util code.
parent
7734dbbe
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
11 deletions
+27
-11
cid-fmt/main.go
cid-fmt/main.go
+27
-11
No files found.
cid-fmt/main.go
View file @
01620693
...
@@ -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
.
Error
f
(
"premature end of format string"
)
return
""
,
FormatString
Error
{
"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
.
Error
f
(
"unrecognized specifier in format string
: %c
"
,
fmtStr
[
i
])
return
""
,
FormatString
Error
{
"unrecognized specifier in format string"
,
fmtStr
[
i
-
1
:
i
+
1
]}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment