代码拉取完成,页面将自动刷新
package controller
import (
"fmt"
"os"
"strings"
"gitee.com/pangxianfei/multiapp/config"
"gitee.com/pangxianfei/multiapp/cmd"
)
func init() {
cmd.Add(&Controller{})
}
type Controller struct {
}
func (s *Controller) Command() string {
return "controller:make {appname} {name}"
}
func (s *Controller) Description() string {
return "Create api controller,exmaple: make controller v1/user"
}
func (s *Controller) Handler(arg *cmd.Arg) error {
appname, err := arg.Get("appname")
name, err := arg.Get("name")
if err != nil {
cmd.Exit(err.Error())
return err
}
// 处理参数,要求附带 API 版本(v1 或者 v2)
array := strings.Split(*name, "/")
if len(array) != 2 {
cmd.Exit("api controller name format: v1/user")
}
newAppName := string(*appname)
newName := string(array[1])
var AppPath string = config.GetString("app.app_path")
var CreateModelPath string = "%s/%s/models/%s/"
var stubsModelPath string = fmt.Sprintf("%s/make/stubs/model/", config.GetString("app.make_stubs"))
var stubsControllerPath string = fmt.Sprintf("%s/make/stubs/controller", config.GetString("app.make_stubs"))
createModel := cmd.Model{}
Models := createModel.MakeModelFromString(newAppName, newName)
dir := fmt.Sprintf(CreateModelPath, AppPath, newAppName, Models.PackageName)
_ = os.MkdirAll(dir, os.ModePerm)
// 替换变量
createModel.CreateFileFromStub(dir+Models.ModelFileName+"Model.go", stubsModelPath+"/model", Models)
createModel.CreateFileFromStub(dir+Models.ModelFileName+"Util.go", stubsModelPath+"/model_util", Models)
createModel.CreateFileFromStub(dir+Models.ModelFileName+"Hooks.go", stubsModelPath+"/model_hooks", Models)
// 控制器版本号
apiVersion := array[0]
// 组建目标目录
filePath := fmt.Sprintf("%s/%s/http/controllers/%s/%sController.go", AppPath, newAppName, apiVersion, Models.StructName)
// 基于模板创建文件(做好变量替换)
createModel.CreateFileFromStub(filePath, stubsControllerPath, Models)
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。