-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (58 loc) · 1.48 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
)
const (
usage = `Usage: fixedtocsv [-c] [-d] [-f] [-o]
Options:
-h | --help help
-c input configuration file (default: "config.json" in current directory) .
-d output delimiter (default: comma ",").
-f input file name (Required).
-o output file name (default: "output.csv" in current directory).
-csv if provided, configuration file is a CSV. (-c flag required with this option)
`
)
var (
dFlag = flag.String("d", ",", "")
cFlag = flag.String("c", "config.json", "")
fFlag = flag.String("f", "", "must specify an input file")
oFlag = flag.String("o", "output.csv", "")
csvFlag = flag.Bool("csv", false, "")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage)
}
flag.Parse()
var configFileType = jsonConfig
if *csvFlag {
configFileType = csvConfig
}
var ifp *os.File
ifp, err := os.Open(*fFlag)
if err != nil {
log.Fatalln("unable to open input file : ", err)
}
defer ifp.Close()
ofp, err := os.Create(*oFlag)
if err != nil {
log.Fatalln(err)
}
defer ofp.Close()
// read config file
confInput, err := ioutil.ReadFile(*cFlag)
if err != nil {
log.Fatalln("unable to read config file : ", err)
}
sw := newScanWriter(ifp, ofp, confInput, configFileType) // initialize scanWriter
// convert fixed width file to csv
sw.convert()
if err := sw.flush(); err != nil {
log.Fatalln("err converting file : ", err)
}
}