-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngos.go
91 lines (73 loc) · 1.71 KB
/
ngos.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
package ngos
import (
"fmt"
"os"
"strings"
"time"
)
// Ngos struct
type Ngos struct {
Reader Reader
Writer Writer
Args *Arguments
}
// New function return *Ngos
func New(args *Arguments) *Ngos {
csvReader := CSVReader{}
csvWriter := CSVWriter{}
return &Ngos{
Reader: csvReader,
Writer: csvWriter,
Args: args,
}
}
// Run function, will Run Ngos
func (n *Ngos) Run() {
defer elapsed("ngos")()
// read old csv file
linesOld, err := n.Reader.Read(n.Args.OldCSVFile)
if err != nil {
fmt.Printf("\033[31m%s\033[0m%s", err.Error(), "\n")
os.Exit(1)
}
// create map from old csv file
lineOldMapper := make(map[string]bool)
for _, record := range linesOld {
lineOldMapper[strings.Join(record, ",")] = true
}
// read new csv file
linesNew, err := n.Reader.Read(n.Args.NewCSVFile)
if err != nil {
fmt.Printf("\033[31m%s\033[0m%s", err.Error(), "\n")
os.Exit(1)
}
if len(linesNew) <= len(linesOld) {
fmt.Printf("\033[31m%s\033[0m%s", "new csv file should larger than old csv file", "\n")
os.Exit(1)
}
linesOut := n.compare(linesNew, lineOldMapper)
err = n.Writer.Write(linesOut, n.Args.OutputCSVFile)
if err != nil {
fmt.Printf("\033[31m%s\033[0m%s", err.Error(), "\n")
os.Exit(1)
}
}
func (n *Ngos) compare(a [][]string, b map[string]bool) [][]string {
for i := len(a) - 1; i >= 0; i-- {
if b[strings.Join(a[i], ",")] {
a = append(a[:i], a[i+1:]...)
}
}
var result [][]string
for _, v := range a {
result = append(result, v)
}
return result
}
func elapsed(w string) func() {
start := time.Now()
return func() {
fmt.Printf("\033[35m%s\033[0m%s", "finish...", "\n")
fmt.Printf("\033[35m%s %s %f %s\033[0m%s", w, "took", time.Since(start).Seconds(), "seconds", "\n")
}
}