-
Notifications
You must be signed in to change notification settings - Fork 154
/
tablediff.go
120 lines (106 loc) · 2.36 KB
/
tablediff.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"github.com/influxdata/flux"
"github.com/influxdata/flux/csv"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/execute/table"
"github.com/spf13/cobra"
)
// decodeCSV will read a csv file with results into a mapping of table iterators.
func decodeCSV(name string) (map[string]flux.TableIterator, error) {
r, err := os.Open(name)
if err != nil {
return nil, err
}
dec := csv.NewMultiResultDecoder(csv.ResultDecoderConfig{})
results, err := dec.Decode(r)
if err != nil {
return nil, err
}
defer results.Release()
tables := make(map[string]flux.TableIterator)
for results.More() {
res := results.Next()
var iter table.Iterator
if err := res.Tables().Do(func(t flux.Table) error {
cpy, err := execute.CopyTable(t)
if err != nil {
return err
}
iter = append(iter, cpy)
return nil
}); err != nil {
return nil, err
}
tables[res.Name()] = iter
}
results.Release()
if err := results.Err(); err != nil {
return nil, err
}
return tables, nil
}
// prefix will prefix each line in the string with another string.
func prefix(s, prefix string) string {
if prefix == "" {
return s
}
var sb strings.Builder
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
sb.WriteString(prefix)
sb.WriteString(": ")
sb.WriteString(scanner.Text())
sb.WriteString("\n")
}
return sb.String()
}
// diff will print out a diff between each table iterator.
func diff(want, got map[string]flux.TableIterator) {
names := make([]string, 0, len(want))
for name := range want {
names = append(names, name)
}
sort.Strings(names)
for name := range got {
if i := sort.SearchStrings(names[:len(want)], name); i < len(want) && names[i] == name {
continue
}
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
diffS := table.Diff(want[name], got[name])
if len(names) > 1 {
diffS = prefix(diffS, name)
}
fmt.Println(diffS)
}
}
func runE(cmd *cobra.Command, args []string) error {
want, err := decodeCSV(args[0])
if err != nil {
return err
}
got, err := decodeCSV(args[1])
if err != nil {
return err
}
diff(want, got)
return nil
}
func main() {
cmd := &cobra.Command{
Use: "tablediff <file1> <file2>",
RunE: runE,
Args: cobra.ExactArgs(2),
}
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}