Commit 29a9c6f1 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

net: added InterfaceMultiaddrs

parent 76ea093a
...@@ -7,17 +7,15 @@ import ( ...@@ -7,17 +7,15 @@ import (
"os" "os"
ma "github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-multiaddr/net"
) )
var usage = `multiaddr conversion var formats = []string{"string", "bytes", "hex", "slice"}
usage: multiaddr [fmt] <>`
var formats = []string{"string", "bytes", "hex", "raw"}
var format string var format string
func init() { func init() {
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <multiaddr>\n\nFlags:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s [<multiaddr>]\n\nFlags:\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
} }
...@@ -29,8 +27,14 @@ func init() { ...@@ -29,8 +27,14 @@ func init() {
func main() { func main() {
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
if len(args) != 1 { if len(args) == 0 {
die("error: can only take one argument") maddrs, err := manet.InterfaceMultiaddrs()
if err != nil {
die(err)
}
output(maddrs...)
return
} }
m, err := ma.NewMultiaddr(args[0]) m, err := ma.NewMultiaddr(args[0])
...@@ -38,16 +42,22 @@ func main() { ...@@ -38,16 +42,22 @@ func main() {
die(err) die(err)
} }
fmt.Println(outfmt(m)) output(m)
}
func output(ms ...ma.Multiaddr) {
for _, m := range ms {
fmt.Println(outfmt(m))
}
} }
func outfmt(m ma.Multiaddr) string { func outfmt(m ma.Multiaddr) string {
switch format { switch format {
case "string": case "string":
return m.String() return m.String()
case "bytes": case "slice":
return fmt.Sprintf("%v", m.Bytes()) return fmt.Sprintf("%v", m.Bytes())
case "raw": case "bytes":
return string(m.Bytes()) return string(m.Bytes())
case "hex": case "hex":
return "0x" + hex.EncodeToString(m.Bytes()) return "0x" + hex.EncodeToString(m.Bytes())
......
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