-
Notifications
You must be signed in to change notification settings - Fork 7
/
load.go
74 lines (62 loc) · 1.22 KB
/
load.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
package main
import (
"encoding/csv"
"errors"
"fmt"
"os"
"path/filepath"
)
func loadFiles(tablename, filespath string) (err error) {
if filespath == "." || filespath == "./" {
filespath, err = os.Getwd()
if err != nil {
return err
}
}
p, err := os.Stat(filespath)
if err != nil {
return err
}
var files []string
if p.Mode().IsRegular() {
files = append(files, filespath)
} else {
err := filepath.Walk(filespath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && path[len(path)-4:] == ".csv" {
files = append(files, path)
}
return nil
})
if err != nil {
return err
}
}
if len(files) == 0 {
return errors.New("file not found")
}
for _, file := range files {
fmt.Println("- loading file", file)
records, err := loadCSV(file)
if err != nil {
return err
}
err = createTableAndInsertRecords(tablename, records)
if err != nil {
return err
}
}
columns, rows, err := tableSelect(tablename, 100)
if err != nil {
return err
}
tableRender(columns, rows)
return nil
}
func loadCSV(filename string) (records [][]string, err error) {
file, err := os.Open(filename)
if err != nil {
return
}
r := csv.NewReader(file)
return r.ReadAll()
}