multiaddr.go 1.69 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8
package main

import (
	"encoding/hex"
	"flag"
	"fmt"
	"os"

Jeromy's avatar
Jeromy committed
9
	ma "github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
10
	manet "github.com/multiformats/go-multiaddr-net"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
// flags
14
var formats = []string{"string", "bytes", "hex", "slice"}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
var format string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
var hideLoopback bool
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18 19

func init() {
	flag.Usage = func() {
20
		fmt.Fprintf(os.Stderr, "usage: %s [<multiaddr>]\n\nFlags:\n", os.Args[0])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26
		flag.PrintDefaults()
	}

	usage := fmt.Sprintf("output format, one of: %v", formats)
	flag.StringVar(&format, "format", "string", usage)
	flag.StringVar(&format, "f", "string", usage+" (shorthand)")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
	flag.BoolVar(&hideLoopback, "hide-loopback", false, "do not display loopback addresses")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29 30 31 32
}

func main() {
	flag.Parse()
	args := flag.Args()
33
	if len(args) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		output(localAddresses()...)
	} else {
		output(address(args[0]))
	}
}

func localAddresses() []ma.Multiaddr {
	maddrs, err := manet.InterfaceMultiaddrs()
	if err != nil {
		die(err)
	}

	if !hideLoopback {
		return maddrs
	}
49

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51 52 53 54
	var maddrs2 []ma.Multiaddr
	for _, a := range maddrs {
		if !manet.IsIPLoopback(a) {
			maddrs2 = append(maddrs2, a)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59 60 61
	return maddrs2
}

func address(addr string) ma.Multiaddr {
	m, err := ma.NewMultiaddr(addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65
	if err != nil {
		die(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66
	return m
67 68 69 70 71 72
}

func output(ms ...ma.Multiaddr) {
	for _, m := range ms {
		fmt.Println(outfmt(m))
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76 77 78
}

func outfmt(m ma.Multiaddr) string {
	switch format {
	case "string":
		return m.String()
79
	case "slice":
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
		return fmt.Sprintf("%v", m.Bytes())
81
	case "bytes":
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		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)
}