-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
151 lines (121 loc) · 3.4 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/360EntSecGroup-Skylar/excelize"
)
type parameters struct {
excelSrc string
luaPath string
jsonPath string
cshapPath string
golangPath string
tag string
}
var params *parameters
type fileXlsx struct {
file *excelize.File
xl *xlsx
}
var loadedFiles map[string]*fileXlsx
func init() {
params = new(parameters)
flag.StringVar(¶ms.excelSrc, "p", "", "[Required] Relative `path` of excel files folder")
flag.StringVar(¶ms.tag, "tag", "", "only field with this tag or empty string will be exported")
flag.StringVar(¶ms.luaPath, "lua", "", "path to place exported .lua files, export no .lua files if parameter is missing")
flag.StringVar(¶ms.jsonPath, "json", "", "path to place exported .json files, export no .json files if parameter is missing")
flag.StringVar(¶ms.cshapPath, "csharp", "", "path to place exported .cs class files, export no .cs files if parameter is missing")
flag.StringVar(¶ms.golangPath, "golang", "", "path to place exported .go struct files, export no .go files if parameter is missing")
}
func main() {
flag.Parse()
if flag.Arg(0) == "test" {
params.excelSrc = "./excels/"
params.tag = "client"
params.luaPath = "./exports/"
params.jsonPath = "./exports/"
params.cshapPath = "./exports/"
params.golangPath = "./exports/"
} else if params.excelSrc == "" || (params.luaPath == "" && params.jsonPath == "" && params.cshapPath == "" && params.golangPath == "") || flag.Arg(0) == "help" {
fmt.Println("Usage: excelizor -p <path> [-lua=<luaExportPath>] [-json=<jsonExportPath>] [-csharp=<csharpExportPath>] [-golang=<golangExportPath>] [-tag=<tag>] ")
fmt.Println(" excelizor (help|test)")
flag.PrintDefaults()
return
}
loadedFiles = make(map[string]*fileXlsx)
err := filepath.Walk(params.excelSrc, loadFile)
if err != nil {
log.Fatalln(err)
}
for key, value := range loadedFiles {
parseExcel(key, value)
}
//x.Print()
return
}
func parseExcel(name string, excel *fileXlsx) {
output := "\nParse and export file [ " + name + " ] "
fmt.Print(output)
for i := 0; i < 60-len(output); i++ {
fmt.Print(".")
}
if excel.xl == nil {
excel.xl = parseFile(name, excel.file)
exportFile(excel.xl)
}
fmt.Print(" Success!")
}
func loadFile(path string, f os.FileInfo, err error) error {
if f == nil {
log.Fatalln(err)
}
if f.IsDir() {
return nil
}
xl, err := excelize.OpenFile(path)
if err != nil {
return nil
}
loadedFiles[f.Name()] = &fileXlsx{xl, nil}
return err
}
func parseFile(fileName string, file *excelize.File) *xlsx {
var data [][]string
sheetName := file.GetSheetName(1)
x := new(xlsx)
if sheetName == "Vertical" {
data = convertToVertical(file.GetRows(sheetName))
} else {
data = file.GetRows(sheetName)
}
x.ParentClass = ""
if strings.Contains(sheetName, "|") {
i := strings.LastIndex(sheetName, "|")
j := strings.LastIndex(sheetName, ".")
x.ParentClass = sheetName[i+1 : j]
}
lower, camel := name2lower2Camel(fileName)
x.Init(lower, camel)
x.Parse(data)
return x
}
func exportFile(x *xlsx) {
e := new(exporter)
e.Init()
if params.luaPath != "" {
e.ExportLua(params.luaPath, x)
}
if params.jsonPath != "" {
e.ExportJSON(params.jsonPath, x)
}
if params.cshapPath != "" {
e.ExportCSharp(params.cshapPath, x)
}
if params.golangPath != "" {
e.ExportGolang(params.golangPath, x)
}
}