Files
nunu/internal/command/create/create.go
2023-07-23 08:54:29 +08:00

145 lines
3.5 KiB
Go

package create
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/go-nunu/nunu/internal/pkg/helper"
"github.com/go-nunu/nunu/tpl"
"github.com/spf13/cobra"
)
type Create struct {
ProjectName string
CreateType string
FilePath string
FileName string
FileNameTitleLower string
FileNameFirstChar string
IsFull bool
}
func NewCreate() *Create {
return &Create{}
}
var CmdCreate = &cobra.Command{
Use: "create [type] [handler-name]",
Short: "Create a new handler/service/repository/model",
Example: "nunu create handler user",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
},
}
var CmdCreateHandler = &cobra.Command{
Use: "handler",
Short: "Create a new handler",
Example: "nunu create handler user",
Args: cobra.ExactArgs(1),
Run: runCreate,
}
var CmdCreateService = &cobra.Command{
Use: "service",
Short: "Create a new service",
Example: "nunu create service user",
Args: cobra.ExactArgs(1),
Run: runCreate,
}
var CmdCreateRepository = &cobra.Command{
Use: "repository",
Short: "Create a new repository",
Example: "nunu create repository user",
Args: cobra.ExactArgs(1),
Run: runCreate,
}
var CmdCreateModel = &cobra.Command{
Use: "model",
Short: "Create a new model",
Example: "nunu create model user",
Args: cobra.ExactArgs(1),
Run: runCreate,
}
var CmdCreateAll = &cobra.Command{
Use: "all",
Short: "Create a new handler & service & repository & model",
Example: "nunu create all user",
Args: cobra.ExactArgs(1),
Run: runCreate,
}
func runCreate(cmd *cobra.Command, args []string) {
c := NewCreate()
c.ProjectName = helper.GetProjectName(".")
c.CreateType = cmd.Use
c.FilePath, c.FileName = filepath.Split(args[0])
c.FileName = strings.ReplaceAll(strings.ToUpper(string(c.FileName[0]))+c.FileName[1:], ".go", "")
c.FileNameTitleLower = strings.ToLower(string(c.FileName[0])) + c.FileName[1:]
c.FileNameFirstChar = string(c.FileNameTitleLower[0])
switch c.CreateType {
case "handler", "service", "repository", "model":
c.genFile()
case "all":
c.CreateType = "handler"
c.genFile()
c.CreateType = "service"
c.genFile()
c.CreateType = "repository"
c.genFile()
c.CreateType = "model"
c.genFile()
default:
log.Fatalf("Invalid handler type: %s", c.CreateType)
}
}
func (c *Create) genFile() {
filePath := c.FilePath
if filePath == "" {
filePath = fmt.Sprintf("internal/%s/", c.CreateType)
}
f := createFile(filePath, strings.ToLower(c.FileName)+".go")
if f == nil {
log.Printf("warn: file %s%s %s", filePath, strings.ToLower(c.FileName)+".go", "already exists.")
return
}
defer f.Close()
t, err := template.ParseFS(tpl.CreateTemplateFS, fmt.Sprintf("create/%s.tpl", c.CreateType))
if err != nil {
log.Fatalf("create %s error: %s", c.CreateType, err.Error())
}
err = t.Execute(f, c)
if err != nil {
log.Fatalf("create %s error: %s", c.CreateType, err.Error())
}
log.Printf("Created new %s: %s", c.CreateType, filePath+strings.ToLower(c.FileName)+".go")
}
func createFile(dirPath string, filename string) *os.File {
filePath := dirPath + filename
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
log.Fatalf("Failed to create dir %s: %v", dirPath, err)
}
stat, _ := os.Stat(filePath)
if stat != nil {
return nil
}
file, err := os.Create(filePath)
if err != nil {
log.Fatalf("Failed to create file %s: %v", filePath, err)
}
return file
}