Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up templator with a helper function, add readme (not filled in … #54

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 6 additions & 15 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"log"
"os"
"path"
"sync"

"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
)
Expand All @@ -24,23 +26,12 @@ func Create(projectName string, outDir string, t *templator.Templator) string {
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}
var wg sync.WaitGroup

commit0ConfigPath := path.Join(rootDir, "commit0.yml")
log.Printf("%s", commit0ConfigPath)

f, err := os.Create(commit0ConfigPath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
t.Commit0.Execute(f, projectName)

gitIgnorePath := path.Join(rootDir, ".gitignore")
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
t.GitIgnore.Execute(f, projectName)
util.TemplateFileIfDoesNotExist(rootDir, "commit0.yml", t.Commit0, &wg, projectName)
util.TemplateFileIfDoesNotExist(rootDir, ".gitignore", t.GitIgnore, &wg, projectName)

wg.Wait()
return rootDir
}

Expand Down
22 changes: 15 additions & 7 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/generate/docker"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/commitdev/commit0/internal/generate/proto"
"github.com/commitdev/commit0/internal/generate/react"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -47,20 +49,26 @@ var generateCmd = &cobra.Command{
cfg.Language = language
cfg.Print()

var wg sync.WaitGroup
switch language {
case Go:
proto.Generate(t, cfg)
golang.Generate(t, cfg)
docker.GenerateGoAppDockerFile(t, cfg)
docker.GenerateGoDockerCompose(t, cfg)
proto.Generate(t, cfg, &wg)
golang.Generate(t, cfg, &wg)

docker.GenerateGoAppDockerFile(t, cfg, &wg)
docker.GenerateGoDockerCompose(t, cfg, &wg)
case React:
react.Generate(t, cfg)
react.Generate(t, cfg, &wg)
}

util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, cfg)

if cfg.Network.Http.Enabled {
http.GenerateHttpGW(t, cfg)
docker.GenerateGoHttpGWDockerFile(t, cfg)
http.GenerateHTTPGW(t, cfg, &wg)
docker.GenerateGoHTTPGWDockerFile(t, cfg, &wg)
}

wg.Wait()
},
}

Expand Down
14 changes: 8 additions & 6 deletions internal/generate/docker/generate.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package docker

import (
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
)

func GenerateGoAppDockerFile(templator *templator.Templator, config *config.Commit0Config) {
util.TemplateFileIfDoesNotExist("docker/app", "Dockerfile", templator.Docker.ApplicationDocker, config)
func GenerateGoAppDockerFile(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
util.TemplateFileIfDoesNotExist("docker/app", "Dockerfile", templator.Docker.ApplicationDocker, wg, config)
}

func GenerateGoHttpGWDockerFile(templator *templator.Templator, config *config.Commit0Config) {
util.TemplateFileIfDoesNotExist("docker/http", "Dockerfile", templator.Docker.HttpGatewayDocker, config)
func GenerateGoHTTPGWDockerFile(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
util.TemplateFileIfDoesNotExist("docker/http", "Dockerfile", templator.Docker.HttpGatewayDocker, wg, config)
}

func GenerateGoDockerCompose(templator *templator.Templator, config *config.Commit0Config) {
util.TemplateFileIfDoesNotExist("", "docker-compose.yml", templator.Docker.DockerCompose, config)
func GenerateGoDockerCompose(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
util.TemplateFileIfDoesNotExist("", "docker-compose.yml", templator.Docker.DockerCompose, wg, config)
}
84 changes: 11 additions & 73 deletions internal/generate/golang/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,39 @@ package golang
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
)

func Generate(templator *templator.Templator, config *config.Commit0Config) {
GenerateGoMain(templator, config)
GenerateGoMod(templator, config)
GenerateHealthServer(templator, config)
GenerateServers(templator, config)
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
util.TemplateFileIfDoesNotExist("", "main.go", templator.Go.GoMain, wg, config)
util.TemplateFileIfDoesNotExist("", "go.mod", templator.Go.GoMod, wg, config)
util.TemplateFileIfDoesNotExist("server/health", "health.go", templator.Go.GoHealthServer, wg, config)
GenerateServers(templator, config, wg)
}

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) {
func GenerateServers(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
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)
}
path := filepath.Join("server", s.Name)
file := fmt.Sprintf("%s.go", s.Name)

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)
util.TemplateFileIfDoesNotExist(path, file, templator.Go.GoServer, wg, data)
}

