-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpims-oc.go
244 lines (219 loc) · 6.13 KB
/
pims-oc.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"os"
"gopkg.in/urfave/cli.v2" // imports as package "cli"
"log"
"encoding/csv"
"io"
"fmt"
"strconv"
"strings"
"gopkg.in/yaml.v2"
"github.com/gosuri/uitable"
)
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
type Product struct {
Category string
ID string
Desc string
Qty int
Price float64
Warning []string
Image string
}
// how to read a csv
// http://stackoverflow.com/q/26437796/4126114
func readPims(filename string) *map[string]Product {
//log.Println("Reading csv file: ", filename)
handle, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
reader := csv.NewReader(handle)
data := map[string]Product{}
current := ""
for i := 0; i < 10000; i++ { // 10k lines max
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
// skip header
if i==0 {
continue
}
// drop empty fields
var filtered []string
for _, field := range record {
if field!="" {
filtered = append(filtered,field)
}
}
// category
if len(filtered)==1 {
current = filtered[0]
continue
}
if len(filtered)==5 {
if strings.Contains(filtered[2]," Pcs") {
qty,err := strconv.Atoi(strings.Replace(filtered[2]," Pcs","",1))
if err!= nil {
log.Fatalf("Invalid qty field found in %s / %s: %s",current, filtered[0], filtered[2])
}
price,err := strconv.ParseFloat(filtered[3],32)
if err!= nil {
log.Fatalf("Invalid price field found in %s / %s: %s",current, filtered[0], filtered[2])
}
pr := Product{Category: current, ID: filtered[0], Desc: filtered[1], Qty: qty, Price: price}
data[pr.ID] = pr
continue
}
}
if len(filtered)==0 {
continue
}
fmt.Printf("Unparsed Line %d (%d): %s\n", i, len(filtered), filtered)
//for _, field := range filtered {
// fmt.Println(field)
//}
}
//log.Println("Done Reading csv file: ", filename)
return &data
}
func readOc(dsn string) *map[string]Product {
// log.Printf("Reading opencart data from: %s\n", dsn)
db, err := sql.Open("mysql", dsn) // "root:password@/opencart"
if err != nil {
log.Fatal("connect fails:", err)
}
defer db.Close()
// Open doesn't open a connection. Validate DSN data:
// https://github.com/go-sql-driver/mysql/wiki/Examples#a-word-on-sqlopen
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// http://go-database-sql.org/retrieving.html
data := map[string]Product{}
rows, err := db.Query(`
select
model,
price,
quantity,
IFNULL(ocko_category_description.name,'No category') as category,
pd.name as description,
image
from ocko_product
left join ocko_product_to_category p2c
on p2c.product_id = ocko_product.product_id
left join ocko_category_description
on ocko_category_description.category_id = p2c.category_id
left join ocko_product_description pd
on pd.product_id = ocko_product.product_id
`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
pr := Product{}
err := rows.Scan(&pr.ID,&pr.Price,&pr.Qty,&pr.Category,&pr.Desc,&pr.Image)
if err != nil {
log.Fatal(err)
}
if pr.Category=="No category" {
pr.Warning = append(pr.Warning,"No category")
}
if pr.Image=="" {
pr.Warning = append(pr.Warning,"No image")
}
data[pr.ID] = pr
// log.Println(pr.ID)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
return &data
}
func printYaml(data *map[string]Product) {
// Golang - How to print struct variables in console?
// http://stackoverflow.com/a/24512194/4126114
// fmt.Printf("%+v\n", data)
// convert to yaml
// https://github.com/go-yaml/yaml#example
// Note:
// struct fields are important to be uppercased
// documented here: https://godoc.org/gopkg.in/yaml.v2#Marshal
y, err := yaml.Marshal(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(y))
}
// https://github.com/gosuri/uitable/blob/master/example/main.go
func printTable(data *map[string]Product) {
table := uitable.New()
table.MaxColWidth = 50
table.AddRow("Category", "ID", "Description", "Qty", "Price", "Image", "Warning")
for _, pr := range *data {
table.AddRow(pr.Category, pr.ID, pr.Desc, pr.Qty, pr.Price, pr.Image, strings.Join(pr.Warning,", "))
}
fmt.Println(table)
}
func main() {
app := &cli.App{
Commands: []*cli.Command{
{
Name: "read:pims",
Usage: "Read pims csv and display as yml",
ArgsUsage: "csvFile",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
log.Fatal("No csv file passed")
}
csvFile := c.Args().First() // Get(0)
data := readPims(csvFile)
printTable(data) // Yaml
return nil
},
},
{
Name: "read:oc",
Usage: "Read data from opencart mysql database",
ArgsUsage: "dsn",
Flags: []cli.Flag {
&cli.BoolFlag{
Name: "warnings",
Value: false,
Usage: "Filter result only for warnings",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
log.Fatal("No DSN passed")
}
dsn := c.Args().First() // Get(0)
data := readOc(dsn)
if c.Bool("warnings") {
filtered := map[string]Product{}
for _, pr := range *data {
if len(pr.Warning)>0 {
filtered[pr.ID]=pr
}
}
data=&filtered
}
printTable(data) // printYaml
return nil
},
},
},
}
app.Name = "pims-oc"
app.Version = "0.0.2.0"
app.Run(os.Args)
}