Repository navigation
Expand file tree
/
Copy pathmake.go
More file actions
executable file
·134 lines (105 loc) · 3.08 KB
/
Copy pathmake.go
File metadata and controls
executable file
·134 lines (105 loc) · 3.08 KB
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/fatih/color"
"github.com/gertd/go-pluralize"
"github.com/iancoleman/strcase"
)
func doMake(arg2, arg3 string) error {
switch arg2 {
case "key":
rnd := gho.RandomString(32)
color.Yellow("32 character encryption key: %s", rnd)
case "migration":
dbType := gho.DB.DataType
if arg3 == "" {
exitGracefully(errors.New("you must give the migration a name"))
}
fileName := fmt.Sprintf("%d_%s", time.Now().UnixMicro(), arg3)
upFile := gho.RootPath + "/migrations/" + fileName + "." + dbType + ".up.sql"
downFile := gho.RootPath + "/migrations/" + fileName + "." + dbType + ".down.sql"
err := copyFilefromTemplate("templates/migrations/migration."+dbType+".up.sql", upFile)
if err != nil {
exitGracefully(err)
}
err = copyFilefromTemplate("templates/migrations/migration."+dbType+".down.sql", downFile)
if err != nil {
exitGracefully(err)
}
case "auth":
err := doAuth()
if err != nil {
exitGracefully(err)
}
case "handler":
if arg3 == "" {
exitGracefully(errors.New("you must give the handler a name"))
}
fileName := gho.RootPath + "/handlers/" + strings.ToLower(arg3) + ".go"
if fileExists(fileName) {
exitGracefully(errors.New(fileName + " already exists!"))
}
data, err := templateFS.ReadFile("templates/handlers/handler.go.txt")
if err != nil {
exitGracefully(err)
}
handler := string(data)
handler = strings.ReplaceAll(handler, "$HANDLERNAME$", strcase.ToCamel(arg3))
err = ioutil.WriteFile(fileName, []byte(handler), 0644)
if err != nil {
exitGracefully(err)
}
case "model":
if arg3 == "" {
exitGracefully(errors.New("you must give the model a name"))
}
data, err := templateFS.ReadFile("templates/data/model.go.txt")
if err != nil {
exitGracefully(err)
}
model := string(data)
plur := pluralize.NewClient()
var modelName = arg3
var tableName = arg3
if plur.IsPlural(arg3) {
modelName = plur.Singular(arg3)
tableName = strings.ToLower(tableName)
} else {
tableName = strings.ToLower(plur.Plural(arg3))
}
fileName := gho.RootPath + "/data/" + strings.ToLower(modelName) + ".go"
if fileExists(fileName) {
exitGracefully(errors.New(fileName + " already exists!"))
}
model = strings.ReplaceAll(model, "$MODELNAME$", strcase.ToCamel(modelName))
model = strings.ReplaceAll(model, "$TABLENAME$", tableName)
err = copyDataToFile([]byte(model), fileName)
if err != nil {
exitGracefully(err)
}
case "mail":
if arg3 == "" {
exitGracefully(errors.New("you must give the mail template a name"))
}
htmlMail := gho.RootPath + "/mail/" + strings.ToLower(arg3) + ".html.tmpl"
plainMail := gho.RootPath + "/mail/" + strings.ToLower(arg3) + ".plain.tmpl"
err := copyFilefromTemplate("templates/mailer/mail.html.tmpl", htmlMail)
if err != nil {
exitGracefully(err)
}
err = copyFilefromTemplate("templates/mailer/mail.plain.tmpl", plainMail)
if err != nil {
exitGracefully(err)
}
case "session":
err := doSessionTable()
if err != nil {
exitGracefully(err)
}
}
return nil
}