-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (197 loc) · 4.93 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
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
package main
import (
"fmt"
"github.com/urfave/cli"
"os"
"strconv"
"strings"
"./bgs"
)
const (
// 参数不够
ErrorCodeRequireArgsNum = 0
// 打开模板文件失败
ErrorCodeTemplateFilePathOpen= 1
// 条数类型错误
ErrorCodeCountType = 2
)
var (
OutputTypes = map[string]bgs.TemplateOutputSequenceFunc {
bgs.TYPE_CSV: bgs.CsvOutputSequence,
bgs.TYPE_EXCEL: bgs.ExcelOutputSequence,
}
)
func main(){
app := cli.NewApp()
app.Version = "0.0.1"
app.Name = "bgs"
app.Usage = `generate sequence form template file path`
app.HelpName = app.Name
app.Authors = []cli.Author{
{
Name: "andy",
Email: "[email protected]",
},
}
app.CustomAppHelpTemplate = getAppHelpTemplate()
app.Commands = []cli.Command{
{
Name: "csv",
Usage: "The template file path format must be CSV",
Action: csvAction,
},
{
Name: "excel",
Usage: "The template file path format must be Excel",
Action: excelAction,
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name:"prefix, p",
Value:"",
Usage: "fields prefix",
},
cli.StringFlag{
Name: "exclude-index,e",
Value: "",
Usage: `exclude field index list, excluding fields does not generate sequences(1 and 3 columns are not generate sequence example:"0,1,2")`,
},
cli.StringFlag{
Name: "start-sequence, s",
Value: "0",
Usage: "generator start sequence",
},
cli.StringFlag{
Name: "output-type, t",
Value: "csv",
Usage: "generator sequence output type",
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
// csv动作
func csvAction(ctx * cli.Context) error {
tc, err := checkOptions(ctx)
if err != nil {
return err
}
templateFilePath, count , err := checkArgs(ctx)
if err != nil {
return err
}
tc.Count = count
outputSequenceFunc, _ := OutputTypes[tc.OutputType]
file, err := os.Open(templateFilePath)
t := bgs.New(file)
t.Config = &tc
t.Reader = bgs.CsvReader
t.OutputSequence = outputSequenceFunc
t.Run()
tc.Count = count
return nil
}
// excel动作
func excelAction(ctx * cli.Context) error {
tc, err := checkOptions(ctx)
if err != nil {
return err
}
templateFilePath, count , err := checkArgs(ctx)
if err != nil {
return err
}
tc.Count = count
outputSequenceFunc, _ := OutputTypes[tc.OutputType]
file, err := os.Open(templateFilePath)
t := bgs.New(file)
t.Config = &tc
t.Reader = bgs.ExcelReader
t.OutputSequence = outputSequenceFunc
t.Run()
tc.Count = count
return nil
}
// 检测选项
func checkOptions(ctx *cli.Context) (tc bgs.TemplateConfig, err error) {
var errMessage strings.Builder
tc.Prefix = ctx.GlobalString("p")
if tc.Prefix == "" {
tc.Prefix = ctx.GlobalString("prefix")
}
excludeIndexs := ctx.GlobalString("e")
if excludeIndexs == "" {
excludeIndexs = ctx.GlobalString("exclude-index")
}
tc.ExcludeIndexs = strings.Split(excludeIndexs, ",")
tc.StartCount = ctx.GlobalUint64("s")
if tc.StartCount == 0 {
tc.StartCount = ctx.GlobalUint64("start-sequence")
}
tc.OutputType = ctx.GlobalString("t")
if tc.OutputType == "" {
tc.OutputType = ctx.GlobalString("output-type")
}
_, ok := OutputTypes[tc.OutputType]
if !ok {
errMessage.WriteString(fmt.Sprintf("%s: output type error\n", ctx.App.Name))
err = cli.NewExitError(errMessage.String(), ErrorCodeRequireArgsNum)
return
}
return
}
// 检测参数
func checkArgs(ctx *cli.Context) (templateFilePath string, count uint64, err error) {
var errMessage strings.Builder
// 检测参数错误
l := ctx.NArg()
if l < 2 {
errMessage.WriteString(fmt.Sprintf("%s: templateFilePath and count required\n", ctx.App.Name))
err = cli.NewExitError(errMessage.String(), ErrorCodeRequireArgsNum)
return
}
// 检测模板文件错误
templateFilePath = ctx.Args().Get(0)
file, err := os.Open(templateFilePath)
if err != nil {
errMessage.WriteString(fmt.Sprintf("%s: open templateFilePath fail\n", ctx.App.Name))
errMessage.WriteString(fmt.Sprintf("system error %s", err))
err = cli.NewExitError(errMessage.String(), ErrorCodeTemplateFilePathOpen)
return
}
defer file.Close()
// 检测count是否错误
count, err = strconv.ParseUint(ctx.Args().Get(1), 10, 64)
if err != nil {
errMessage.WriteString(fmt.Sprintf("%s: count type error\n", ctx.App.Name))
errMessage.WriteString(fmt.Sprintf("system error %s", err))
err = cli.NewExitError(errMessage.String(), ErrorCodeCountType)
return
}
return
}
func getAppHelpTemplate() string {
return `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command {{end}} <templateFilePath> <count>
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}{{if .Version}}
VERSION:
{{.Version}}
{{end}}
`
}