Files

74 lines
1.4 KiB
Go
Raw Normal View History

2021-11-02 09:43:19 +08:00
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
})
}
}