-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (73 loc) · 1.41 KB
/
main.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
package main
import (
"encoding/csv"
"log"
"os"
"path/filepath"
"strconv"
)
func withFile(path string, fn func(*os.File) error) error {
fh, err := os.Open(path)
if err != nil {
return err
}
defer fh.Close()
return fn(fh)
}
type fileData struct {
name string
entries []string
}
func writeTable(data []fileData, writeRow func([]string) error) error {
row := make([]string, 1, len(data)+1)
row[0] = "Index"
longest := 0
for _, fd := range data {
row = append(row, fd.name)
if len(fd.entries) > longest {
longest = len(fd.entries)
}
}
if err := writeRow(row); err != nil {
return err
}
for i := 0; i < longest; i++ {
row = make([]string, 1, len(data)+1)
row[0] = strconv.Itoa(i)
for _, fd := range data {
if i < len(fd.entries) {
row = append(row, fd.entries[i])
} else {
row = append(row, "")
}
}
if err := writeRow(row); err != nil {
return err
}
}
return nil
}
func main() {
data := []fileData{}
for _, path := range os.Args[1:] {
fd := fileData{
name: filepath.Base(path),
}
if err := withFile(path, func(fh *os.File) error {
var err error
fd.entries, err = parseLangFile(fh)
return err
}); err != nil {
log.Fatal(err)
}
data = append(data, fd)
}
w := csv.NewWriter(os.Stdout)
if err := writeTable(data, w.Write); err != nil {
log.Fatal(err)
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}