mirror of
https://github.com/cubixle/tuugen.git
synced 2026-04-24 19:54:45 +01:00
tidy up
This commit is contained in:
14
app.go
14
app.go
@@ -3,8 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateMain(FS embed.FS, cfg Config) error {
|
func GenerateMain(FS embed.FS, cfg Config) error {
|
||||||
@@ -14,18 +12,14 @@ func GenerateMain(FS embed.FS, cfg Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputFile := "cmd/app/main.go"
|
outputFile := "cmd/app/main.go"
|
||||||
fp := filepath.Dir(outputFile)
|
f, err := createFile(outputFile)
|
||||||
if err := os.MkdirAll(fp, 0777); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(outputFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.Execute(f, cfg); err != nil {
|
if err := t.Execute(f, cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return f.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
34
db.go
34
db.go
@@ -3,8 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,21 +12,21 @@ type DataModel struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DataProperty struct {
|
type DataProperty struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
AutoIncrement bool `yaml:"autoinc"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p DataProperty) NameCorrected() string {
|
func (p DataProperty) NameCorrected() string {
|
||||||
if strings.Contains(p.Name, "_") {
|
if !strings.Contains(p.Name, "_") {
|
||||||
parts := strings.Split(p.Name, "_")
|
return strings.Title(p.Name)
|
||||||
newParts := []string{}
|
|
||||||
for _, p := range parts {
|
|
||||||
newParts = append(newParts, strings.Title(p))
|
|
||||||
}
|
|
||||||
return strings.Join(newParts, "")
|
|
||||||
}
|
}
|
||||||
return strings.Title(p.Name)
|
|
||||||
|
parts := strings.Split(p.Name, "_")
|
||||||
|
newParts := []string{}
|
||||||
|
for _, p := range parts {
|
||||||
|
newParts = append(newParts, strings.Title(p))
|
||||||
|
}
|
||||||
|
return strings.Join(newParts, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p DataProperty) DBTypeToGoType() string {
|
func (p DataProperty) DBTypeToGoType() string {
|
||||||
@@ -52,15 +50,11 @@ func GenerateDataModels(FS embed.FS, cfg Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
outputFile := "internal/storage/" + strings.ToLower(dm.Name) + ".go"
|
outputFile := "internal/storage/" + strings.ToLower(dm.Name) + ".go"
|
||||||
fp := filepath.Dir(outputFile)
|
f, err := createFile(outputFile)
|
||||||
if err := os.MkdirAll(fp, 0777); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
f, err := os.Create(outputFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := t.Execute(f, dm); err != nil {
|
if err := t.Execute(f, dm); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ data_models:
|
|||||||
properties:
|
properties:
|
||||||
- name: id
|
- name: id
|
||||||
type: varchar
|
type: varchar
|
||||||
autoinc: true
|
|
||||||
- name: team_id
|
- name: team_id
|
||||||
type: varchar
|
type: varchar
|
||||||
- name: name
|
- name: name
|
||||||
|
|||||||
19
file.go
Normal file
19
file.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createFile(path string) (*os.File, error) {
|
||||||
|
fp := filepath.Dir(path)
|
||||||
|
if err := os.MkdirAll(fp, 0777); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
4
main.go
4
main.go
@@ -4,12 +4,14 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/*
|
//go:embed templates/*
|
||||||
var FS embed.FS
|
var FS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
tNow := time.Now()
|
||||||
// read config file
|
// read config file
|
||||||
d, err := ioutil.ReadFile("tuugen.yml")
|
d, err := ioutil.ReadFile("tuugen.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,6 +61,6 @@ func main() {
|
|||||||
log.Fatalf("failed to run 'goimports': %v", err)
|
log.Fatalf("failed to run 'goimports': %v", err)
|
||||||
}
|
}
|
||||||
log.Println("------")
|
log.Println("------")
|
||||||
log.Println("🎉 All setup has been complete enjoy working on your business logic.")
|
log.Printf("🎉 All setup has been complete enjoy working on your business logic. took: %s\n", time.Since(tNow))
|
||||||
log.Println("------")
|
log.Println("------")
|
||||||
}
|
}
|
||||||
|
|||||||
28
service.go
28
service.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -50,23 +49,6 @@ func (s serviceFuncDef) ArgsToStr() string {
|
|||||||
|
|
||||||
// generate the service/interactor template and store it to a file.
|
// generate the service/interactor template and store it to a file.
|
||||||
func createFileFromProto(FS embed.FS, cfg Config, templateFile, outputFile string) error {
|
func createFileFromProto(FS embed.FS, cfg Config, templateFile, outputFile string) error {
|
||||||
// load service template
|
|
||||||
t, err := template.ParseFS(FS, templateFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// check/create file path
|
|
||||||
fp := filepath.Dir(outputFile)
|
|
||||||
|
|
||||||
if err := os.MkdirAll(fp, 0777); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(outputFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
def, err := parseProto(cfg)
|
def, err := parseProto(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -77,6 +59,16 @@ func createFileFromProto(FS embed.FS, cfg Config, templateFile, outputFile strin
|
|||||||
def.Imports = append(def.Imports, cfg.ImportPath+"/internal/interactors")
|
def.Imports = append(def.Imports, cfg.ImportPath+"/internal/interactors")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, err := createFile(outputFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := template.ParseFS(FS, templateFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := t.Execute(f, def); err != nil {
|
if err := t.Execute(f, def); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user