-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
76 lines (62 loc) · 1.44 KB
/
csv.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
package csv
import (
"errors"
"io"
"os"
"path/filepath"
)
// New initialize CSV backend, config is option, the last one will be used if there are more than one configs
func New(value interface{}, config ...Config) *CSV {
csv := &CSV{}
if f, ok := value.(string); ok {
csv.filename = f
} else {
if r, ok := value.(io.ReadCloser); ok {
csv.reader = r
}
if w, ok := value.(io.WriteCloser); ok {
csv.writer = w
}
}
for _, cfg := range config {
csv.config = cfg
}
return csv
}
// Config CSV exchange config
type Config struct {
TrimSpace bool
}
// CSV CSV struct
type CSV struct {
config Config
records [][]string
filename string
reader io.ReadCloser
writer io.WriteCloser
}
func (c *CSV) getReader() (io.ReadCloser, error) {
if c.reader != nil {
return c.reader, nil
} else if c.filename != "" {
if isXLSXFile(c.filename) {
return generateCSVFromXLSXFile(c.filename)
}
readerCloser, err := os.Open(c.filename)
return readerCloser, err
}
return nil, errors.New("Nothing available to import")
}
func (c *CSV) getWriter() (io.WriteCloser, error) {
if c.writer != nil {
return c.writer, nil
} else if c.filename != "" {
dir := filepath.Dir(c.filename)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
}
writerCloser, err := os.OpenFile(c.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
return writerCloser, err
}
return nil, errors.New("Nowhere to export")
}