forked from gogf/gf-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
219 lines (204 loc) · 5.33 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
209
210
211
212
213
214
215
216
217
218
219
package main
import (
_ "github.com/gogf/gf-cli/boot"
"fmt"
"github.com/gogf/gf-cli/command/build"
"github.com/gogf/gf-cli/command/docker"
"github.com/gogf/gf-cli/command/env"
"github.com/gogf/gf-cli/command/fix"
"github.com/gogf/gf-cli/command/gen"
"github.com/gogf/gf-cli/command/get"
"github.com/gogf/gf-cli/command/initialize"
"github.com/gogf/gf-cli/command/install"
"github.com/gogf/gf-cli/command/mod"
"github.com/gogf/gf-cli/command/pack"
"github.com/gogf/gf-cli/command/run"
"github.com/gogf/gf-cli/command/swagger"
"github.com/gogf/gf-cli/command/update"
"github.com/gogf/gf-cli/library/allyes"
"github.com/gogf/gf-cli/library/mlog"
"github.com/gogf/gf-cli/library/proxy"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/os/gbuild"
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
"strings"
)
const (
VERSION = "v1.17.0"
)
func init() {
// Automatically sets the golang proxy for all commands.
proxy.AutoSet()
}
var (
helpContent = gstr.TrimLeft(`
USAGE
gf COMMAND [ARGUMENT] [OPTION]
COMMAND
env show current Golang environment variables
get install or update GF to system in default...
gen automatically generate go files for ORM models...
mod extra features for go modules...
run running go codes with hot-compiled-like feature...
init create and initialize an empty GF project...
help show more information about a specified command
pack packing any file/directory to a resource file, or a go file...
build cross-building go project for lots of platforms...
docker create a docker image for current GF project...
swagger swagger feature for current project...
update update current gf binary to latest one (might need root/admin permission)
install install gf binary to system (might need root/admin permission)
version show current binary version info
OPTION
-y all yes for all command without prompt ask
-?,-h show this help or detail for specified command
-v,-i show version information
-debug show internal detailed debugging information
ADDITIONAL
Use 'gf help COMMAND' or 'gf COMMAND -h' for detail about a command, which has '...'
in the tail of their comments.
`)
)
func main() {
defer func() {
if exception := recover(); exception != nil {
if err, ok := exception.(error); ok {
mlog.Print(gerror.Current(err).Error())
} else {
panic(exception)
}
}
}()
allyes.Init()
command := gcmd.GetArg(1)
// Help information
if gcmd.ContainsOpt("h") && command != "" {
help(command)
return
}
switch command {
case "help":
help(gcmd.GetArg(2))
case "version":
version()
case "env":
env.Run()
case "get":
get.Run()
case "gen":
gen.Run()
case "fix":
fix.Run()
case "mod":
mod.Run()
case "init":
initialize.Run()
case "pack":
pack.Run()
case "docker":
docker.Run()
case "swagger":
swagger.Run()
case "update":
update.Run()
case "install":
install.Run()
case "build":
build.Run()
case "run":
run.Run()
default:
for k := range gcmd.GetOptAll() {
switch k {
case "?", "h":
mlog.Print(helpContent)
return
case "i", "v":
version()
return
}
}
// No argument or option, do installation checks.
if !install.IsInstalled() {
mlog.Print("hi, it seams it's the first time you installing gf cli.")
s := gcmd.Scanf("do you want to install gf binary to your system? [y/n]: ")
if strings.EqualFold(s, "y") {
install.Run()
gcmd.Scan("press `Enter` to exit...")
return
}
}
mlog.Print(helpContent)
}
}
// help shows more information for specified command.
func help(command string) {
switch command {
case "get":
get.Help()
case "gen":
gen.Help()
case "init":
initialize.Help()
case "docker":
docker.Help()
case "swagger":
swagger.Help()
case "build":
build.Help()
case "pack":
pack.Help()
case "run":
run.Help()
case "mod":
mod.Help()
default:
mlog.Print(helpContent)
}
}
// version prints the version information of the cli tool.
func version() {
info := gbuild.Info()
if info["git"] == "" {
info["git"] = "none"
}
mlog.Printf(`GoFrame CLI Tool %s, https://goframe.org`, VERSION)
gfVersion, err := getGFVersionOfCurrentProject()
if err != nil {
gfVersion = err.Error()
} else {
gfVersion = gfVersion + " in current go.mod"
}
mlog.Printf(`GoFrame Version: %s`, gfVersion)
mlog.Printf(`CLI Installed At: %s`, gfile.SelfPath())
if info["gf"] == "" {
mlog.Print(`Current is a custom installed version, no installation information.`)
return
}
mlog.Print(gstr.Trim(fmt.Sprintf(`
CLI Built Detail:
Go Version: %s
GF Version: %s
Git Commit: %s
Build Time: %s
`, info["go"], info["gf"], info["git"], info["time"])))
}
// getGFVersionOfCurrentProject checks and returns the GoFrame version current project using.
func getGFVersionOfCurrentProject() (string, error) {
goModPath := gfile.Join(gfile.Pwd(), "go.mod")
if gfile.Exists(goModPath) {
match, err := gregex.MatchString(`github.com/gogf/gf\s+([\w\d\.]+)`, gfile.GetContents(goModPath))
if err != nil {
return "", err
}
if len(match) > 1 {
return match[1], nil
}
return "", gerror.New("cannot find goframe requirement in go.mod")
} else {
return "", gerror.New("cannot find go.mod")
}
}