beegoinit-cmd/cmd/new.go

248 lines
5.4 KiB
Go
Raw Normal View History

2020-08-31 11:07:18 +08:00
package cmd
2020-08-31 14:37:22 +08:00
import (
"fmt"
2020-08-31 15:13:00 +08:00
"github.com/gookit/color"
2020-08-31 14:37:22 +08:00
"github.com/urfave/cli/v2"
"golib.gaore.com/GaoreGo/beegoinit-cmd/utils"
"os"
"path"
2020-08-31 15:13:00 +08:00
"strings"
2020-08-31 14:37:22 +08:00
)
2020-08-31 11:07:18 +08:00
2020-08-31 11:56:54 +08:00
var gitignore = `# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
/runtime
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with 'go test -c\'
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/`
2020-08-31 15:13:00 +08:00
var tree = `.
2020-08-31 14:37:22 +08:00
applications
_common
api
2020-08-31 15:13:00 +08:00
| | base.go
2020-08-31 14:37:22 +08:00
| index.go
init.go
conf
app.conf
runtime
logs
static
2020-08-31 15:13:00 +08:00
| js
| css
main.go
2020-08-31 14:37:22 +08:00
Makefile
README.md
`
2020-08-31 15:13:00 +08:00
var appconf = `appname = {{ packageName }}
httpaddr = "${CENTER_HTTP_HOST||127.0.0.1}"
httpport = "${CENTER_HTTP_PORT||8080}"
runmode ="${CENTER_RUNMODE||prod}"
copyrequestbody = true
sessionon = true
customviewdir = "views"
custommodules = "api,web,openapi,admin"
RouterCaseSensitive = false
EnableGzip = true
enablexsrf = true
xsrfkey = 61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o
xsrfexpire = 21600
`
2020-08-31 14:37:22 +08:00
var initfun = `
package applications
import (
"errors"
"github.com/astaxie/beego"
"golib.gaore.com/GaoreGo/beegoinit/response"
router "golib.gaore.com/GaoreGo/beegoinit/routers"
"golib.gaore.com/GaoreGo/grconfig"
"golib.gaore.com/GaoreGo/grlogs"
2020-08-31 15:13:00 +08:00
common "{{ packageName }}/applications/_common/controllers"
api "{{ packageName }}/applications/api/controllers"
2020-08-31 14:37:22 +08:00
"strings"
)
func init() {
router.AddController("api", new(api.IndexController))
// 通用路由处理
router.ErrorController(&common.ErrorController{})
beego.Router("/", &api.IndexController{}, "get:Index")
beego.SetStaticPath("/assets", "static")
if err := beego.AddViewPath("applications/_common/views"); err != nil {
grlogs.Error(err)
}
}
`
var indexcontroller = `
package controllers
import (
)
type IndexController struct {
baseController
}
func (this *IndexController) Index() {
this.Ctx.WriteString("ok")
this.StopRun()
}
`
var basecontroller = `
package controllers
import (
"github.com/astaxie/beego"
"golib.gaore.com/GaoreGo/beegoinit/mvc"
"golib.gaore.com/GaoreGo/grconfig"
"path"
)
type baseController struct {
Configer *grconfig.Configer
mvc.BaseController
}
func (this *baseController) NestPrepare() {
this.Configer = grconfig.New(path.Join(beego.WorkPath, "conf/"))
this.EnableXSRF = false
this.StopRender()
}
`
var errcontroller = `
package controllers
import (
"golib.gaore.com/GaoreGo/beegoinit/mvc"
"golib.gaore.com/GaoreGo/beegoinit/response"
"strings"
)
type ErrorController struct {
mvc.BaseController
}
func (this *ErrorController) NestPrepare() {
this.ViewPath = "applications/_common/views"
}
func (this *ErrorController) Error404() {
if this.IsAjax() || strings.Contains(strings.ToLower(this.Ctx.Input.Header("Accept")), "json") {
resp := response.NewJsonByDefaultFailed()
this.Data["json"] = resp
this.ServeJSON()
} else {
this.Data["content"] = "page not found"
this.TplName = "404.tpl"
}
}
`
var mainfun = `
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/toolbox"
_ "golib.gaore.com/GaoreGo/beegoinit"
2020-08-31 15:13:00 +08:00
_ "{{ packageName }}/applications"
2020-08-31 14:37:22 +08:00
)
func main() {
defer toolbox.StopTask()
toolbox.StartTask()
beego.Run()
}
`
var indextpl = `<h1 class="animation-slide-top">400</h1>`
2020-08-31 11:56:54 +08:00
func New(c *cli.Context) error {
2020-08-31 14:37:22 +08:00
var appPath string
var packPath string
2020-08-31 15:13:00 +08:00
var packageName string
var err error
2020-08-31 14:37:22 +08:00
if packPath == "." {
packPath = path.Base(appPath)
}
fmt.Println(tree)
2020-08-31 15:13:00 +08:00
appPath = c.String("apppath")
if appPath == "" {
if appPath, err = os.Getwd(); err != nil {
return err
}
}
packageName = c.String("package")
if packageName == "" {
packageName = path.Base(appPath)
}
color.Yellow.Println("Now current working directory is :", appPath)
color.Yellow.Println("Now package name is default to :", packageName)
2020-08-31 14:37:22 +08:00
os.Mkdir(path.Join(appPath, "static"), 0755)
os.Mkdir(path.Join(appPath, "conf"), 0755)
os.Mkdir(path.Join(appPath, "cli"), 0755)
os.MkdirAll(path.Join(appPath, "runtime", "logs"), 0755)
os.MkdirAll(path.Join(appPath, "applications", "_common", "controllers"), 0755)
os.MkdirAll(path.Join(appPath, "applications", "_common", "views"), 0755)
2020-08-31 15:13:00 +08:00
os.MkdirAll(path.Join(appPath, "applications", "api", "controllers"), 0755)
2020-08-31 14:37:22 +08:00
2020-08-31 15:13:00 +08:00
utils.WriteToFile(path.Join(appPath, "main1.go"), strings.ReplaceAll(mainfun, "{{ packageName }}", packageName))
utils.WriteToFile(path.Join(appPath, "applications", "init.go"), strings.ReplaceAll(initfun, "{{ packageName }}", packageName))
utils.WriteToFile(path.Join(appPath, "conf", "app.conf"), strings.ReplaceAll(appconf, "{{ packageName }}", packageName))
2020-08-31 14:37:22 +08:00
utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "404.tpl"), indextpl)
utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "403.tpl"), indextpl)
utils.WriteToFile(path.Join(appPath, "applications", "_common", "controllers", "error.go"), errcontroller)
utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "base.go"), basecontroller)
utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "index.go"), indexcontroller)
2020-08-31 15:13:00 +08:00
color.Yellow.Println("Finish ...")
2020-08-31 11:56:54 +08:00
return nil
2020-08-31 11:07:18 +08:00
}