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
14bdd327
Commit
14bdd327
authored
Nov 19, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'matool' of ../go-multiaddr
parents
63387dde
c1210ef5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
multiaddr/multiaddr.go
multiaddr/multiaddr.go
+96
-0
No files found.
multiaddr/multiaddr.go
0 → 100644
View file @
14bdd327
package
main
import
(
"encoding/hex"
"flag"
"fmt"
"os"
ma
"github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-multiaddr/net"
)
// flags
var
formats
=
[]
string
{
"string"
,
"bytes"
,
"hex"
,
"slice"
}
var
format
string
var
hideLoopback
bool
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)"
)
flag
.
BoolVar
(
&
hideLoopback
,
"hide-loopback"
,
false
,
"do not display loopback addresses"
)
}
func
main
()
{
flag
.
Parse
()
args
:=
flag
.
Args
()
if
len
(
args
)
==
0
{
output
(
localAddresses
()
...
)
}
else
{
output
(
address
(
args
[
0
]))
}
}
func
localAddresses
()
[]
ma
.
Multiaddr
{
maddrs
,
err
:=
manet
.
InterfaceMultiaddrs
()
if
err
!=
nil
{
die
(
err
)
}
if
!
hideLoopback
{
return
maddrs
}
var
maddrs2
[]
ma
.
Multiaddr
for
_
,
a
:=
range
maddrs
{
if
!
manet
.
IsIPLoopback
(
a
)
{
maddrs2
=
append
(
maddrs2
,
a
)
}
}
return
maddrs2
}
func
address
(
addr
string
)
ma
.
Multiaddr
{
m
,
err
:=
ma
.
NewMultiaddr
(
addr
)
if
err
!=
nil
{
die
(
err
)
}
return
m
}
func
output
(
ms
...
ma
.
Multiaddr
)
{
for
_
,
m
:=
range
ms
{
fmt
.
Println
(
outfmt
(
m
))
}
}
func
outfmt
(
m
ma
.
Multiaddr
)
string
{
switch
format
{
case
"string"
:
return
m
.
String
()
case
"slice"
:
return
fmt
.
Sprintf
(
"%v"
,
m
.
Bytes
())
case
"bytes"
:
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