forked from eyjian/mooon-district
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (104 loc) · 3.5 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
// Wrote by yijian on 2024/03/09
package main
import (
"context"
"flag"
"fmt"
"github.com/eyjian/mooon-district/district"
"os"
)
var (
help = flag.Bool("h", false, "Display a help message and exit.")
version = flag.Bool("v", false, "Display version info and exit.")
districtDataFile = flag.String("f", "", "Path to the district data file (e.g., -f=district-2022.csv).")
withJson = flag.Bool("with-json", false, "Whether to generate json format data.")
withJsonIndent = flag.Bool("with-json-indent", true, "Whether JSON format is indented.")
jsonIndent = flag.String("json-indent", " ", "Json indent when -with-json-indent is enabled.")
jsonPrefix = flag.String("json-prefix", "", "Prefix for each line when -with-json-indent is enabled.")
withCsv = flag.Bool("with-csv", false, "Whether to generate csv format data.")
csvDelimiter = flag.String("csv-delimiter", ",", "Delimiter of csv data.")
csvWithCode = flag.Bool("csv-with-code", true, "Whether the csv format outputs the code column.")
withSql = flag.Bool("with-sql", false, "Whether to generate sql data.")
withSqlIgnore = flag.Bool("with-sql-ignore", false, "Use `INSERT IGNORE` to ignore existing.")
sqlTable = flag.String("sql-table", "t_dict_district", "Table name for sql data.")
withXlsx = flag.Bool("with-xlsx", false, "Whether to generate xlsx data.")
)
var (
buildTime string // build time
)
func main() {
flag.Parse()
if *help {
usage()
os.Exit(1)
}
if *version {
showVersion()
os.Exit(1)
}
if !checkParameters() {
os.Exit(1)
}
ctx := context.Background()
districtTable, err := district.LoadDistrict(ctx, *districtDataFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Load district error: %s.\n", err.Error())
os.Exit(2)
}
done := false
if *withJson {
done = true
err := district.GenerateJson(districtTable, "example.json", *withJsonIndent, *jsonIndent, *jsonPrefix)
if err != nil {
fmt.Fprintf(os.Stderr, "Generate json error: %s.\n", err.Error())
os.Exit(3)
}
}
if *withCsv {
done = true
err := district.GenerateCsv(districtTable, "example.csv", *csvDelimiter, *csvWithCode)
if err != nil {
fmt.Fprintf(os.Stderr, "Generate csv error: %s.\n", err.Error())
os.Exit(3)
}
}
if *withSql {
done = true
err := district.GenerateSql(districtTable, "example.sql", *sqlTable, *withSqlIgnore)
if err != nil {
fmt.Fprintf(os.Stderr, "Generate sql error: %s.\n", err.Error())
os.Exit(3)
}
}
if *withXlsx {
done = true
err := district.GenerateXlsx(districtTable, "example.xlsx")
if err != nil {
fmt.Fprintf(os.Stderr, "Generate xlsx error: %s.\n", err.Error())
os.Exit(3)
}
}
if !done {
fmt.Fprintf(os.Stderr, "Do nothing.\n")
os.Exit(4)
}
}
func usage() {
flag.Usage()
}
func showVersion() {
fmt.Printf("Version: %s, build at %s\n", "v0.0.1", buildTime)
}
func checkParameters() bool {
if len(*districtDataFile) == 0 {
fmt.Fprintf(os.Stderr, "Parameter -f is not set.\n")
return false
}
if *withSql {
if len(*sqlTable) == 0 {
fmt.Fprintf(os.Stderr, "Parameter -sql-table is not set.\n")
return false
}
}
return true
}