-
Notifications
You must be signed in to change notification settings - Fork 3
/
structure.go
91 lines (74 loc) · 1.53 KB
/
structure.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
package main
import (
"fmt"
"os"
"path"
"text/template"
"github.com/gobuffalo/packr/v2"
)
const (
permMode = 0764
)
type application struct {
Path string
Name string
Logger string
LoggerPackage string
Router string
RouterPackage string
}
func (a *application) String() string {
return a.Name
}
type dir struct {
Name string
Files map[string]string // [name]template
}
type structure struct {
Box *packr.Box
App *application
Files map[string]string // [name]template
Directories []*dir
}
func (s *structure) create() {
for file, temp := range s.Files {
s.fileFromTemplate(file, temp, s.App)
}
for _, d := range s.Directories {
s.makeDir(d.Name)
for file, temp := range d.Files {
s.fileFromTemplate(file, temp, s.App)
}
}
}
func (s *structure) makeDir(path string) {
err := os.MkdirAll(path, permMode)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
printSuccess(fmt.Sprintf("Created %s", path))
}
func (s *structure) fileFromTemplate(filePath, templatePath string, args interface{}) {
f, err := os.Create(filePath)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
defer f.Close()
txt, err := s.Box.FindString(templatePath)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
tpl, err := template.New(path.Base(templatePath)).Parse(txt)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
if err = tpl.Execute(f, args); err != nil {
printErr(err.Error())
os.Exit(1)
}
printSuccess(fmt.Sprintf("Created %s", filePath))
}