mirror of
https://github.com/cubixle/tuugen.git
synced 2026-04-24 22:34:41 +01:00
use the tuugen.yml file
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/cubixle/tuugen"
|
||||
@@ -12,39 +13,33 @@ import (
|
||||
var FS embed.FS
|
||||
|
||||
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.
|
||||
err := tuugen.GenerateProtos()
|
||||
err = tuugen.GenerateProtos()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
importPath := "github.com/cubixle/tuugen/example_project"
|
||||
|
||||
err = tuugen.GenerateService(
|
||||
FS,
|
||||
"Service",
|
||||
importPath,
|
||||
"./internal/pb/service/service_grpc.pb.go",
|
||||
"./internal/service/service.go",
|
||||
)
|
||||
err = tuugen.GenerateService(FS, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to generate service: %v", err)
|
||||
}
|
||||
err = tuugen.GenerateInteractor(
|
||||
FS,
|
||||
"Service",
|
||||
importPath,
|
||||
"./internal/pb/service/service_grpc.pb.go",
|
||||
"./internal/interactors/interactors.go",
|
||||
)
|
||||
err = tuugen.GenerateInteractor(FS, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to generate interactor: %v", err)
|
||||
}
|
||||
|
||||
if err := tuugen.GoFmt(); err != nil {
|
||||
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)
|
||||
}
|
||||
if err := tuugen.GoImports(); err != nil {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package storage
|
||||
|
||||
type {{ .Name }} struct {
|
||||
{{ range .Properties }}
|
||||
{{ .Name }} {{ .Type }}
|
||||
{{ end }}
|
||||
}
|
||||
17
config.go
Normal file
17
config.go
Normal file
@@ -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
|
||||
}
|
||||
12
db.go
Normal file
12
db.go
Normal file
@@ -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
|
||||
import_path: github.com/cubixle/tuugen/example_project
|
||||
proto: service.proto
|
||||
service_name: Service
|
||||
db_models:
|
||||
- name: User
|
||||
- properties:
|
||||
|
||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module github.com/cubixle/tuugen
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
3
go.sum
3
go.sum
@@ -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-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
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=
|
||||
|
||||
22
service.go
22
service.go
@@ -12,17 +12,31 @@ import (
|
||||
"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.
|
||||
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 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.
|
||||
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 nil
|
||||
|
||||
Reference in New Issue
Block a user