-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (117 loc) · 4.3 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
var (
path = flag.String("path", ".", "Path for creating project ")
name = flag.String("name", "microservices-default", "Name of the microservice")
protoFileName = flag.String("proto", "proto", "proto file name")
providerUsername = flag.String("username", "github.com/username", "Path for provider/Username")
)
func main() {
flag.Parse()
microserviceName := *path + "/" + *name
Createmicroservice(microserviceName)
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
TemplateFile := dir + "/Template/"
folders := []string{"api", "bin", "cmd", "config", "middleware", "internal", "model", "repository", "transport", "usecase", "usecase/handler"}
//Creating folder & files
for _, folder := range folders {
basestring := microserviceName + "/" + folder + "/"
Createfolder(folder, microserviceName)
switch folder {
case "api":
Createfile(basestring + *protoFileName + ".proto")
ReadWrite(TemplateFile+"proto.txt", basestring+*protoFileName+".proto")
Createfile(basestring + *protoFileName + ".yaml")
ReadWrite(TemplateFile+"yaml.txt", basestring+*protoFileName+".yaml")
case "config":
Createfile(basestring + "config.go")
ReadWrite(TemplateFile+"config.txt", basestring+"config.go")
case "middleware":
Createfile(basestring + "logger.go")
ReadWrite(TemplateFile+"middleware.txt", basestring+"logger.go")
case "model":
Createfile(basestring + "datastore.go")
ReadWrite(TemplateFile+"model.txt", basestring+"datastore.go")
case "repository":
Createfile(basestring + *protoFileName + ".repository.go")
ReadWrite(TemplateFile+"repository.txt", basestring+*protoFileName+".repository.go")
case "transport":
Createfile(basestring + "grpc.go")
ReadWrite(TemplateFile+"grpc.txt", basestring+"grpc.go")
Createfile(basestring + "rest.go")
ReadWrite(TemplateFile+"rest.txt", basestring+"rest.go")
Createfile(basestring + "init.go")
ReadWrite(TemplateFile+"init.txt", basestring+"init.go")
case "usecase/handler":
Createfile(basestring + *protoFileName + ".service.go")
ReadWrite(TemplateFile+"service.txt", basestring+*protoFileName+".service.go")
case "cmd":
Createfile(basestring + "main.go")
ReadWrite(TemplateFile+folder+".txt", basestring+"main.go")
}
}
//file on root
//config.yaml
Createfile(microserviceName + "/" + "config.yaml")
ReadWrite(TemplateFile+"configyaml.txt", microserviceName+"/"+"config.yaml")
//makefile
Createfile(microserviceName + "/" + "Makefile")
ReadWrite(TemplateFile+"makefile.txt", microserviceName+"/"+"Makefile")
//mod
Createfile(microserviceName + "/" + "go.mod")
ReadWrite(TemplateFile+"mod.txt", microserviceName+"/"+"go.mod")
//Dockerfile
Createfile(microserviceName + "/" + "Dockerfile")
ReadWrite(TemplateFile+"Dockerfile.txt", microserviceName+"/"+"Dockerfile")
//Tool file
Createfile(microserviceName + "/" + "tool.go")
ReadWrite(TemplateFile+"tool.txt", microserviceName+"/"+"tool.go")
fmt.Println("Microservice Structure Created")
}
func Createmicroservice(microservicename string) {
err := os.Mkdir(microservicename, 0700)
if err != nil {
fmt.Println("err while creating microservice dir", err)
}
}
func Createfolder(folder string, microserviceName string) {
err := os.Mkdir(microserviceName+"/"+folder, 0700)
if err != nil {
fmt.Println("err while creating folder", err)
}
}
func Createfile(file string) {
createFile, err := os.Create(file)
if err != nil {
fmt.Println("err while creating file", err)
}
defer createFile.Close()
}
func ReadWrite(sourceFile string, destinationFile string) {
templateContent, err := ioutil.ReadFile(sourceFile)
if err != nil {
fmt.Println("Error while Reading Template ", destinationFile)
fmt.Println(err)
return
}
customTemplate := strings.Replace(string(templateContent), "{name}", *protoFileName, -1)
customTemplate = strings.Replace(string(customTemplate), "{Name}", strings.Title(*protoFileName), -1)
customTemplate = strings.Replace(string(customTemplate), "{provider-username}", *providerUsername, -1)
customTemplate = strings.Replace(string(customTemplate), "{microservices}", *name, -1)
err = ioutil.WriteFile(destinationFile, []byte(customTemplate), 0777)
if err != nil {
fmt.Println("Error while writing file ", destinationFile)
fmt.Println(err)
return
}
}