Skip to content

Commit

Permalink
feat: generate injection
Browse files Browse the repository at this point in the history
fix: migrate log to logrus
  • Loading branch information
Hidayat Hamir committed Feb 7, 2024
1 parent ab80403 commit 0aa0067
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ go install github.com/spacetronot-research-team/erago@latest
or you can define your prefered version.

```shell
go install github.com/spacetronot-research-team/erago@v0.0.12
go install github.com/spacetronot-research-team/erago@v0.0.13
```

Or you can download erago binary from [release page](https://github.com/spacetronot-research-team/erago/releases).
Expand Down
90 changes: 69 additions & 21 deletions cmd/createdomain/create_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,75 @@ import (
"bufio"
"errors"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/iancoleman/strcase"
"github.com/sirupsen/logrus"
"github.com/spacetronot-research-team/erago/cmd/createdomain/template"
"github.com/spacetronot-research-team/erago/common/gomod"
)

// CreateDomain is main func to create new domain.
func CreateDomain(domain string) {
log.Println("create domain start")
logrus.Println("create domain start")

log.Println("get module name")
logrus.Println("get module name")
moduleName, err := getModuleName()
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

varErr1 := fmt.Sprintf("%d", rand.Int())
varErr2 := fmt.Sprintf("%d", rand.Int())

log.Println("generate controller template")
logrus.Println("generate controller template")
if err := generateControllerTemplate(domain, moduleName, varErr1); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

log.Println("generate service template")
logrus.Println("generate service template")
if err := generateServiceTemplate(domain, moduleName, varErr1, varErr2); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

log.Println("generate repository template")
logrus.Println("generate repository template")
if err := generateRepositoryTemplate(domain, varErr1, varErr2); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

log.Println("generate mock repository using mockgen")
logrus.Println("generate injection")
if err := generateInjectionTemplate(domain, moduleName); err != nil {
logrus.Fatal(err)
}

logrus.Println("generate mock repository using mockgen")
if err := generateMockRepository(domain); err != nil {
log.Println("err generate mock repository using mockgen, did you able to run `mockgen`?")
log.Println("try to install mockgen:\n\tgo install go.uber.org/mock/mockgen@latest")
log.Println("create domain finish without mock repository and service test template")
logrus.Println("err generate mock repository using mockgen, did you able to run `mockgen`?")
logrus.Println("try to install mockgen:\n\tgo install go.uber.org/mock/mockgen@latest")
logrus.Println("create domain finish without mock repository and service test template")
return
}

log.Println("generate service test template")
logrus.Println("generate service test template")
if err := generateServiceTestTemplate(domain, moduleName, varErr1, varErr2); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

log.Println("generate mock service using mockgen")
logrus.Println("generate mock service using mockgen")
if err := generateMockService(domain); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

log.Println("run go mod tidy")
logrus.Println("run go mod tidy")
if err := gomod.RunGoModTidy(); err != nil {
log.Fatal(fmt.Errorf("err run go mod tidy: %v", err))
logrus.Fatal(fmt.Errorf("err run go mod tidy: %v", err))
}

log.Println("create domain finish")
logrus.Println("create domain finish")
}

func generateMockRepository(domain string) error {
Expand Down Expand Up @@ -200,3 +205,46 @@ func generateRepositoryTemplate(domain string, varErr1 string, varErr2 string) e

return nil
}

func generateInjectionTemplate(domain string, moduleName string) error {
// TODO: check if file exists
// TODO: if exists, append
// TODO: if not, create new
path := filepath.Join("internal", "router", "injection.go")
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("err open injection.go file: %err", err)
}

if err = generateNewInjectionTemplate(domain, moduleName, path); err != nil {
return fmt.Errorf("err generate new injection template: %v", err)
}

return nil
}
defer f.Close()

injectionAppendTemplate, err := template.GetInjectionAppendTemplate(domain)
if err != nil {
return fmt.Errorf("err append injection: %v", err)
}

if _, err := f.WriteString(injectionAppendTemplate); err != nil {
return fmt.Errorf("err write injection append: %v", err)
}

