forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.go
63 lines (53 loc) · 1.54 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"flag"
"strings"
dht "github.com/libp2p/go-libp2p-kad-dht"
maddr "github.com/multiformats/go-multiaddr"
)
// A new type we need for writing a custom flag parser
type addrList []maddr.Multiaddr
func (al *addrList) String() string {
strs := make([]string, len(*al))
for i, addr := range *al {
strs[i] = addr.String()
}
return strings.Join(strs, ",")
}
func (al *addrList) Set(value string) error {
addr, err := maddr.NewMultiaddr(value)
if err != nil {
return err
}
*al = append(*al, addr)
return nil
}
func StringsToAddrs(addrStrings []string) (maddrs []maddr.Multiaddr, err error) {
for _, addrString := range addrStrings {
addr, err := maddr.NewMultiaddr(addrString)
if err != nil {
return maddrs, err
}
maddrs = append(maddrs, addr)
}
return
}
type Config struct {
RendezvousString string
BootstrapPeers addrList
ListenAddresses addrList
ProtocolID string
}
func ParseFlags() (Config, error) {
config := Config{}
flag.StringVar(&config.RendezvousString, "rendezvous", "meet me here",
"Unique string to identify group of nodes. Share this with your friends to let them connect with you")
flag.Var(&config.BootstrapPeers, "peer", "Adds a peer multiaddress to the bootstrap list")
flag.Var(&config.ListenAddresses, "listen", "Adds a multiaddress to the listen list")
flag.StringVar(&config.ProtocolID, "pid", "/chat/1.1.0", "Sets a protocol id for stream headers")
flag.Parse()
if len(config.BootstrapPeers) == 0 {
config.BootstrapPeers = dht.DefaultBootstrapPeers
}
return config, nil
}