project init

This commit is contained in:
liangzy 2020-08-31 14:37:22 +08:00
parent 77010a08b7
commit bca3a2be1e
3 changed files with 195 additions and 1 deletions

View File

@ -9,3 +9,12 @@ A flexsible and powerful command line tool to initialize beego framework
``` ```
go get golib.gaore.com/GaoreGo/beegoinit-cmd go get golib.gaore.com/GaoreGo/beegoinit-cmd
``` ```
## How to run
```
{$GOPATH}/bin/beegoinit-cmd create
```
e.g.
```
~/go/bin/beegoinit-cmd create
```

View File

@ -1,6 +1,12 @@
package cmd package cmd
import "github.com/urfave/cli/v2" import (
"fmt"
"github.com/urfave/cli/v2"
"golib.gaore.com/GaoreGo/beegoinit-cmd/utils"
"os"
"path"
)
var gitignore = `# Created by .ignore support plugin (hsz.mobi) var gitignore = `# Created by .ignore support plugin (hsz.mobi)
### Example user template template ### Example user template template
@ -29,6 +35,164 @@ gen
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/` # vendor/`
var tree = `
.
applications
_common
api
| index.go
init.go
cli
test
conf
app.conf
main.go
runtime
logs
static
Makefile
README.md
`
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"
common "jedi.gaore.com/applications/_common/controllers"
api "jedi.gaore.com/applications/api/controllers"
"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"
_ "jedi.gaore.com/applications"
)
func main() {
defer toolbox.StopTask()
toolbox.StartTask()
beego.Run()
}
`
var indextpl = `<h1 class="animation-slide-top">400</h1>`
func New(c *cli.Context) error { func New(c *cli.Context) error {
var appPath string
var packPath string
if packPath == "." {
packPath = path.Base(appPath)
}
fmt.Println(tree)
appPath = "."
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)
os.MkdirAll(path.Join(appPath, "applications", "api", "controlelrs"), 0755)
utils.WriteToFile(path.Join(appPath, "main.go"), mainfun)
utils.WriteToFile(path.Join(appPath, "applications", "init.go"), initfun)
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)
return nil return nil
} }

21
utils/utils.go Normal file
View File

@ -0,0 +1,21 @@
package utils
import "os"
func WriteToFile(filename, content string) {
f, err := os.Create(filename)
defer CloseFile(f)
_, err = f.WriteString(content)
MustCheck(err)
}
func CloseFile(f *os.File) {
err := f.Close()
MustCheck(err)
}
func MustCheck(err error) {
if err != nil {
panic(err)
}
}