Skip to content

Commit dbab471

Browse files
committed
Cleaned up older functions to use templating util
1 parent 6a4960d commit dbab471

File tree

4 files changed

+20
-174
lines changed

4 files changed

+20
-174
lines changed

cmd/create.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88

99
"github.com/commitdev/commit0/internal/templator"
10+
"github.com/commitdev/commit0/internal/util"
1011
"github.com/gobuffalo/packr/v2"
1112
"github.com/spf13/cobra"
1213
)
@@ -25,33 +26,10 @@ func Create(projectName string, outDir string, t *templator.Templator) string {
2526
} else if err != nil {
2627
log.Fatalf("Error creating root: %v ", err)
2728
}
28-
29-
commit0ConfigPath := path.Join(rootDir, "commit0.yml")
30-
log.Printf("%s", commit0ConfigPath)
31-
3229
var wg sync.WaitGroup
33-
wg.Add(1)
34-
go func() {
35-
f, err := os.Create(commit0ConfigPath)
36-
if err != nil {
37-
log.Printf("Error creating commit0 config: %v", err)
38-
}
39-
40-
t.Commit0.Execute(f, projectName)
41-
wg.Done()
42-
}()
43-
44-
wg.Add(1)
45-
go func() {
46-
gitIgnorePath := path.Join(rootDir, ".gitignore")
47-
f, err := os.Create(gitIgnorePath)
48-
if err != nil {
49-
log.Printf("Error creating commit0 config: %v", err)
50-
}
5130

52-
t.GitIgnore.Execute(f, projectName)
53-
wg.Done()
54-
}()
31+
util.TemplateFileIfDoesNotExist(rootDir, "commit0.yml", t.Commit0, wg, projectName)
32+
util.TemplateFileIfDoesNotExist(rootDir, ".gitignore", t.GitIgnore, wg, projectName)
5533

5634
wg.Wait()
5735
return rootDir

cmd/generate.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"log"
5-
"os"
65
"sync"
76

87
"github.com/commitdev/commit0/internal/config"
@@ -12,6 +11,7 @@ import (
1211
"github.com/commitdev/commit0/internal/generate/proto"
1312
"github.com/commitdev/commit0/internal/generate/react"
1413
"github.com/commitdev/commit0/internal/templator"
14+
"github.com/commitdev/commit0/internal/util"
1515
"github.com/gobuffalo/packr/v2"
1616
"github.com/spf13/cobra"
1717
)
@@ -61,16 +61,7 @@ var generateCmd = &cobra.Command{
6161
react.Generate(t, cfg, wg)
6262
}
6363

64-
f, err := os.Create("README.md")
65-
if err != nil {
66-
log.Printf("Error creating commit0 config: %v", err)
67-
}
68-
69-
wg.Add(1)
70-
go func() {
71-
t.Readme.Execute(f, cfg)
72-
wg.Done()
73-
}()
64+
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, wg, cfg)
7465

7566
if cfg.Network.Http.Enabled {
7667
http.GenerateHTTPGW(t, cfg, wg)

internal/generate/golang/generate.go

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package golang
33
import (
44
"fmt"
55
"log"
6-
"os"
6+
"path/filepath"
77
"sync"
88

99
"github.com/commitdev/commit0/internal/config"
@@ -12,39 +12,12 @@ import (
1212
)
1313

1414
func Generate(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
15-
GenerateGoMain(templator, config, wg)
16-
GenerateGoMod(templator, config, wg)
17-
GenerateHealthServer(templator, config, wg)
15+
util.TemplateFileIfDoesNotExist("", "main.go", templator.Go.GoMain, wg, config)
16+
util.TemplateFileIfDoesNotExist("", "go.mod", templator.Go.GoMod, wg, config)
17+
util.TemplateFileIfDoesNotExist("server/health", "health.go", templator.Go.GoHealthServer, wg, config)
1818
GenerateServers(templator, config, wg)
1919
}
2020

21-
func GenerateGoMain(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
22-
if _, err := os.Stat("main.go"); os.IsNotExist(err) {
23-
24-
f, err := os.Create("main.go")
25-
26-
if err != nil {
27-
log.Printf("Error: %v", err)
28-
}
29-
30-
wg.Add(1)
31-
go templator.Go.GoMain.Execute(f, config)
32-
} else {
33-
log.Printf("main.go already exists. skipping.")
34-
}
35-
}
36-
37-
func GenerateGoMod(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
38-
f, err := os.Create("go.mod")
39-
40-
if err != nil {
41-
log.Printf("Error: %v", err)
42-
}
43-
44-
wg.Add(1)
45-
go templator.Go.GoMod.Execute(f, config)
46-
}
47-
4821
func GenerateServers(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
4922
serverDirPath := "server"
5023
err := util.CreateDirIfDoesNotExist(serverDirPath)
@@ -53,56 +26,16 @@ func GenerateServers(templator *templator.Templator, config *config.Commit0Confi
5326
}
5427

5528
for _, s := range config.Services {
56-
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, s.Name)
57-
err := os.Mkdir(serverLibPath, os.ModePerm)
58-
if os.IsExist(err) {
59-
log.Printf("%s server exists skipping.", s.Name)
60-
continue
61-
}
62-
log.Printf("generating %s", s.Name)
63-
if err != nil {
64-
log.Printf("Error generating server: %v", err)
65-
}
66-
67-
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, s.Name)
68-
f, err := os.Create(serverFilePath)
69-
70-
if err != nil {
71-
log.Printf("Error: %v", err)
72-
}
29+
path := filepath.Join("server", s.Name)
30+
file := fmt.Sprintf("%s.go", s.Name)
7331

7432
data := map[string]string{
7533
"ProjectName": config.Name,
7634
"ServiceName": s.Name,
7735
"GitRepo": config.GitRepo,
7836
}
7937

80-
wg.Add(1)
81-
go templator.Go.GoServer.Execute(f, data)
82-
}
83-
84-
}
85-
86-
func GenerateHealthServer(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
87-
serverDirPath := "server"
88-
err := util.CreateDirIfDoesNotExist(serverDirPath)
89-
if err != nil {
90-
log.Printf("Error creating server path: %v", err)
91-
}
92-
93-
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, "health")
94-
err = util.CreateDirIfDoesNotExist(serverLibPath)
95-
if err != nil {
96-
log.Printf("Error generating server: %v", err)
97-
}
98-
99-
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, "health")
100-
f, err := os.Create(serverFilePath)
101-
102-
if err != nil {
103-
log.Printf("Error: %v", err)
38+
util.TemplateFileIfDoesNotExist(path, file, templator.Go.GoServer, wg, data)
10439
}
10540