return nil
}

func generateNewInjectionTemplate(domain string, moduleName string, path string) error {
injectionTemplate, err := template.GetInjectionTemplate(domain, moduleName)
if err != nil {
return fmt.Errorf("err get injection template: %v", err)
}
err = os.WriteFile(path, []byte(injectionTemplate), 0666)
if err != nil {
return fmt.Errorf("err write injection template: %v", err)
}
return nil
}
56 changes: 56 additions & 0 deletions cmd/createdomain/template/injection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package template

import (
"bytes"
"fmt"
"text/template"

"github.com/iancoleman/strcase"
)

func GetInjectionTemplate(domain string, moduleName string) (string, error) {
injectionConfig := NewInjectionConfig(domain)
injectionConfig.ModuleName = moduleName

injectionTemplate, err := template.New("injectionTemplate").Parse(injectionTemplate)
if err != nil {
return "", fmt.Errorf("err parse template injectionTemplate: %v", err)
}

var templateBuf bytes.Buffer
if err = injectionTemplate.Execute(&templateBuf, injectionConfig); err != nil {
return "", fmt.Errorf("err create template: %v", err)
}

return templateBuf.String(), nil
}

type InjectionConfig struct {
DomainPascalCase string
DomainCamelCase string
ModuleName string
}

func NewInjectionConfig(domain string) InjectionConfig {
return InjectionConfig{
DomainPascalCase: strcase.ToCamel(domain),
DomainCamelCase: strcase.ToLowerCamel(domain),
}
}

var injectionTemplate = `package router
import (
"gorm.io/gorm"
"{{.ModuleName}}/internal/controller/http"
"{{.ModuleName}}/internal/repository"
"{{.ModuleName}}/internal/service"
)
func get{{.DomainPascalCase}}Controller(db *gorm.DB) *http.{{.DomainPascalCase}}Controller {
{{.DomainCamelCase}}Repository := repository.New{{.DomainPascalCase}}Repository(db)
{{.DomainCamelCase}}Service := service.New{{.DomainPascalCase}}Service({{.DomainCamelCase}}Repository)
{{.DomainCamelCase}}Controller := http.New{{.DomainPascalCase}}Controller({{.DomainCamelCase}}Service)
return {{.DomainCamelCase}}Controller
}
`
46 changes: 46 additions & 0 deletions cmd/createdomain/template/injection_append.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package template

import (
"bytes"
"fmt"
"text/template"

"github.com/iancoleman/strcase"
)

func GetInjectionAppendTemplate(domain string) (string, error) {
injectionAppendConfig := NewInjectionConfigAppend(domain)

injectionAppendTemplate, err := template.New("injectionAppendTemplate").Parse(injectionAppendTemplate)
if err != nil {
return "", fmt.Errorf("err parse template injectionAppendTemplate: %v", err)
}

var templateBuf bytes.Buffer
if err = injectionAppendTemplate.Execute(&templateBuf, injectionAppendConfig); err != nil {
return "", fmt.Errorf("err create template: %v", err)
}

return templateBuf.String(), nil
}

type InjectionConfigAppend struct {
DomainPascalCase string
DomainCamelCase string
}

func NewInjectionConfigAppend(domain string) InjectionConfigAppend {
return InjectionConfigAppend{
DomainPascalCase: strcase.ToCamel(domain),
DomainCamelCase: strcase.ToLowerCamel(domain),
}
}

var injectionAppendTemplate = `
func get{{.DomainPascalCase}}Controller(db *gorm.DB) *http.{{.DomainPascalCase}}Controller {
{{.DomainCamelCase}}Repository := repository.New{{.DomainPascalCase}}Repository(db)
{{.DomainCamelCase}}Service := service.New{{.DomainPascalCase}}Service({{.DomainCamelCase}}Repository)
{{.DomainCamelCase}}Controller := http.New{{.DomainPascalCase}}Controller({{.DomainCamelCase}}Service)
return {{.DomainCamelCase}}Controller
}
`
49 changes: 27 additions & 22 deletions cmd/createproject/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,74 @@ package createproject

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spacetronot-research-team/erago/cmd/createdomain"
"github.com/spacetronot-research-team/erago/common/gomod"
)

