1 Star 0 Fork 0

庞飞/multiapp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
controller.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
庞飞 提交于 2023-10-03 17:10 . 合并主框架
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pangxianfei/multiapp.git
git@gitee.com:pangxianfei/multiapp.git
pangxianfei
multiapp
multiapp
v1.1.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385