Skip to content

Commit adb22dd

Browse files
committed
Purge output dir always
1 parent 81efb6d commit adb22dd

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

nerc.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const IMAGE_URL_COL = 21
2222
const PRODUCT_NAME_COL = 6
2323
const PRICE_COL = 14
2424

25-
var purge bool
25+
var purgeOnly bool
26+
var verbose bool
2627

2728
type TemplateVariable struct {
2829
Key string `yaml:"key"`
@@ -41,7 +42,8 @@ type NercConf struct {
4142
}
4243

4344
func main() {
44-
flag.BoolVar(&purge, "purge", false, "Purge all existing files from output directory.")
45+
flag.BoolVar(&purgeOnly, "purge", false, "Purge all existing files from output directory and stop.")
46+
flag.BoolVar(&verbose, "v", false, "Use verbose output.")
4547
flag.Parse()
4648

4749
nercConf := NercConf{}
@@ -61,24 +63,29 @@ func main() {
6163
if _, err := os.Stat(nercConf.Input); os.IsNotExist(err) {
6264
fmt.Println("Could not find '" + nercConf.Input + "'. Specify input file with -i=<filepath>.")
6365
} else {
64-
fmt.Println("Reading input file: " + nercConf.Input)
65-
csvFile, _ := os.Open(nercConf.Input)
66-
r := csv.NewReader(bufio.NewReader(csvFile))
66+
purgeOutput(nercConf, !verbose)
6767

68-
if purge {
69-
fmt.Println("Purging output directory...")
70-
err := os.RemoveAll(nercConf.Output)
71-
if err != nil {
72-
fmt.Println(err)
73-
}
68+
if purgeOnly {
69+
fmt.Println("Purged output directory")
70+
} else {
71+
fmt.Println("Reading input file: " + nercConf.Input)
72+
csvFile, _ := os.Open(nercConf.Input)
73+
r := csv.NewReader(bufio.NewReader(csvFile))
74+
os.Mkdir(nercConf.Output, os.ModePerm)
75+
csvToConfigs(r, nercConf)
7476
}
75-
os.Mkdir(nercConf.Output, os.ModePerm)
76-
csvToConfigs(r, nercConf)
7777
}
7878

7979
fmt.Println("Done")
8080
}
8181

82+
func purgeOutput(nercConf NercConf, silent bool) {
83+
err := os.RemoveAll(nercConf.Output)
84+
if err != nil && !silent {
85+
fmt.Println(err)
86+
}
87+
}
88+
8289
// process applies the data structure 'vars' onto an already
8390
// parsed template 't', and returns the resulting string.
8491
func process(t *template.Template, vars interface{}) string {

0 commit comments

Comments
 (0)