This commit is contained in:
henry
2021-11-02 09:43:19 +08:00
parent 570bb3c772
commit 4734344985
78 changed files with 4798 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package model
import (
"ArmedPolice/utils"
"bytes"
"fmt"
"go/format"
"text/template"
)
const ModelTemplate = `package {{.Name}}
type {{.StrutName}} struct {
Model
ModelDeleted
ModelAt
}
func (m *{{.StrutName}}) TableName() string {
return "{{.TableName}}"
}
func New{{.StrutName}}() *{{.StrutName}} {
return &{{.StrutName}}{}
}`
type ModelFile struct {
// Name is the plugin name. Snack style.
Name string
// StrutName is the struct name.
StrutName string
// TableName is the model table name.
TableName string
}
func (v *ModelFile) Execute(file, tmplName, tmpl string) error {
fmt.Println(file)
f, err := utils.Open(file)
if err != nil {
return err
}
defer f.Close()
temp := new(template.Template)
if temp, err = template.New(tmplName).Parse(tmpl); err != nil {
return err
}
buf := new(bytes.Buffer)
if err = temp.Execute(buf, v); err != nil {
return err
}
out := make([]byte, 0)
if out, err = format.Source(buf.Bytes()); err != nil {
return err
}
_, err = f.Write(out)
return err
}

View File

@ -0,0 +1,73 @@
package model
import (
"Edu/config"
"Edu/serve/logger"
"Edu/utils"
"fmt"
"os"
"path"
"strings"
"github.com/spf13/cobra"
)
// ModelCommand
var ModelCommand = &cobra.Command{
Use: "make:model",
Short: "quick make model",
Example: "ctl make:model -f sys_user -t sys_user",
Version: *config.Version,
}
var (
file string
tableName string
)
func init() {
ModelCommand.Flags().StringVarP(&file, "file", "f", "", "The file name.")
ModelCommand.Flags().StringVarP(&tableName, "tableName", "t", "", "The file model tableName")
ModelCommand.Run = func(cmd *cobra.Command, args []string) {
utils.TryCatch(func() {
if file == "" {
logger.ErrorF("Filename Not Nil")
return
}
if tableName == "" {
logger.ErrorF("TableName Not Nil")
return
}
file := strings.ToLower(file)
dir, err := os.Getwd()
output := "/app/common/model"
output = path.Join(dir, output)
if err != nil {
logger.ErrorF("Make Model Error%v", err)
return
}
if err = utils.PrepareOutput(output); err != nil {
logger.ErrorF("Make Model Error%v", err)
return
}
fmt.Println(output)
modelFile := &ModelFile{
Name: "model",
StrutName: utils.ToSnake(file, "_"),
TableName: tableName,
}
err = modelFile.Execute(path.Join(output, file+".go"), "model", ModelTemplate)
if err != nil {
logger.ErrorF("Make Model Error%v", err)
return
}
return
})
}
}