-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (107 loc) · 2.44 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
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
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"github.com/carlmjohnson/errors"
"github.com/carlmjohnson/flagext"
)
func main() {
enc, err := FromArgs(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Initial error: %v\n", err)
os.Exit(3)
}
if err = enc.Encode(); err != nil {
fmt.Fprintf(os.Stderr, "Run time error: %v\n", err)
os.Exit(1)
}
}
type Encoder struct {
src io.ReadCloser
dest io.WriteCloser
transformer func() error
}
func FromArgs(args []string) (*Encoder, error) {
fl := flag.NewFlagSet("csv2json", flag.ExitOnError)
src := flagext.FileOrURL(flagext.StdIO, nil)
fl.Var(src, "src", "Source `path` for file or URL")
dest := fl.String("dest", "", "Destination file (default: stdout)")
array := fl.Bool("no-headers", false, "Return each row as an array")
_ = fl.Parse(args)
e := Encoder{
src: src,
dest: os.Stdout,
}
if *dest != "" && *dest != "-" {
f, err := os.Create(*dest)
if err != nil {
return nil, err
}
e.dest = f
}
if *array {
e.transformer = e.NoHeaders
} else {
e.transformer = e.WithHeaders
}
return &e, nil
}
func (e *Encoder) Encode() error {
return e.transformer()
}
func (e *Encoder) WithHeaders() (err error) {
defer errors.Defer(&err, e.dest.Close)
data, err := makeWithHeader(e.src)
if err != nil {
return err
}
enc := json.NewEncoder(e.dest)
return enc.Encode(&data)
}
func makeWithHeader(src io.ReadCloser) (data []map[string]string, err error) {
defer errors.Defer(&err, src.Close)
cr := csv.NewReader(src)
cr.Comment = '#'
cr.FieldsPerRecord = -1
cr.ReuseRecord = true
fields, err := cr.Read()
// Save headers for each row of dict
dataHeader := make(map[int]string, len(fields))
for i, field := range fields {
dataHeader[i] = field
}
for {
fields, err = cr.Read()
if err == io.EOF {
return data, nil
}
if err != nil {
return nil, err
}
datum := make(map[string]string, len(fields))
for i, val := range fields {
datum[dataHeader[i]] = val
}
data = append(data, datum)
}
}
func (e *Encoder) NoHeaders() (err error) {
defer errors.Defer(&err, e.dest.Close)
data, err := makeWithoutHeader(e.src)
if err != nil {
return err
}
enc := json.NewEncoder(e.dest)
return enc.Encode(&data)
}
func makeWithoutHeader(src io.ReadCloser) (data [][]string, err error) {
defer errors.Defer(&err, src.Close)
cr := csv.NewReader(src)
cr.Comment = '#'
cr.FieldsPerRecord = -1
return cr.ReadAll()
}