-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommand_line.go
145 lines (119 loc) · 3.4 KB
/
command_line.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
package main
import (
"os"
"path/filepath"
"github.com/urfave/cli/v2"
)
type params struct {
output string
input []string
xlsxTemplate string
sheets []string
exampleRow int
delimiter rune
}
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func initCommandLine(args []string) error {
app := cli.NewApp()
app.Suggest = true
app.EnableBashCompletion = true
app.Name = "csv2xlsx"
app.Usage = "Convert CSV data to XLSX - especially the big one. \n\n" +
"Example: \n" +
" csv2xlsx --template example/template.xlsx --sheet Sheet_1 --sheet Sheet_2 --exampleRow 2 --output result.xlsx data.csv data2.csv \n" +
" csv2xlsx.exe -t example\\template.xlsx -s Sheet_1 -s Sheet_2 -r 2 -o result.xlsx data.csv data2.csv "
app.Version = version + " built in " + date + " from commit: [" + commit + "] by " + builtBy
app.ArgsUsage = "[file or file's list with csv data]"
app.DisableSliceFlagSeparator = true
app.Flags = []cli.Flag{
&cli.StringSliceFlag{
Name: "sheets",
Aliases: []string{"s"},
Usage: "sheet `names` in the same order like csv files. If sheet with that name exists, data is inserted to this sheet. Usage: -s AA -s BB ",
},
&cli.StringFlag{
Name: "template",
Aliases: []string{"t"},
Value: "",
Usage: "`path` to xlsx file with template file",
},
&cli.StringFlag{
Name: "delimiter",
Aliases: []string{"d"},
Value: ",",
Usage: "one `letter` delimiter used in csv file",
},
&cli.IntFlag{
Name: "exampleRow",
Aliases: []string{"r"},
Value: 0,
Usage: "exampleRow `number` to use for create rows format. When '0' - not used. This exampleRow will be overwrite in result file.",
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Value: "./output.xlsx",
Usage: "path to result `xlsx file`",
},
}
app.Action = func(c *cli.Context) error {
p, err := checkAndReturnParams(c)
if err != nil {
return err
}
if err = buildXls(p); err != nil {
return cli.Exit(err.Error(), 99)
}
return nil
}
return app.Run(args)
}
func checkAndReturnParams(c *cli.Context) (*params, error) {
p := ¶ms{}
output := c.String("output")
if output == "" {
return nil, cli.Exit("Path to output file not defined", 1)
}
output, err := filepath.Abs(output)
if err != nil {
return nil, cli.Exit("Wrong path to output file", 2)
}
p.output = output
delimiter := []rune(c.String("delimiter"))
p.delimiter = delimiter[0]
//
p.input = make([]string, c.Args().Len())
for i, f := range c.Args().Slice() {
filename, err := filepath.Abs(f)
if err != nil {
return nil, cli.Exit("Wrong path to input file "+filename, 3)
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, cli.Exit("Input file does not exist ( "+filename+" )", 4)
}
p.input[i] = filename
}
//
p.exampleRow = c.Int("exampleRow")
p.sheets = c.StringSlice("sheets")
xlsxTemplate := c.String("template")
if xlsxTemplate != "" {
xlsxTemplate, err = filepath.Abs(xlsxTemplate)
if err != nil {
return nil, cli.Exit("Wrong path to template file", 5)
}
if _, err := os.Stat(xlsxTemplate); os.IsNotExist(err) {
return nil, cli.Exit("Template file does not exist ( "+xlsxTemplate+" )", 6)
}
p.xlsxTemplate = xlsxTemplate
}
if p.exampleRow != 0 && xlsxTemplate == "" {
return nil, cli.Exit("Defined `exampleRow in template` without template file", 7)
}
return p, nil
}