mirror of
https://github.com/cubixle/tuugen.git
synced 2026-04-30 16:18:46 +01:00
use the tuugen.yml file
This commit is contained in:
+14
-19
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/cubixle/tuugen"
|
"github.com/cubixle/tuugen"
|
||||||
@@ -12,39 +13,33 @@ import (
|
|||||||
var FS embed.FS
|
var FS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// read config file
|
||||||
|
d, err := ioutil.ReadFile("tuugen.yml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to find tuugen.yaml, %v", err)
|
||||||
|
}
|
||||||
|
cfg, err := tuugen.YamlToConfig(d)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to load config, %v", err)
|
||||||
|
}
|
||||||
// idea is to read a yaml file and create the project.
|
// idea is to read a yaml file and create the project.
|
||||||
err := tuugen.GenerateProtos()
|
err = tuugen.GenerateProtos()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
importPath := "github.com/cubixle/tuugen/example_project"
|
|
||||||
|
|
||||||
err = tuugen.GenerateService(
|
err = tuugen.GenerateService(FS, cfg)
|
||||||
FS,
|
|
||||||
"Service",
|
|
||||||
importPath,
|
|
||||||
"./internal/pb/service/service_grpc.pb.go",
|
|
||||||
"./internal/service/service.go",
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to generate service: %v", err)
|
log.Fatalf("failed to generate service: %v", err)
|
||||||
}
|
}
|
||||||
err = tuugen.GenerateInteractor(
|
err = tuugen.GenerateInteractor(FS, cfg)
|
||||||
FS,
|
|
||||||
"Service",
|
|
||||||
importPath,
|
|
||||||
"./internal/pb/service/service_grpc.pb.go",
|
|
||||||
"./internal/interactors/interactors.go",
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to generate interactor: %v", err)
|
log.Fatalf("failed to generate interactor: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tuugen.GoFmt(); err != nil {
|
if err := tuugen.GoFmt(); err != nil {
|
||||||
log.Fatalf("failed to run gofmt: %v", err)
|
log.Fatalf("failed to run gofmt: %v", err)
|
||||||
}
|
}
|
||||||
if err := tuugen.GoModInit(importPath); err != nil {
|
if err := tuugen.GoModInit(cfg.ImportPath); err != nil {
|
||||||
log.Fatalf("failed to run 'go mod init': %v", err)
|
log.Fatalf("failed to run 'go mod init': %v", err)
|
||||||
}
|
}
|
||||||
if err := tuugen.GoImports(); err != nil {
|
if err := tuugen.GoImports(); err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
type {{ .Name }} struct {
|
||||||
|
{{ range .Properties }}
|
||||||
|
{{ .Name }} {{ .Type }}
|
||||||
|
{{ end }}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package tuugen
|
||||||
|
|
||||||
|
import "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Project string `yaml:"project"`
|
||||||
|
ServiceName string `yaml:"service_name"`
|
||||||
|
ImportPath string `yaml:"import_path"`
|
||||||
|
ProtoFile string `yaml:"proto_file"`
|
||||||
|
DataModels []DataModel `yaml:"data_models"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func YamlToConfig(b []byte) (Config, error) {
|
||||||
|
cfg := Config{}
|
||||||
|
err := yaml.Unmarshal(b, &cfg)
|
||||||
|
return cfg, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package tuugen
|
||||||
|
|
||||||
|
type DataModel struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Properties []DataProperty `yaml:"properties"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DataProperty struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Type string `yaml:"type"`
|
||||||
|
AutoIncrement bool `yaml:"autoinc"`
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
project: tuugen_test_project
|
project: tuugen_test_project
|
||||||
import_path: github.com/cubixle/tuugen/example_project
|
import_path: github.com/cubixle/tuugen/example_project
|
||||||
proto: service.proto
|
proto: service.proto
|
||||||
|
service_name: Service
|
||||||
db_models:
|
db_models:
|
||||||
- name: User
|
- name: User
|
||||||
- properties:
|
- properties:
|
||||||
|
|||||||
@@ -2,4 +2,7 @@ module github.com/cubixle/tuugen
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require golang.org/x/tools v0.1.3 // indirect
|
require (
|
||||||
|
golang.org/x/tools v0.1.3 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -25,3 +25,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
|
|||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
|||||||
+18
-4
@@ -12,17 +12,31 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateService(FS embed.FS, serviceName, importPath, protoFilepath, outputFile string) error {
|
const grpcServiceFile = "./internal/pb/service/service_grpc.pb.go"
|
||||||
|
|
||||||
|
func GenerateService(FS embed.FS, cfg Config) error {
|
||||||
// read interface from project built protos.
|
// read interface from project built protos.
|
||||||
if err := createFileFromProto(FS, serviceName, "templates/service.go.tmpl", importPath, protoFilepath, outputFile); err != nil {
|
if err := createFileFromProto(FS,
|
||||||
|
cfg.ServiceName,
|
||||||
|
"templates/service.go.tmpl",
|
||||||
|
cfg.ImportPath,
|
||||||
|
grpcServiceFile,
|
||||||
|
"./internal/service/service.go",
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateInteractor(FS embed.FS, serviceName, importPath, protoFilepath, outputFile string) error {
|
func GenerateInteractor(FS embed.FS, cfg Config) error {
|
||||||
// read interface from project built protos.
|
// read interface from project built protos.
|
||||||
if err := createFileFromProto(FS, serviceName, "templates/interactors.go.tmpl", importPath, protoFilepath, outputFile); err != nil {
|
if err := createFileFromProto(FS,
|
||||||
|
cfg.ServiceName,
|
||||||
|
"templates/interactors.go.tmpl",
|
||||||
|
cfg.ImportPath,
|
||||||
|
grpcServiceFile,
|
||||||
|
"./internal/interactors/interactors.go",
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user