/
main.go
94 lines (83 loc) · 2.76 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
// 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.")
districtDataFile = flag.String("f", "", "File that stores district data.")
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.")
sqlTable = flag.String("sql-table", "t_dict_district", "Table name for sql data.")
)
func main() {
flag.Parse()
if *help {
usage()
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)
if err != nil {
fmt.Fprintf(os.Stderr, "Generate sql 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 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
}