forked from whyrusleeping/ipfs-counter
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ma_list.go
61 lines (53 loc) · 1.1 KB
/
ma_list.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
package main
import (
"fmt"
"os"
"strings"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
)
// maList is a list of peers by id
type maList map[peer.ID]Node
// NewMAList makes a new multiaddr list
func newMAList() maList {
return make(map[peer.ID]Node)
}
func (m maList) Add(ma multiaddr.Multiaddr) error {
pi, err := peer.AddrInfoFromP2pAddr(ma)
if err != nil {
return err
}
if _, ok := m[pi.ID]; !ok {
m[pi.ID] = Node{
ID: pi.ID,
Addrs: []multiaddr.Multiaddr{ma},
}
}
return nil
}
func (m maList) AddString(ma string) error {
parsed, err := multiaddr.NewMultiaddr(ma)
if err != nil {
return err
}
return m.Add(parsed)
}
func (m maList) AddStrings(lines []string) (bool, error) {
var err error
for _, maddr := range lines {
if len(maddr) > 0 {
if e2 := m.AddString(maddr); e2 != nil {
err = fmt.Errorf("%v; %w", err, e2)
continue
}
}
}
return true, err
}
func (m maList) AddFile(f string) (bool, error) {
lines, err := os.ReadFile(f)
if err != nil {
return false, err
}
return m.AddStrings(strings.Split(string(lines), "\n"))
}