diff --git a/cmd/tuugen/main.go b/cmd/tuugen/main.go index e58fbad..d523711 100644 --- a/cmd/tuugen/main.go +++ b/cmd/tuugen/main.go @@ -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 { diff --git a/cmd/tuugen/templates/data_obj.go.tmpl b/cmd/tuugen/templates/data_obj.go.tmpl index e69de29..ec4f8bb 100644 --- a/cmd/tuugen/templates/data_obj.go.tmpl +++ b/cmd/tuugen/templates/data_obj.go.tmpl @@ -0,0 +1,7 @@ +package storage + +type {{ .Name }} struct { + {{ range .Properties }} + {{ .Name }} {{ .Type }} + {{ end }} +} \ No newline at end of file diff --git a/config.go b/config.go new file mode 100644 index 0000000..39494f4 --- /dev/null +++ b/config.go @@ -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 +} diff --git a/db.go b/db.go new file mode 100644 index 0000000..5f0ff7c --- /dev/null +++ b/db.go @@ -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"` +} diff --git a/example_project/tuugen.yml b/example_project/tuugen.yml index e1d0f91..66d2aeb 100644 --- a/example_project/tuugen.yml +++ b/example_project/tuugen.yml @@ -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: diff --git a/go.mod b/go.mod index 8cfdfb2..1cb2a44 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index e652f3e..764f06e 100644 --- a/go.sum +++ b/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= diff --git a/service.go b/service.go index 9b3885d..567d296 100644 --- a/service.go +++ b/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