Files
2021-11-25 09:58:08 +08:00

74 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
})
}
}