-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
236 lines (199 loc) · 6.55 KB
/
init.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// MIT License
// Copyright (c) The RAI Authors
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package cmd
import (
"fmt"
"io"
"io/fs"
"os"
"os/exec"
fpath "path"
"strings"
"text/template"
"github.com/go-playground/validator/v10"
str "github.com/retail-ai-inc/bean/v2/string"
"github.com/spf13/cobra"
)
var (
validationRule = `A module path must satisfy the following requirements:
1. The path must consist of one or more path elements separated by slashes (/, U+002F). It must not begin or end with a slash.
2. Each path element is a non-empty string made of up ASCII letters, ASCII digits, and limited ASCII punctuation (-, ., _).
3. A path element may not begin or end with a dot (., U+002E).
4. The leading path element (up to the first slash, if any), by convention a domain name, must contain only lower-case ASCII letters, ASCII digits, dots (., U+002E), and dashes (-, U+002D); it must contain at least one dot and cannot start with a dash.`
// initCmd represents the init command
initCmd = &cobra.Command{
Use: "init package_name",
Short: "Initialize a project in current directory",
Long: `Init generates all the directories and files structures needed in the current
directory. the suffix of the package_name should match the current directory.`,
Example: "bean init github.com/retail-ai-inc/bean/v2",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkgPath := args[0]
pkgName, err := getProjectName(pkgPath)
if err != nil {
fmt.Println(validationRule)
os.Exit(1)
}
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Generate a 32 character long random secret string.
secret, err := str.GenerateRandomString(32, false)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Generate a 24 character long random JWT secret string.
jwtSecret, err := str.GenerateRandomString(24, false)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Generate a 36 character long random token string.
bearerToken, err := str.GenerateRandomString(36, false)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
p := &Project{
Copyright: copyright,
PkgPath: pkgPath,
PkgName: pkgName,
RootDir: wd,
Secret: secret,
JWTSecret: jwtSecret,
BearerToken: bearerToken,
}
// Set the relative root path of the internal FS.
if p.RootFS, err = fs.Sub(InternalFS, "internal/project"); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("initializing " + p.PkgName + "...")
if err := fs.WalkDir(p.RootFS, ".", p.generateProjectFiles); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("\ntidying go mod...")
goModTidyCmd := exec.Command("go", "mod", "tidy")
goModTidyCmd.Stdout = os.Stdout
goModTidyCmd.Stderr = os.Stderr
if err := goModTidyCmd.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
)
func init() {
initCmd.DisableFlagsInUseLine = true
rootCmd.AddCommand(initCmd)
}
func getProjectName(pkgPath string) (string, error) {
validate := validator.New()
if errs := validate.Var(pkgPath, "required,max=100,startsnotwith=/,endsnotwith=/"); errs != nil {
if errs, ok := errs.(validator.ValidationErrors); ok {
return "", errs
}
fmt.Println(errs)
os.Exit(1)
}
pathElements := strings.Split(pkgPath, "/")
for _, element := range pathElements {
if errs := validate.Var(element, "required,printascii,excludesall=!\"#$%&'()*+0x2C:;<=>?@[\\]^`{0x7C~},startsnotwith=.,endsnotwith=."); errs != nil {
if errs, ok := errs.(validator.ValidationErrors); ok {
return "", errs
}
fmt.Println(errs)
os.Exit(1)
}
}
if len(pathElements) > 1 {
domain := pathElements[0]
if errs := validate.Var(domain, "required,max=100,fqdn"); errs != nil {
if errs, ok := errs.(validator.ValidationErrors); ok {
return "", errs
}
fmt.Println(errs)
os.Exit(1)
}
}
pkgName := pathElements[len(pathElements)-1]
return pkgName, nil
}
func (p *Project) generateProjectFiles(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// Create the same directory under current directory.
if err := os.Mkdir(p.RootDir+"/"+path, 0754); err != nil && d.Name() != "project" {
return err
}
} else {
// Create templates from the files.
var temp *template.Template
fmt.Println(p.RootDir + "/" + path)
if strings.HasSuffix(path, ".html") {
// For .html files, just copy and not need to pass the template.
file, err := p.RootFS.Open(path)
if err != nil {
return err
}
content, err := io.ReadAll(file)
if err != nil {
return err
}
err = os.WriteFile(p.RootDir+"/"+path, content, 0664)
if err != nil {
return err
}
return nil
} else {
// No preprocess for other files.
temp, err = template.ParseFS(p.RootFS, path)
if err != nil {
return err
}
}
// For files which start with `.`, for example `.gitignore`.
fileName := path
fileNameWithoutPath := fpath.Base(path)
if strings.HasPrefix(fileNameWithoutPath, "bean-dot") {
parentPath := strings.TrimSuffix(path, fileNameWithoutPath)
fileName = parentPath + strings.TrimPrefix(fileNameWithoutPath, "bean-dot")
}
// Strip off the `.tpl` file
fileName = strings.TrimSuffix(fileName, ".tpl")
file, err := os.Create(p.RootDir + "/" + fileName)
if err != nil {
return err
}
defer file.Close()
// Parse the template and write to the files.
fileTemplate := template.Must(temp, err)
err = fileTemplate.Execute(file, p)
if err != nil {
return err
}
}
return nil
}