// CreateProject is main func to create new project.
func CreateProject(projectName string, moduleName string) {
log.Println("create project start")
logrus.Println("create project start")

log.Println("create project dir")
logrus.Println("create project dir")
projectPath := filepath.Join(".", projectName)
if err := os.MkdirAll(projectPath, os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err mkdir projectPath: %v", err))
logrus.Fatal(fmt.Errorf("err mkdir projectPath: %v", err))
}

log.Println("run go mod init in project dir")
logrus.Println("run go mod init in project dir")
if err := runGoModInit(moduleName, projectPath); err != nil {
log.Fatal(fmt.Errorf("err run go mod init in project path: %v", err))
logrus.Fatal(fmt.Errorf("err run go mod init in project path: %v", err))
}

log.Println("create internal/controller/http dir in project dir")
logrus.Println("create internal/controller/http dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "internal", "controller", "http"), os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err mkdir projectPath/internal/controller/http/: %v", err))
logrus.Fatal(fmt.Errorf("err mkdir projectPath/internal/controller/http/: %v", err))
}

log.Println("create internal/service dir in project dir")
logrus.Println("create internal/service dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "internal", "service"), os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err mkdir projectPath/internal/service: %v", err))
logrus.Fatal(fmt.Errorf("err mkdir projectPath/internal/service: %v", err))
}

log.Println("create internal/repository dir in project dir")
logrus.Println("create internal/repository dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "internal", "repository"), os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err mkdir projectPath/internal/repository: %v", err))
logrus.Fatal(fmt.Errorf("err mkdir projectPath/internal/repository: %v", err))
}

log.Println("create cmd dir in project dir")
logrus.Println("create internal/router dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "internal", "router"), os.ModePerm); err != nil {
logrus.Fatal(fmt.Errorf("err mkdir projectPath/internal/router: %v", err))
}

logrus.Println("create cmd dir in project dir")
if err := os.MkdirAll(filepath.Join(projectPath, "cmd"), os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err mkdir projectPath/cmd: %v", err))
logrus.Fatal(fmt.Errorf("err mkdir projectPath/cmd: %v", err))
}

log.Println("create main.go file in cmd dir")
logrus.Println("create main.go file in cmd dir")
if err := os.WriteFile(filepath.Join(projectPath, "cmd", "main.go"), []byte("package main\n"), os.ModePerm); err != nil {
log.Fatal(fmt.Errorf("err write file projectPath/cmd/main.go: %v", err))
logrus.Fatal(fmt.Errorf("err write file projectPath/cmd/main.go: %v", err))
}

log.Println("change current dir to project dir")
logrus.Println("change current dir to project dir")
if err := os.Chdir(projectPath); err != nil {
log.Fatal(fmt.Errorf("err change work dir to projectPath: %v", err))
logrus.Fatal(fmt.Errorf("err change work dir to projectPath: %v", err))
}

log.Println("create domain hello world")
logrus.Println("create domain hello world")
createdomain.CreateDomain("hello world")

log.Println("run go mod tidy")
logrus.Println("run go mod tidy")
if err := gomod.RunGoModTidy(); err != nil {
log.Fatal(fmt.Errorf("err run go mod tidy: %v", err))
logrus.Fatal(fmt.Errorf("err run go mod tidy: %v", err))
}

log.Println("create project finish, go to your project:\n\tcd", projectPath)
logrus.Println("create project finish, go to your project:\n\tcd", projectPath)
}

func runGoModInit(moduleName string, projectPath string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package version
import "fmt"

func Version() {
fmt.Println("v0.0.12")
fmt.Println("v0.0.13")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.21.6

require (
github.com/iancoleman/strcase v0.3.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.15.0 // indirect
)
Loading

0 comments on commit 0aa0067

Please sign in to comment.