-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
209 lines (169 loc) · 5.79 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"github.com/invxp/go-layout-assistant/internal/util/convert"
"github.com/invxp/go-layout-assistant/internal/util/io"
"github.com/pkg/errors"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
defaultServiceNameLower = "application"
defaultServiceNameUpper = "Application"
defaultModPath = "github.com/invxp/go-layout-assistant"
defaultInstallDir = "/home/application"
)
var (
blacklist = map[string]struct{}{
".git": {},
".idea": {},
"logs": {},
"vendor": {},
"cmd/assistant": {}}
whitelist = map[string]struct{}{
"go.mod": {},
"go.sum": {}}
)
func main() {
var serviceName string
var modPath string
var installDir string
fmt.Printf("---input your service name(app)*: ")
_, _ = fmt.Scanln(&serviceName)
serviceName = strings.TrimSpace(serviceName)
if serviceName == "" {
log.Printf("create service name must be exist")
os.Exit(0)
}
fmt.Printf("---input your go mod path(github.com/invxp/fsm)*: ")
_, _ = fmt.Scanln(&modPath)
modPath = strings.TrimSpace(modPath)
if modPath == "" {
log.Printf("create mod path must be exist")
os.Exit(0)
}
fmt.Printf("---input %s install dir(/home/%s): ", serviceName, serviceName)
_, _ = fmt.Scanln(&installDir)
if err := CreateService(serviceName, modPath, installDir); err != nil {
log.Printf("create service: %s failed: %v", serviceName, err)
} else {
log.Printf("create service: %s success", serviceName)
}
fixedName := strings.Split(serviceName, "-")[len(strings.Split(serviceName, "-"))-1]
if serviceName != fixedName {
if err := FixServiceCodes(serviceName, fixedName); err != nil {
log.Printf("fix service code: %s failed: %v.%v", serviceName, err, os.RemoveAll(serviceName))
} else {
log.Printf("fix service code: %s success: %v", serviceName, os.RemoveAll(filepath.Join(serviceName, serviceName)))
}
}
os.Exit(0)
}
//CreateService 克隆一个应用
func CreateService(serviceName, modPath, installDir string) error {
if installDir == "" {
installDir = filepath.Join("/home", serviceName)
}
err := os.RemoveAll(serviceName)
if err != nil {
return err
}
mustMkdir(serviceName)
log.Printf("prepare create %s success, installDir: %s, modPath: %s", serviceName, installDir, modPath)
fullPath := io.FullPath()
return filepath.WalkDir(fullPath, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
if strings.HasPrefix(path, filepath.Join(fullPath, serviceName)) {
log.Printf("skip self: %s from blacklist", path)
return err
}
for name := range blacklist {
if strings.HasPrefix(path, filepath.Join(fullPath, name)) {
log.Printf("skip path: %s from blacklist", path)
return err
}
}
path = strings.ReplaceAll(path, fullPath, "")
if dir := filepath.Dir(path); dir != "." {
return replaceFile(path, serviceName, modPath, installDir, true)
}
if _, exists := whitelist[path]; exists {
return replaceFile(path, serviceName, modPath, installDir, true)
} else {
log.Printf("skip file: %s from whitelist", path)
}
return err
})
}
//replaceFile 克隆文件+内容
func replaceFile(originFilename, serviceName, modPath, installDir string, replaceContent bool) error {
var write string
upperServiceName := strings.ToUpper(serviceName[:1]) + serviceName[1:]
if dir := filepath.Dir(originFilename); dir != "." {
if replaceContent {
dir = strings.ReplaceAll(dir, defaultServiceNameLower, serviceName)
}
mustMkdir(filepath.Join(serviceName, dir))
}
read, err := ioutil.ReadFile(originFilename)
if err != nil {
return err
}
write = convert.ByteToString(read)
if replaceContent {
write = strings.ReplaceAll(convert.ByteToString(read), defaultModPath, modPath)
write = strings.ReplaceAll(write, defaultInstallDir, installDir)
write = strings.ReplaceAll(write, defaultServiceNameLower, serviceName)
write = strings.ReplaceAll(write, defaultServiceNameUpper, upperServiceName)
originFilename = strings.ReplaceAll(originFilename, defaultServiceNameLower, serviceName)
}
return errors.Wrapf(ioutil.WriteFile(filepath.Join(serviceName, originFilename), convert.StringToByte(write), 0644), "create file: %s", filepath.Join(serviceName, originFilename))
}
//FixServiceCodes 修复带'-'符号的工程代码
func FixServiceCodes(serviceName, fixedName string) error {
if serviceName == fixedName {
return nil
}
mustMkdir(filepath.Join(serviceName, fixedName))
return filepath.WalkDir(serviceName, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
if path == filepath.Join(serviceName, "cmd", serviceName, "main.go") {
return fixFile(path, path, serviceName, fixedName)
}
if strings.HasPrefix(path, filepath.Join(serviceName, serviceName)) {
return fixFile(path, filepath.Join(serviceName, fixedName, strings.ReplaceAll(strings.Split(path, string(os.PathSeparator))[len(strings.Split(path, string(os.PathSeparator)))-1], serviceName, fixedName)), serviceName, fixedName)
}
return err
})
}
func fixFile(originFilename, destFilename, serviceName, fixedName string) error {
read, err := ioutil.ReadFile(originFilename)
if err != nil {
return err
}
upperServiceName := strings.ToUpper(serviceName[:1]) + serviceName[1:]
upperFixedName := strings.ToUpper(fixedName[:1]) + fixedName[1:]
write := convert.ByteToString(read)
write = strings.ReplaceAll(convert.ByteToString(read), serviceName, fixedName)
write = strings.ReplaceAll(write, upperServiceName, upperFixedName)
return errors.Wrapf(ioutil.WriteFile(destFilename, convert.StringToByte(write), 0644), "create file: %s", filepath.Join(destFilename))
}
func mustMkdir(dir string) {
dirPerm := os.FileMode(0644)
if runtime.GOOS == "darwin" {
dirPerm = 0755
}
err := os.MkdirAll(dir, dirPerm)
if err != nil {
panic(err)
}
}