-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate.go
103 lines (81 loc) · 2.43 KB
/
generate.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
package golang
import (
"fmt"
"github.com/commitdev/commit0/util"
"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
"log"
"os"
)
func Generate(templator *templator.Templator, config *config.Commit0Config) {
GenerateGoMain(templator, config)
GenerateGoMod(templator, config)
GenerateHealthServer(templator, config)
GenerateServers(templator, config)
}
func GenerateGoMain(templator *templator.Templator, config *config.Commit0Config) {
if _, err := os.Stat("main.go"); os.IsNotExist(err) {
f, err := os.Create("main.go")
if err != nil {
log.Printf("Error: %v", err)
}
templator.Go.GoMain.Execute(f, config)
} else {
log.Printf("main.go already exists. skipping.")
}
}
func GenerateGoMod(templator *templator.Templator, config *config.Commit0Config) {
f, err := os.Create("go.mod")
if err != nil {
log.Printf("Error: %v", err)
}
templator.Go.GoMod.Execute(f, config)
}
func GenerateServers(templator *templator.Templator, config *config.Commit0Config) {
serverDirPath := "server"
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error creating server path: %v", err)
}
for _, s := range config.Services {
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, s.Name)
err := os.Mkdir(serverLibPath, os.ModePerm)
if os.IsExist(err) {
log.Printf("%s server exists skipping.", s.Name)
continue
}
log.Printf("generating %s", s.Name)
if err != nil {
log.Printf("Error generating server: %v", err)
}
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, s.Name)
f, err := os.Create(serverFilePath)
if err != nil {
log.Printf("Error: %v", err)
}
data := map[string]string{
"ProjectName": config.Name,
"ServiceName": s.Name,
"GitRepo": config.GitRepo,
}
templator.Go.GoServer.Execute(f, data)
}
}
func GenerateHealthServer(templator *templator.Templator, config *config.Commit0Config) {
serverDirPath := "server"
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error creating server path: %v", err)
}
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, "health")
err = util.CreateDirIfDoesNotExist(serverLibPath)
if err != nil {
log.Printf("Error generating server: %v", err)
}
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, "health")
f, err := os.Create(serverFilePath)
if err != nil {
log.Printf("Error: %v", err)
}
templator.Go.GoHealthServer.Execute(f, config)
}