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
mf
go-multiaddr
Commits
76ea093a
Commit
76ea093a
authored
Nov 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
multiaddr conversion tool
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
0 deletions
+65
-0
multiaddr.go
multiaddr.go
+65
-0
No files found.
multiaddr.go
0 → 100644
View file @
76ea093a
package
main
import
(
"encoding/hex"
"flag"
"fmt"
"os"
ma
"github.com/jbenet/go-multiaddr"
)
var
usage
=
`multiaddr conversion
usage: multiaddr [fmt] <>`
var
formats
=
[]
string
{
"string"
,
"bytes"
,
"hex"
,
"raw"
}
var
format
string
func
init
()
{
flag
.
Usage
=
func
()
{
fmt
.
Fprintf
(
os
.
Stderr
,
"usage: %s <multiaddr>
\n\n
Flags:
\n
"
,
os
.
Args
[
0
])
flag
.
PrintDefaults
()
}
usage
:=
fmt
.
Sprintf
(
"output format, one of: %v"
,
formats
)
flag
.
StringVar
(
&
format
,
"format"
,
"string"
,
usage
)
flag
.
StringVar
(
&
format
,
"f"
,
"string"
,
usage
+
" (shorthand)"
)
}
func
main
()
{
flag
.
Parse
()
args
:=
flag
.
Args
()
if
len
(
args
)
!=
1
{
die
(
"error: can only take one argument"
)
}
m
,
err
:=
ma
.
NewMultiaddr
(
args
[
0
])
if
err
!=
nil
{
die
(
err
)
}
fmt
.
Println
(
outfmt
(
m
))
}
func
outfmt
(
m
ma
.
Multiaddr
)
string
{
switch
format
{
case
"string"
:
return
m
.
String
()
case
"bytes"
:
return
fmt
.
Sprintf
(
"%v"
,
m
.
Bytes
())
case
"raw"
:
return
string
(
m
.
Bytes
())
case
"hex"
:
return
"0x"
+
hex
.
EncodeToString
(
m
.
Bytes
())
}
die
(
"error: invalid format"
,
format
)
return
""
}
func
die
(
v
...
interface
{})
{
fmt
.
Fprint
(
os
.
Stderr
,
v
...
)
fmt
.
Fprint
(
os
.
Stderr
,
"
\n
"
)
flag
.
Usage
()
os
.
Exit
(
-
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