forked from go-nunu/nunu
/
create.go
163 lines (145 loc) · 4.13 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package create
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/Tooooommy/nunu/internal/pkg/helper"
"github.com/Tooooommy/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 (
tplPath string
)
func init() {
CmdCreateHandler.Flags().StringVarP(&tplPath, "tpl-path", "t", tplPath, "template path")
CmdCreateService.Flags().StringVarP(&tplPath, "tpl-path", "t", tplPath, "template path")
CmdCreateRepository.Flags().StringVarP(&tplPath, "tpl-path", "t", tplPath, "template path")
CmdCreateModel.Flags().StringVarP(&tplPath, "tpl-path", "t", tplPath, "template path")
CmdCreateAll.Flags().StringVarP(&tplPath, "tpl-path", "t", tplPath, "template path")
}
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()
var t *template.Template
var err error
if tplPath == "" {
t, err = template.ParseFS(tpl.CreateTemplateFS, fmt.Sprintf("create/%s.tpl", c.CreateType))
} else {
t, err = template.ParseFiles(path.Join(tplPath, fmt.Sprintf("%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 := filepath.Join(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
}