106-
wg.Add(1)
107-
go templator.Go.GoHealthServer.Execute(f, config)
10841
}

internal/generate/proto/generate.go

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"log"
7-
"os"
87
"os/exec"
98
"sync"
109

@@ -14,76 +13,22 @@ import (
1413
)
1514

1615
func Generate(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
17-
GenerateIDLMakefile(templator, config, wg)
18-
GenerateProtoHealth(templator, config, wg)
19-
GenerateServiceProtobufFiles(templator, config, wg)
20-
GenerateGoModIDL(templator, config, wg)
21-
GenerateProtoServiceLibs(config)
22-
}
23-
24-
func GenerateGoModIDL(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
2516
idlPath := fmt.Sprintf("%s-idl", config.Name)
26-
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)
27-
err := util.CreateDirIfDoesNotExist(idlPath)
28-
f, err := os.Create(idlOutput)
17+
idlHealthPath := fmt.Sprintf("%s/proto/health", idlPath)
2918

30-
if err != nil {
31-
log.Printf("Error: %v", err)
32-
}
33-
34-
wg.Add(1)
35-
go templator.Go.GoModIDL.Execute(f, config)
36-
}
37-
38-
func GenerateIDLMakefile(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
39-
makeFilePath := fmt.Sprintf("%s-idl", config.Name)
40-
makeFileOutput := fmt.Sprintf("%s/Makefile", makeFilePath)
41-
42-
err := util.CreateDirIfDoesNotExist(makeFilePath)
43-
if err != nil {
44-
log.Printf("Error generating prototool config: %v", err)
45-
}
46-
47-
f, err := os.Create(makeFileOutput)
48-
if err != nil {
49-
log.Printf("Error: %v", err)
50-
}
51-
52-
wg.Add(1)
53-
go templator.MakefileTemplate.Execute(f, config)
54-
}
55-
56-
func GenerateProtoHealth(templator *templator.Templator, config *config.Commit0Config, wg sync.WaitGroup) {
57-
protoHealthPath := fmt.Sprintf("%s-idl/proto/health", config.Name)
58-
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)
59-
60-
err := util.CreateDirIfDoesNotExist(protoHealthPath)
61-
if err != nil {
62-
log.Printf("Error: %v", err)
63-
}
64-
65-
f, err := os.Create(protoHealthOutput)
66-
if err != nil {
67-
log.Printf("Error: %v", err)
68-
}
19+
util.TemplateFileIfDoesNotExist(idlPath, "go.mod", templator.Go.GoModIDL, wg, config)
20+
util.TemplateFileIfDoesNotExist(idlPath, "Makefile", templator.MakefileTemplate, wg, config)
21+
GenerateServiceProtobufFiles(templator, config, wg)
22+
util.TemplateFileIfDoesNotExist(idlHealthPath, "health.proto", templator.ProtoHealthTemplate, wg, config)
6923

70-
wg.Add(1)
71-
go templator.ProtoHealthTemplate.Execute(f, config)
24+
GenerateProtoServiceLibs(config)
7225
}
7326

7427
func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.Commit0Config, wg sync.WaitGroup) {
7528
protoPath := fmt.Sprintf("%s-idl/proto", cfg.Name)
7629
for _, s := range cfg.Services {
7730
serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name)
78-
err := os.Mkdir(serviceProtoDir, os.ModePerm)
79-
if os.IsExist(err) {
80-
log.Printf("%s service proto exists skipping.", s.Name)
81-
continue
82-
}
83-
84-
serviceProtoFilePath := fmt.Sprintf("%s/%s.proto", serviceProtoDir, s.Name)
85-
86-
f, err := os.Create(serviceProtoFilePath)
31+
file := fmt.Sprintf("%s.proto", s.Name)
8732

8833
data := struct {
8934
*config.Commit0Config
@@ -93,8 +38,7 @@ func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.Co
9338
s.Name,
9439
}
9540

96-
wg.Add(1)
97-
go templator.ProtoServiceTemplate.Execute(f, data)
41+
util.TemplateFileIfDoesNotExist(serviceProtoDir, file, templator.ProtoServiceTemplate, wg, data)
9842
}
9943
}
10044

0 commit comments

Comments
 (0)