-
Notifications
You must be signed in to change notification settings - Fork 23
/
result.go
70 lines (54 loc) · 1.28 KB
/
result.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
package sonar
import (
"fmt"
"sort"
"strings"
"sync"
)
type Result struct {
Domain string `json:"domain"`
Addrs []string `json:"addrs"`
}
func (r Result) String() string {
return fmt.Sprintf("%-50s %s", r.Domain, strings.Join(r.Addrs, ", "))
}
func NewResultSet() *ResultSet {
return &ResultSet{
results: make(map[string]map[string]struct{}),
}
}
type ResultSet struct {
mu sync.Mutex
results map[string]map[string]struct{}
}
func (rs *ResultSet) Add(domain, address string) {
rs.mu.Lock()
if _, ok := rs.results[domain]; !ok {
rs.results[domain] = make(map[string]struct{})
}
rs.results[domain][address] = struct{}{}
rs.mu.Unlock()
}
func (rs *ResultSet) Results() Results {
rs.mu.Lock()
results := make(Results, 0)
for domain, addresses := range rs.results {
result := Result{Domain: domain, Addrs: make([]string, 0)}
for address, _ := range addresses {
result.Addrs = append(result.Addrs, address)
}
results = append(results, result)
}
sort.Sort(results)
rs.mu.Unlock()
return results
}
type Results []Result
func (r Results) Len() int { return len(r) }
func (r Results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Results) Less(i, j int) bool {
if strings.Compare(r[i].Domain, r[j].Domain) < 0 {
return true
}
return false
}