Files

76 lines
1.6 KiB
Go
Raw Normal View History

2021-11-25 09:58:08 +08:00
package controller
import (
"SciencesServer/config"
"SciencesServer/serve/logger"
"SciencesServer/utils"
"fmt"
"os"
"path"
"strings"
"github.com/spf13/cobra"
)
var ControllerCommand = &cobra.Command{
Use: "make:controller",
Short: "quick make controller",
Example: "ctl make:controller -f sys_user -t sys_user",
Version: *config.Version,
}
var (
file string
address string
)
func init() {
ControllerCommand.Flags().StringVarP(&file, "file", "f", "", "The file name.")
ControllerCommand.Flags().StringVarP(&address, "address", "a", "", "The file controller address")
ControllerCommand.Run = func(cmd *cobra.Command, args []string) {
utils.TryCatch(func() {
if file == "" {
logger.ErrorF("Filename Not Nil")
return
}
output := "../../app/controller"
if address == "" {
logger.ErrorF("Address Not Nil")
return
}
// 解析文件地址
address = strings.Replace(address, "/", "", -1)
addresss := strings.Split(address, "/")
file := strings.ToLower(file)
dir, err := os.Getwd()
output = path.Join(dir, output+"/"+address)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
if err = utils.PrepareOutput(output); err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
fmt.Println(output)
modelFile := &ControllerFile{
Name: addresss[len(addresss)-1],
StrutName: utils.ToSnake(file, "_"),
}
err = modelFile.Execute(path.Join(output, file+".go"), "controller", ControllerTemplate)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
return
})
}
}