forked from astaxie/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateapp.go
169 lines (147 loc) · 3.92 KB
/
createapp.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
package main
import (
"fmt"
"os"
"path"
"strings"
)
var cmdCreate = &Command{
UsageLine: "create [appname]",
Short: "create an application base on beego framework",
Long: `
create an application base on beego framework
In the current path, will create a folder named [appname]
In the appname folder has the follow struct:
|- main.go
|- conf
|- app.conf
|- controllers
|- default.go
|- models
|- static
|- js
|- css
|- img
|- views
index.tpl
`,
}
func init() {
cmdCreate.Run = createapp
}
func createapp(cmd *Command, args []string) {
crupath, _ := os.Getwd()
if len(args) != 1 {
fmt.Println("error args")
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
fmt.Println("you should set GOPATH in the env")
os.Exit(2)
}
haspath := false
if crupath != path.Join(gopath, "src") {
wgopath := strings.Split(gopath, ";")
if len(wgopath) >= 1 {
for _, wg := range wgopath {
wg = wg + `\src`
if crupath == wg {
haspath = true
break
}
}
}
if !haspath {
lgopath := strings.Split(gopath, ":")
if len(lgopath) >= 1 {
for _, wg := range lgopath {
if crupath == path.Join(wg, "src") {
haspath = true
break
}
}
}
}
} else {
haspath = true
}
if !haspath {
fmt.Println("can't create application outside of GOPATH")
fmt.Println("you first should `cd $GOPATH/src` then use create")
os.Exit(2)
}
apppath := path.Join(crupath, args[0])
os.Mkdir(apppath, 0755)
fmt.Println("create app folder:", apppath)
os.Mkdir(path.Join(apppath, "conf"), 0755)
fmt.Println("create conf:", path.Join(apppath, "conf"))
os.Mkdir(path.Join(apppath, "controllers"), 0755)
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
os.Mkdir(path.Join(apppath, "models"), 0755)
fmt.Println("create models:", path.Join(apppath, "models"))
os.Mkdir(path.Join(apppath, "static"), 0755)
fmt.Println("create static:", path.Join(apppath, "static"))
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
fmt.Println("create static js:", path.Join(apppath, "static", "js"))
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
fmt.Println("create static css:", path.Join(apppath, "static", "css"))
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
fmt.Println("create static img:", path.Join(apppath, "static", "img"))
fmt.Println("create views:", path.Join(apppath, "views"))
os.Mkdir(path.Join(apppath, "views"), 0755)
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
fmt.Println("create views index.tpl:", path.Join(apppath, "views", "index.tpl"))
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", args[0], -1))
}
var appconf = `
appname = {{.Appname}}
httpport = 8080
runmode = dev
`
var maingo = `package main
import (
"{{.Appname}}/controllers"
"github.com/astaxie/beego"
)
func main() {
beego.Router("/", &controllers.MainController{})
beego.Run()
}
`
var controllers = `package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) Get() {
this.Data["Username"] = "astaxie"
this.Data["Email"] = "[email protected]"
this.TplNames = "index.tpl"
}
`
var indextpl = `<!DOCTYPE html>
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
<h1>Hello, world!{{.Username}},{{.Email}}</h1>
</body>
</html>
`
func writetofile(filename, content string) {
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString(content)
}