-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnapp.go
337 lines (270 loc) · 8.45 KB
/
napp.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"embed"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/urfave/cli"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
//go:embed all:source
var source embed.FS
func main() {
app := &cli.App{
Name: "napp",
UsageText: "[command] [command options]",
Version: "v1.3.1",
Description: `A command line tool that bootstraps Go, HTMX and SQLite web
applications and Dockerises them for ease of deployment`,
Commands: []cli.Command{
{
Name: "init",
ShortName: "i",
Usage: "Initialise a new napp project ready for development",
UsageText: "napp init <project-name>",
Action: func(cCtx *cli.Context) error {
if len(cCtx.Args()) != 1 {
msg := fmt.Sprintf(
"Oops! Received %v arguments, wanted 1",
len(cCtx.Args()),
)
return cli.NewExitError(msg, 1)
}
projectname := cCtx.Args().Get(0)
if isInvalidProjectName(projectname) {
return cli.NewExitError(
"Oops! Project name must be in the following format: <project-name>",
1,
)
}
ok, _ := createProject(projectname)
if ok {
fmt.Println("Successfully created " + projectname + ", next steps:")
fmt.Println("cd " + projectname)
fmt.Println("go mod init")
fmt.Println("go mod tidy")
fmt.Println("go run cmd/main.go")
}
return nil
},
},
},
Author: "Damien Sedgwick",
Email: "damienksedgwick@gmail.com",
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func isInvalidProjectName(name string) bool {
pattern := "^[a-z0-9-]+$"
matched, err := regexp.MatchString(pattern, name)
if err != nil {
return true
}
return !matched
}
func createProject(projectName string) (bool, error) {
err := os.Mkdir(projectName, 0755)
if err != nil {
return false, fmt.Errorf("error creating project directory: %w", err)
}
subfolders := []string{"cmd", "template", "static"}
for _, folder := range subfolders {
folderPath := fmt.Sprintf("%s/%s", projectName, folder)
err := os.Mkdir(folderPath, 0755)
if err != nil {
return false, fmt.Errorf("error creating subfolder %s: %w", folder, err)
}
}
createGoMainFile(projectName)
createHtmlFile(projectName)
createDashboardHtmlFile(projectName)
createHtmxFile(projectName)
createTwColorsFile(projectName)
createCssFile(projectName)
createIgnoreFile(projectName)
createDotEnvFile(projectName)
createSqliteDbFile(projectName)
createDockerfile(projectName)
return true, nil
}
func createGoMainFile(projectName string) {
sessEnv := strings.ReplaceAll(strings.ToUpper(projectName), "-", "_") + "_COOKIE_STORE_SECRET"
dbEnv := strings.ReplaceAll(strings.ToUpper(projectName), "-", "_") + "_DB_PATH"
mainGoTemplate, err := source.ReadFile("source/cmd/main.go")
if err != nil {
fmt.Println(fmt.Errorf("error reading source main.go file: %w", err))
}
mainGoContent := fmt.Sprintf(string(mainGoTemplate), sessEnv, dbEnv)
filePath := filepath.Join(projectName, "cmd", "main.go")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating main.go file: ", err)
}
defer f.Close()
_, err = f.WriteString(mainGoContent)
if err != nil {
fmt.Println("error writing main.go content to file: ", err)
}
}
func createHtmlFile(projectName string) {
pn := strings.ReplaceAll(projectName, "-", " ")
caser := cases.Title(language.English)
title := caser.String(pn)
indexHTMLTemplate, err := source.ReadFile("source/template/index.html")
if err != nil {
fmt.Println(fmt.Errorf("error reading source index.html file: %w", err))
}
indexHTMLContent := fmt.Sprintf(string(indexHTMLTemplate), title, title, title, title)
filePath := filepath.Join(projectName, "template", "index.html")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating index.html file: ", err)
}
defer f.Close()
_, err = f.WriteString(indexHTMLContent)
if err != nil {
fmt.Println("error writing index.html content to file: ", err)
}
}
func createDashboardHtmlFile(projectName string) {
pn := strings.ReplaceAll(projectName, "-", " ")
caser := cases.Title(language.English)
title := caser.String(pn)
dashboardHTMLTemplate, err := source.ReadFile("source/template/dashboard.html")
if err != nil {
fmt.Println(fmt.Errorf("error reading source dashboard.html file: %w", err))
}
dashboardHTMLContent := fmt.Sprintf(string(dashboardHTMLTemplate), title)
filePath := filepath.Join(projectName, "template", "dashboard.html")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating dashboard.html file: ", err)
}
defer f.Close()
_, err = f.WriteString(dashboardHTMLContent)
if err != nil {
fmt.Println("error writing dashboard.html content to file: ", err)
}
}
func createHtmxFile(projectName string) {
htmxJsContent, err := source.ReadFile("source/static/htmx.min.js")
if err != nil {
fmt.Println(fmt.Errorf("error reading source htmx.min.js file: %w", err))
}
filePath := filepath.Join(projectName, "static", "htmx.min.js")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating styles.css file: ", err)
}
defer f.Close()
_, err = f.WriteString(string(htmxJsContent))
if err != nil {
fmt.Println("error writing styles.css content to file: ", err)
}
}
func createTwColorsFile(projectName string) {
cssContent, err := source.ReadFile("source/static/twcolors.min.css")
if err != nil {
fmt.Println(fmt.Errorf("error reading source htmx.min.js file: %w", err))
}
filePath := filepath.Join(projectName, "static", "twcolors.min.css")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating twcolors.min.css file: ", err)
}
defer f.Close()
_, err = f.WriteString(string(cssContent))
if err != nil {
fmt.Println("error writing twcolors.min.css content to file: ", err)
}
}
func createCssFile(projectName string) {
cssContent, err := source.ReadFile("source/static/styles.css")
if err != nil {
fmt.Println(fmt.Errorf("error reading source htmx.min.js file: %w", err))
}
filePath := filepath.Join(projectName, "static", "styles.css")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating styles.css file: ", err)
}
defer f.Close()
_, err = f.WriteString(string(cssContent))
if err != nil {
fmt.Println("error writing styles.css content to file: ", err)
}
}
func createIgnoreFile(projectName string) {
dbFilename := strings.ToLower(projectName) + ".db"
envFilename := ".env"
ignoreTemplate, err := source.ReadFile("source/.gitignore")
if err != nil {
fmt.Println(fmt.Errorf("error reading source .gitignore file: %w", err))
}
ignoreContent := fmt.Sprintf(
string(ignoreTemplate),
envFilename,
dbFilename,
)
filePath := filepath.Join(projectName, ".gitignore")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating .gitignore file: ", err)
}
defer f.Close()
_, err = f.WriteString(ignoreContent)
if err != nil {
fmt.Println("error writing .gitignore content to file: ", err)
}
}
func createDotEnvFile(projectName string) {
dbEnv := strings.ReplaceAll(strings.ToUpper(projectName), "-", "_")
sessEnv := strings.ReplaceAll(strings.ToUpper(projectName), "-", "_")
sessSecret := "secret"
dbFilename := strings.ToLower(projectName) + ".db"
dotenvTemplate, err := source.ReadFile("source/.env")
if err != nil {
fmt.Println(fmt.Errorf("error reading source .env file: %w", err))
}
dotenvContent := fmt.Sprintf(string(dotenvTemplate), dbEnv, dbFilename, sessEnv, sessSecret)
filePath := filepath.Join(projectName, ".env")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating .env file: ", err)
}
defer f.Close()
_, err = f.WriteString(dotenvContent)
if err != nil {
fmt.Println("error writing database content to file: ", err)
}
}
func createSqliteDbFile(projectName string) {
dbfileName := strings.ToLower(projectName) + ".db"
filePath := filepath.Join(projectName, dbfileName)
_, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating database file: ", err)
}
}
func createDockerfile(projectName string) {
dockerfileContent, err := source.ReadFile("source/Dockerfile")
if err != nil {
fmt.Println(fmt.Errorf("error reading source Dockerfile file: %w", err))
}
filePath := filepath.Join(projectName, "Dockerfile")
f, err := os.Create(filePath)
if err != nil {
fmt.Println("error creating Dockerfile file: ", err)
}
defer f.Close()
_, err = f.WriteString(string(dockerfileContent))
if err != nil {
fmt.Println("error writing Dockerfile content to file: ", err)
}
}