-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.go
93 lines (80 loc) · 2.79 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// From: github.com/libp2p/go-libp2p-examples/blob/master/chat-with-rendezvous/flags.go
// Authors
// Abhishek Upperwal
// Mantas Vidutis
// TODO: Read config file form here too
package main
import (
"flag"
"strconv"
"strings"
"time"
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
}
// IPFS bootstrap nodes. Used to find other peers in the network.
var defaultBootstrapAddrStrings = []string{
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
}
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
HostMultiAddress string
ChannelConfigFile string
ChannelName string
Username string
}
func ParseFlags() (Config, error) {
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
config := Config{}
flag.StringVar(&config.RendezvousString, "rendezvous", "gravitation",
"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.StringVar(&config.HostMultiAddress, "host", "", "MultiAddress of host")
flag.StringVar(&config.ChannelName, "channel", "test", "Name of channel to be joined.")
flag.StringVar(&config.ChannelConfigFile, "config", "", "Channel config file.")
flag.StringVar(&config.Username, "user", timestamp, "Username.")
flag.Parse()
if len(config.BootstrapPeers) == 0 {
bootstrapPeerAddrs, err := StringsToAddrs(defaultBootstrapAddrStrings)
if err != nil {
return config, err
}
config.BootstrapPeers = bootstrapPeerAddrs
}
return config, nil
}