templator.Go.GoHealthServer.Execute(f, config)
}
6 changes: 4 additions & 2 deletions internal/generate/http/generate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package http

import (
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
)

func GenerateHttpGW(templator *templator.Templator, config *config.Commit0Config) {
util.TemplateFileAndOverwrite("http", "main.go", templator.Go.GoHttpGW, config)
func GenerateHTTPGW(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
util.TemplateFileAndOverwrite("http", "main.go", templator.Go.GoHTTPGW, wg, config)
}
73 changes: 11 additions & 62 deletions internal/generate/proto/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,31 @@ import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
)

func Generate(templator *templator.Templator, config *config.Commit0Config) {
GenerateIDLMakefile(templator, config)
GenerateProtoHealth(templator, config)
GenerateServiceProtobufFiles(templator, config)
GenerateProtoServiceLibs(config)
GenerateGoModIDL(templator, config)
}

func GenerateGoModIDL(templator *templator.Templator, config *config.Commit0Config) {
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
idlPath := fmt.Sprintf("%s-idl", config.Name)
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)
err := util.CreateDirIfDoesNotExist(idlPath)
f, err := os.Create(idlOutput)

if err != nil {
log.Printf("Error: %v", err)
}

templator.Go.GoModIDL.Execute(f, config)
}

func GenerateIDLMakefile(templator *templator.Templator, config *config.Commit0Config) {
makeFilePath := fmt.Sprintf("%s-idl", config.Name)
makeFileOutput := fmt.Sprintf("%s/Makefile", makeFilePath)
idlHealthPath := fmt.Sprintf("%s/proto/health", idlPath)

err := util.CreateDirIfDoesNotExist(makeFilePath)
if err != nil {
log.Printf("Error generating prototool config: %v", err)
}
util.TemplateFileIfDoesNotExist(idlPath, "go.mod", templator.Go.GoModIDL, wg, config)
util.TemplateFileIfDoesNotExist(idlPath, "Makefile", templator.MakefileTemplate, wg, config)
GenerateServiceProtobufFiles(templator, config, wg)
util.TemplateFileIfDoesNotExist(idlHealthPath, "health.proto", templator.ProtoHealthTemplate, wg, config)

f, err := os.Create(makeFileOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.MakefileTemplate.Execute(f, config)
}

func GenerateProtoHealth(templator *templator.Templator, config *config.Commit0Config) {
protoHealthPath := fmt.Sprintf("%s-idl/proto/health", config.Name)
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)

err := util.CreateDirIfDoesNotExist(protoHealthPath)
if err != nil {
log.Printf("Error: %v", err)
}

f, err := os.Create(protoHealthOutput)
if err != nil {
log.Printf("Error: %v", err)
}

templator.ProtoHealthTemplate.Execute(f, config)
GenerateProtoServiceLibs(config)
}

func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.Commit0Config) {
func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup) {
protoPath := fmt.Sprintf("%s-idl/proto", cfg.Name)
for _, s := range cfg.Services {
serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name)
err := os.Mkdir(serviceProtoDir, os.ModePerm)
if os.IsExist(err) {
log.Printf("%s service proto exists skipping.", s.Name)
continue
}

serviceProtoFilePath := fmt.Sprintf("%s/%s.proto", serviceProtoDir, s.Name)

f, err := os.Create(serviceProtoFilePath)
file := fmt.Sprintf("%s.proto", s.Name)

data := struct {
*config.Commit0Config
Expand All @@ -88,9 +38,8 @@ func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.Co
s.Name,
}

templator.ProtoServiceTemplate.Execute(f, data)
util.TemplateFileIfDoesNotExist(serviceProtoDir, file, templator.ProtoServiceTemplate, wg, data)
}

}

func GenerateProtoServiceLibs(config *config.Commit0Config) {
Expand Down
6 changes: 4 additions & 2 deletions internal/generate/react/generate.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package react

import (
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config) {
templator.React.TemplateFiles(config, false)
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
templator.React.TemplateFiles(config, false, wg)
}