-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoutput.go
70 lines (65 loc) · 1.24 KB
/
output.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 main
import (
"fmt"
"log"
"os"
"path/filepath"
"sort"
"text/template"
)
func saveData(
classedData map[int]map[string]AddressData,
path string,
tmpl *template.Template,
addressbook map[string]string,
addUnmatched bool,
) {
var f *os.File
var err error
isstdout := path == "-"
if isstdout {
f = os.Stdout
} else {
os.MkdirAll(filepath.Dir(path), os.ModePerm)
f, err = os.Create(path)
if err != nil {
log.Fatal(err)
}
}
defer f.Close()
type KeyValue struct {
Key string
Value AddressData
}
count := 0
for class := 2; class >= 0; class-- {
thisclass, _ := classedData[class]
s := make([]KeyValue, 0, len(thisclass))
for k, v := range thisclass {
s = append(s, KeyValue{k, v})
}
sort.SliceStable(s, func(i, j int) bool {
if s[i].Value.TotalRank == s[j].Value.TotalRank {
return s[i].Value.Address < s[j].Value.Address
} else {
return s[i].Value.TotalRank < s[j].Value.TotalRank
}
})
for _, kv := range s {
count++
tmpl.Execute(f, kv.Value)
}
}
if addUnmatched {
for ak, av := range addressbook {
aD := AddressData{}
aD.Address = ak
aD.Name = av
count++
tmpl.Execute(f, aD)
}
}
if !isstdout {
fmt.Println(count, " addresses written to ", path)
}
}