-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnerc.go
221 lines (193 loc) · 5.29 KB
/
nerc.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
package main
import (
"bufio"
"bytes"
"encoding/csv"
"errors"
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"
)
const IMAGE_URL_COL = 21
const PRODUCT_NAME_COL = 6
const PRICE_COL = 14
var purgeOnly bool
var verbose bool
var run bool
type TemplateVariable struct {
Key string `yaml:"key"`
CSVSourceCol int `yaml:"csvSourceCol"`
Type string `yaml:"type"`
}
type NercConf struct {
Input string `yaml:"input"`
Templates string `yaml:"templates"`
Output string `yaml:"output"`
Variables []TemplateVariable `yaml:"variables"`
StaticVariables map[string]interface{} `yaml:"staticVariables"`
CSVMapping map[string]int `yaml:"csvMapping"`
RunCommand string `yaml:"runCommand"`
}
func main() {
flag.BoolVar(&purgeOnly, "purge", false, "Purge all existing files from output directory and stop.")
flag.BoolVar(&verbose, "v", false, "Use verbose output.")
flag.BoolVar(&run, "run", false, "Execute runCommand (from nerc.yml) for each generated file.")
flag.Parse()
nercConf := NercConf{}
confFile, readErr := ioutil.ReadFile("nerc.yml")
if readErr != nil {
panic(readErr)
}
parseErr := yaml.Unmarshal(confFile, &nercConf)
if parseErr != nil {
panic(parseErr)
}
if nercConf.Output == "" {
nercConf.Output = "output/"
}
if _, err := os.Stat(nercConf.Input); os.IsNotExist(err) {
fmt.Println("Could not find '" + nercConf.Input + "'. Specify input file with -i=<filepath>.")
} else {
purgeOutput(nercConf, !verbose)
if purgeOnly {
fmt.Println("Purged output directory")
} else {
fmt.Println("Reading input file: " + nercConf.Input)
csvFile, _ := os.Open(nercConf.Input)
r := csv.NewReader(bufio.NewReader(csvFile))
os.Mkdir(nercConf.Output, os.ModePerm)
csvToConfigs(r, nercConf)
if run && nercConf.RunCommand != "" {
runCommandForTemplates(nercConf)
}
}
}
fmt.Println("Done")
}
func runCommandForTemplates(conf NercConf) {
var files []string
if _, err := os.Stat(conf.Output); !os.IsNotExist(err) {
err := filepath.Walk(conf.Output, visitPath(&files))
if err != nil {
panic(err)
}
for _, file := range files {
cmdArgs := strings.Fields(fmt.Sprintf(conf.RunCommand, file))
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Print(string(stdout))
}
}
}
func purgeOutput(nercConf NercConf, silent bool) {
err := os.RemoveAll(nercConf.Output)
if err != nil && !silent {
fmt.Println(err)
}
}
// process applies the data structure 'vars' onto an already
// parsed template 't', and returns the resulting string.
func process(t *template.Template, vars interface{}) string {
var tmplBytes bytes.Buffer
err := t.Execute(&tmplBytes, vars)
if err != nil {
panic(err)
}
return tmplBytes.String()
}
// ProcessFile parses the supplied filename and compiles its template
// using the given variables.
func ProcessFile(fileName string, vars interface{}) string {
tmpl, err := template.ParseFiles(fileName)
if err != nil {
panic(err)
}
return process(tmpl, vars)
}
func visitPath(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
if info.IsDir() {
return nil
}
*files = append(*files, path)
return nil
}
}
// Read given csv file and build NexRender configurations
// out of the csv and hard coded variation parameters.
func csvToConfigs(r *csv.Reader, nercConf NercConf) {
var files []string
if _, err := os.Stat(nercConf.Templates); !os.IsNotExist(err) {
err := filepath.Walk(nercConf.Templates, visitPath(&files))
if err != nil {
panic(err)
}
fmt.Println(strconv.Itoa(len(files)) + " templates found from " + nercConf.Templates)
}
configCount := 0
firstLine := true
for {
row, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if firstLine {
// Treat first line as header line and skip it
firstLine = false
} else {
for i, templateFile := range files {
writeConf(row, templateFile, i, nercConf)
configCount += 1
}
}
}
fmt.Println("Wrote " + strconv.Itoa(configCount) + " config files to " + nercConf.Output)
}
func toPriceString(price interface{}) (string, error) {
if s, err := strconv.ParseFloat(fmt.Sprintf("%v", price), 64); err == nil {
return fmt.Sprintf("%.2f", s), nil
} else {
return "", errors.New(fmt.Sprintf("Could not convert '%v' to a price string", price))
}
}
func writeConf(row []string, template string, i int, nercConf NercConf) {
templateVars := make(map[string]interface{})
for _, variable := range nercConf.Variables {
value := string(row[variable.CSVSourceCol])
if variable.Type == "price" && value != "" {
price, err := toPriceString(value)
if err != nil {
fmt.Println("Error in sku " + row[0] + ": " + err.Error())
} else {
value = price
}
}
templateVars[variable.Key] = value
}
conf := ProcessFile(template, templateVars)
outputFile := "sku_" + row[0] + "_version_" + strconv.Itoa(i) + ".json"
err := ioutil.WriteFile(path.Join(nercConf.Output, outputFile), []byte(conf), 0644)
if err != nil {
fmt.Println(err)
}
}