1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 23:50:54 +00:00

bee support create controllers

This commit is contained in:
astaxie 2014-08-09 01:37:55 +08:00
parent 50dfe40b94
commit 375eb44479
4 changed files with 138 additions and 4 deletions

2
bee.go
View File

@ -25,7 +25,7 @@ import (
"strings"
)
const version = "1.1.3"
const version = "1.2.0"
type Command struct {
// Run runs the command.

14
g.go
View File

@ -30,8 +30,8 @@ bee generate model [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)
bee generate migration [filename]
generate migration file for making database schema update
bee generate controller [modelfile]
generate RESTFul controllers based on modelfile
bee generate controller [controllerfile]
generate RESTFul controllers
bee generate router [controllerfile]
generate router based on controllerfile
@ -102,6 +102,16 @@ func generateCode(cmd *Command, args []string) {
ColorLog("[HINT] Usage: bee generate migration [filename]\n")
os.Exit(2)
}
case "controller":
if len(args) == 2 {
cname := args[1]
ColorLog("[INFO] Using '%s' as controller name\n", cname)
generateController(cname, curpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate controller [filename]\n")
os.Exit(2)
}
default:
ColorLog("[ERRO] command is missing\n")
}

124
g_controllers.go Normal file
View File

@ -0,0 +1,124 @@
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import (
"os"
"path"
"strings"
)
// article
// cms/article
//
func generateController(cname, crupath string) {
p, f := path.Split(cname)
controllerName := strings.Title(f)
packageName := "controllers"
if p != "" {
i := strings.LastIndex(p[:len(p)-1], "/")
packageName = p[i+1 : len(p)-1]
}
fp := path.Join(crupath, "controllers", p)
if _, err := os.Stat(fp); os.IsNotExist(err) {
// create controller directory
if err := os.MkdirAll(fp, 0777); err != nil {
ColorLog("[ERRO] Could not create controllers directory: %s\n", err)
os.Exit(2)
}
}
fpath := path.Join(fp, strings.ToLower(controllerName)+".go")
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close()
content := strings.Replace(controllerTpl, "{{packageName}}", packageName, -1)
content = strings.Replace(content, "{{controllerName}}", controllerName, -1)
f.WriteString(content)
// gofmt generated source code
formatSourceCode(fpath)
ColorLog("[INFO] controller file generated: %s\n", fpath)
} else {
// error creating file
ColorLog("[ERRO] Could not create controller file: %s\n", err)
os.Exit(2)
}
}
var controllerTpl = `package {{packageName}}
import (
"github.com/astaxie/beego"
)
// oprations for {{controllerName}}
type {{controllerName}}Controller struct {
beego.Controller
}
// @Title Post
// @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
// @Success 200 {int} models.{{controllerName}}.Id
// @Failure 403 body is empty
// @router / [post]
func (this *{{controllerName}}Controller) Post() {
}
// @Title Get
// @Description get {{controllerName}} by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.{{controllerName}}
// @Failure 403 :id is empty
// @router /:id [get]
func (this *{{controllerName}}Controller) GetOne() {
}
// @Title Get All
// @Description get {{controllerName}}
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.{{controllerName}}
// @Failure 403
// @router / [get]
func (this *{{controllerName}}Controller) GetAll() {
}
// @Title Update
// @Description update the {{controllerName}}
// @Param id path string true "The id you want to update"
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
// @Success 200 {object} models.{{controllerName}}
// @Failure 403 :id is not int
// @router /:id [put]
func (this *{{controllerName}}Controller) Put() {
}
// @Title Delete
// @Description delete the {{controllerName}}
// @Param id path string true "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (this *{{controllerName}}Controller) Delete() {
}
`

View File

@ -35,7 +35,7 @@ func generateMigration(mname string, curpath string) {
migrationFilePath := path.Join(curpath, M_PATH)
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
// create migrations directory
if err := os.Mkdir(migrationFilePath, 0777); err != nil {
if err := os.MkdirAll(migrationFilePath, 0777); err != nil {
ColorLog("[ERRO] Could not create migration directory: %s\n", err)
os.Exit(2)
}