-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjay.go
322 lines (271 loc) · 9.79 KB
/
jay.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Package main is the entry point for the Blue Jay command-line tool called
// Jay.
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/blue-jay/core/env"
"github.com/blue-jay/core/file"
"github.com/blue-jay/core/find"
"github.com/blue-jay/core/generate"
"github.com/blue-jay/core/jsonconfig"
"github.com/blue-jay/core/replace"
"github.com/blue-jay/core/storage"
mysqlMigration "github.com/blue-jay/core/storage/migration/mysql"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("jay", "A command-line application to build faster with Blue Jay.")
flagConfigFile = app.Flag("config", "Path to the env.json file.").Short('c').String()
cFind = app.Command("find", "Search for files containing matching text.")
cFindFolder = cFind.Arg("folder", "Folder to search").Required().String()
cFindText = cFind.Arg("text", "Case-sensitive text to find.").Required().String()
cFindExtension = cFind.Arg("extension", "File name or extension to search in. Use * as a wildcard. Directory names are not valid.").Default("*.go").String()
cFindRecursive = cFind.Arg("recursive", "True to search in subfolders. Default: true").Default("true").Bool()
cFindFilename = cFind.Arg("filename", "True to include file path in results if matched. Default: false").Default("false").Bool()
cReplace = app.Command("replace", "Search for files containing matching text and then replace it with new text.")
cReplaceFolder = cReplace.Arg("folder", "Folder to search").Required().String()
cReplaceFind = cReplace.Arg("find", "Case-sensitive text to replace.").Required().String()
cReplaceText = cReplace.Arg("replace", "Text to replace with.").String()
cReplaceExtension = cReplace.Arg("extension", "File name or extension to search in. Use * as a wildcard. Directory names are not valid.").Default("*.go").String()
cReplaceRecursive = cReplace.Arg("recursive", "True to search in subfolders. Default: true").Default("true").Bool()
cReplaceFilename = cReplace.Arg("filename", "True to include file path in results if matched. Default: false").Default("false").Bool()
cReplaceCommit = cReplace.Arg("commit", "True to makes the changes instead of just displaying them. Default: true").Default("true").Bool()
cEnv = app.Command("env", "Manage the environment config file.")
cEnvMake = cEnv.Command("make", "Create a new env.json file.")
cEnvKeyshow = cEnv.Command("keyshow", "Show a new set of session keys.")
cEnvKeyUpdate = cEnv.Command("keyupdate", "Update env.json with a new set of session keys.")
cMigrateMySQL = app.Command("migrate:mysql", "Migrate MySQL to different states using 'up' and 'down' files.")
cMigrateMySQLMake = cMigrateMySQL.Command("make", "Create a migration file.")
cMigrateMySQLMakeDesc = cMigrateMySQLMake.Arg("description", "Description for the migration file. Spaces will be converted to underscores and all characters will be make lowercase.").Required().String()
cMigrateMySQLAll = cMigrateMySQL.Command("all", "Run all 'up' files to advance the database to the latest.")
cMigrateMySQLReset = cMigrateMySQL.Command("reset", "Run all 'down' files to rollback the database to empty.")
cMigrateMySQLRefresh = cMigrateMySQL.Command("refresh", "Run all 'down' files and then 'up' files so the database is fresh and updated.")
cMigrateMySQLStatus = cMigrateMySQL.Command("status", "View the last 'up' file performed on the database.")
cMigrateMySQLUp = cMigrateMySQL.Command("up", "Apply only the next 'up' file to the database to advance the database one iteration.")
cMigrateMySQLDown = cMigrateMySQL.Command("down", "Apply only the current 'down' file to the database to rollback the database one iteration.")
cGenerate = app.Command("generate", "Generate files from template pairs.")
cGenerateTmpl = cGenerate.Arg("folder/template", "Template pair name. Don't include an extension.").Required().String()
cGenerateVars = stringList(cGenerate.Arg("key:value", "Key and value required for the template pair."))
)
// init sets runtime settings.
func init() {
// Verbose logging with file name and line number
log.SetFlags(log.Lshortfile)
// Use all CPU cores
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
app.Version("0.5-bravo")
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
argList := os.Args[1:]
arg := kingpin.MustParse(app.Parse(argList))
commandFind(arg)
commandReplace(arg)
commandEnv(arg)
commandMigrateMySQL(arg, argList)
commandGenerate(arg, argList)
}
func commandFind(arg string) {
switch arg {
case cFind.FullCommand():
contents, err := find.Run(cFindText,
cFindFolder,
cFindExtension,
cFindRecursive,
cFindFilename)
if err != nil {
app.Fatalf("%v", err)
}
for _, line := range contents {
fmt.Println(line)
}
}
}
func commandReplace(arg string) {
switch arg {
case cReplace.FullCommand():
contents, err := replace.Run(cReplaceFind,
cReplaceFolder,
cReplaceText,
cReplaceExtension,
cReplaceRecursive,
cReplaceFilename,
cReplaceCommit)
if err != nil {
app.Fatalf("%v", err)
}
for _, line := range contents {
fmt.Println(line)
}
}
}
func commandEnv(arg string) {
switch arg {
case cEnvMake.FullCommand():
err := file.Copy("env.json.example", "env.json")
if err != nil {
app.Fatalf("%v", err)
}
err = env.UpdateFileKeys("env.json")
if err != nil {
app.Fatalf("%v", err)
}
p, err := filepath.Abs(".")
if err != nil {
app.Fatalf("%v", err)
}
config := filepath.Join(p, "env.json")
if !file.Exists(config) {
app.Fatalf("%v", err)
}
fmt.Println("File, env.json, created successfully with new session keys.")
fmt.Println("Set your environment variable, JAYCONFIG, to:")
fmt.Println(config)
case cEnvKeyshow.FullCommand():
fmt.Println("Paste these into your env.json file:")
fmt.Printf(` "AuthKey":"%v",`+"\n", env.EncodedKey(64))
fmt.Printf(` "EncryptKey":"%v",`+"\n", env.EncodedKey(32))
fmt.Printf(` "CSRFKey":"%v",`+"\n", env.EncodedKey(32))
case cEnvKeyUpdate.FullCommand():
err := env.UpdateFileKeys("env.json")
if err != nil {
app.Fatalf("%v", err)
}
fmt.Println("Session keys updated in env.json.")
}
}
func commandMigrateMySQL(arg string, argList []string) {
if argList[0] != "migrate:mysql" {
return
}
var err error
// Config struct
info := &storage.Info{}
configFile := ""
// Check if the config file path was passed
if len(*flagConfigFile) > 0 {
// Load the config from the passed file
err = jsonconfig.Load(*flagConfigFile, info)
// Get the config file path
configFile = *flagConfigFile
} else {
// Load the config from the environment variable
err = jsonconfig.LoadFromEnv(info)
// Get the config file path
configFile = os.Getenv("JAYCONFIG")
}
if err != nil {
app.Fatalf("%v", err)
}
// Perform config validation
if len(info.MySQL.Database) == 0 {
app.Fatalf("%v", "Database name is missing from the config file.")
}
if len(info.MySQL.Migration.Folder) == 0 {
app.Fatalf("%v", "Migration folder is missing from the config file.")
}
// Set to the absolute path
info.MySQL.Migration.Folder = filepath.Join(filepath.Dir(configFile), info.MySQL.Migration.Folder)
if !file.Exists(info.MySQL.Migration.Folder) {
app.Fatalf("%v", "Migration folder is not found on disk.")
}
if len(info.MySQL.Migration.Table) == 0 {
app.Fatalf("%v", "Migration table is missing from the config file.")
}
// Create a new configuration
mysqlConfig := &mysqlMigration.Configuration{
info.MySQL,
}
// Create a new migration object
mig, err := mysqlConfig.New()
if err != nil {
app.Fatalf("%v", err)
}
switch arg {
case cMigrateMySQLMake.FullCommand():
err = mig.Create(*cMigrateMySQLMakeDesc)
case cMigrateMySQLAll.FullCommand():
err = mig.UpAll()
case cMigrateMySQLReset.FullCommand():
err = mig.DownAll()
case cMigrateMySQLRefresh.FullCommand():
if mig.Position() == 0 {
err = mig.UpAll()
} else {
err = mig.DownAll()
err = mig.UpAll()
}
case cMigrateMySQLStatus.FullCommand():
fmt.Println("Last migration:", mig.Status())
case cMigrateMySQLUp.FullCommand():
err = mig.UpOne()
case cMigrateMySQLDown.FullCommand():
err = mig.DownOne()
}
if err != nil {
app.Fatalf("%v", err)
} else {
fmt.Print(mig.Output())
}
}
func commandGenerate(arg string, args []string) {
if args[0] != "generate" {
return
}
var err error
// Load the config
info := &generate.Container{}
configFile := ""
// Check if the config file path was passed
if len(*flagConfigFile) > 0 {
// Load the config from the passed file
err = jsonconfig.Load(*flagConfigFile, info)
configFile = *flagConfigFile
} else {
// Load the config from the environment variable
err = jsonconfig.LoadFromEnv(info)
// Get the config file path
configFile = os.Getenv("JAYCONFIG")
}
if err != nil {
app.Fatalf("%v", err)
}
// Get the folders
projectFolder := filepath.Dir(configFile)
templateFolder := filepath.Join(projectFolder, info.Generation.TemplateFolder)
// Generate the code
err = generate.Run(args[1:], projectFolder, templateFolder)
if err != nil {
app.Fatalf("%v", err)
}
}
// *****************************************************************************
// Custom Arguments
// *****************************************************************************
// StringList is a string array.
type StringList []string
// Set appends the string to the list.
func (i *StringList) Set(value string) error {
*i = append(*i, value)
return nil
}
// String returns the list.
func (i *StringList) String() string {
return strings.Join(*i, " ")
}
// IsCumulative allows more than one value to be passed.
func (i *StringList) IsCumulative() bool {
return true
}
// stringList accepts one or more strings as arguments.
func stringList(s kingpin.Settings) (target *StringList) {
target = new(StringList)
s.SetValue((*StringList)(target))
return
}