forked from GoAdminGroup/go-admin
/
cli.go
201 lines (163 loc) · 5.65 KB
/
cli.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
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"runtime"
"runtime/debug"
_ "github.com/backyio/go-admin/modules/db/drivers/mysql"
_ "github.com/backyio/go-admin/modules/db/drivers/postgres"
cli "github.com/jawher/mow.cli"
"github.com/mgutz/ansi"
)
func main() {
var verbose *bool
defer func() {
if err := recover(); err != nil {
if errs, ok := err.(error); ok {
fmt.Println()
if runtime.GOOS == "windows" && errs.Error() == "Incorrect function." {
fmt.Println(ansi.Color(getWord("GoAdmin CLI error: CLI has not supported MINGW64 for now, "+
"please use cmd terminal instead."), "red"))
fmt.Println(getWord("Know more here: http://discuss.go-admin.com/t/goadmin-cli-adm-does-not-support-git-bash-mingw64-for-now/77"))
} else {
fmt.Println(ansi.Color("GoAdmin CLI error: "+errs.Error(), "red"))
if *verbose {
fmt.Println(string(debug.Stack()))
}
}
fmt.Println()
}
}
}()
app := cli.App("adm", "GoAdmin CLI tool for developing and generating")
app.Spec = "[-v]"
verbose = app.BoolOpt("v verbose", false, "debug info output")
// quiet
app.Command("-V version", "display this application version", func(cmd *cli.Cmd) {
cmd.Action = func() {
cliInfo()
}
})
app.Command("combine", "combine assets", func(cmd *cli.Cmd) {
cmd.Command("css", "combine css assets", func(cmd *cli.Cmd) {
var (
rootPath = cmd.StringOpt("s src", "./resource/assets/src/css/combine/", "css src path")
outputPath = cmd.StringOpt("d dist", "./resource/assets/dist/css/all.min.css", "css output path")
hash = cmd.BoolOpt("h hash", false, "add hash tag to file name")
)
cmd.Action = func() {
cssMinifier(*rootPath, *outputPath, *hash)
}
})
cmd.Command("js", "combine js assets", func(cmd *cli.Cmd) {
var (
rootPath = cmd.StringOpt("s src", "./resource/assets/src/js/combine/", "js src path")
outputPath = cmd.StringOpt("d dist", "./resource/assets/dist/js/all.min.js", "js output path")
hash = cmd.BoolOpt("h hash", false, "add hash tag to file name")
)
cmd.Action = func() {
jsMinifier(*rootPath, *outputPath, *hash)
}
})
})
app.Command("compile", "compile template files or assets to one go file", func(cmd *cli.Cmd) {
cmd.Command("tpl", "compile template files", func(cmd *cli.Cmd) {
var (
rootPath = cmd.StringOpt("s src", "./resource/pages/", "template files src path")
outputPath = cmd.StringOpt("d dist", "./template.go", "compile file output path")
packageName = cmd.StringOpt("p package", "newTmplTheme", "the package name")
varName = cmd.StringOpt("v var", "TemplateList", "the variable name")
)
cmd.Action = func() {
compileTmpl(*rootPath, *outputPath, *packageName, *varName)
}
})
cmd.Command("asset", "compile assets", func(cmd *cli.Cmd) {
var (
rootPath = cmd.StringOpt("s src", "./resource/assets/dist/", "assets root path")
outputPath = cmd.StringOpt("d dist", "./resource/", "compile file output path")
packageName = cmd.StringOpt("p package", "resource", "package name of the output golang file")
)
cmd.Action = func() {
compileAsset(*rootPath, *outputPath, *packageName)
}
})
})
app.Command("develop", "commands for developing", func(cmd *cli.Cmd) {
cmd.Command("tpl", "generate a theme project from a remote template", func(cmd *cli.Cmd) {
var (
moduleName = cmd.StringOpt("m module", "", "the module path of your theme")
themeName = cmd.StringOpt("n name", "newTmplTheme", "the name of your theme")
)
cmd.Action = func() {
getThemeTemplate(*moduleName, *themeName)
}
})
cmd.Command("plug", "initialize a plugin project", func(cmd *cli.Cmd) {
var (
moduleName = cmd.StringOpt("m module", "", "the module path of your plugin")
themeName = cmd.StringOpt("n name", "", "the name of your plugin")
)
cmd.Action = func() {
getPluginTemplate(*moduleName, *themeName)
}
})
})
app.Command("generate", "generate table model files", func(cmd *cli.Cmd) {
var (
config = cmd.StringOpt("c config", "", "config ini path")
lang = cmd.StringOpt("l language", "en", "language")
conn = cmd.StringOpt("conn connection", "", "connection")
)
cmd.Action = func() {
setDefaultLangSet(*lang)
generating(*config, *conn)
}
})
app.Command("init", "generate a template project", func(cmd *cli.Cmd) {
var (
config = cmd.StringOpt("c config", "", "config ini path")
lang = cmd.StringOpt("l language", "en", "language")
)
cmd.Action = func() {
setDefaultLangSet(*lang)
buildProject(*config)
}
cmd.Command("web", "generate a template project", func(cmd *cli.Cmd) {
var (
lang = cmd.StringOpt("l language", "en", "language")
port = cmd.StringOpt("p port", "6633", "port")
)
cmd.Action = func() {
setDefaultLangSet(*lang)
buildProjectWeb(*port)
}
})
})
app.Command("add", "generate user/permission/roles", func(cmd *cli.Cmd) {
cmd.Command("user", "generate users", func(cmd *cli.Cmd) {
var (
config = cmd.StringOpt("c config", "", "config ini path")
lang = cmd.StringOpt("l language", "en", "language")
)
cmd.Action = func() {
setDefaultLangSet(*lang)
addUser(*config)
}
})
cmd.Command("permission", "generate permissions of table", func(cmd *cli.Cmd) {
var (
config = cmd.StringOpt("c config", "", "config ini path")
lang = cmd.StringOpt("l language", "en", "language")
)
cmd.Action = func() {
setDefaultLangSet(*lang)
addPermission(*config)
}
})
})
_ = app.Run(os.Args)
}