feat:完善项目
This commit is contained in:
62
cmd/ctl/command/model/file.go
Normal file
62
cmd/ctl/command/model/file.go
Normal file
@ -0,0 +1,62 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/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
|
||||
}
|
73
cmd/ctl/command/model/model.go
Normal file
73
cmd/ctl/command/model/model.go
Normal file
@ -0,0 +1,73 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/logger"
|
||||
"SciencesServer/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
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user