Skip to content

Commit

Permalink
add flag -p pb_path, -i pb_import_path
Browse files Browse the repository at this point in the history
  • Loading branch information
chaseSpace committed Sep 6, 2020
1 parent e4ad444 commit 5f7661d
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 28 deletions.
22 changes: 22 additions & 0 deletions cmd/g_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/kujtimiihoxha/kit/generator"
"github.com/kujtimiihoxha/kit/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -22,6 +23,21 @@ var initserviceCmd = &cobra.Command{
return
}
}
var pbPath, pbImportPath string
pbPath = viper.GetString("g_s_pb_path")
pbImportPath = viper.GetString("g_s_pb_import_path")
if pbPath != "" {
exist := utils.IsExist(pbPath)
if !exist {
logrus.Errorf("You must provide a existed pb path to store pb dir, given path:<%s> is not exist", pbPath)
return
}
if pbImportPath == "" {
logrus.Error("You must provide pb import path by --pb_import_path or -i, because you provide a pb_path")
return
}
}

var emw, smw bool
if viper.GetBool("g_s_dmw") {
emw = true
Expand All @@ -33,6 +49,8 @@ var initserviceCmd = &cobra.Command{
g := generator.NewGenerateService(
args[0],
viper.GetString("g_s_transport"),
pbPath,
pbImportPath,
smw,
viper.GetBool("g_s_gorilla"),
emw,
Expand All @@ -47,12 +65,16 @@ var initserviceCmd = &cobra.Command{
func init() {
generateCmd.AddCommand(initserviceCmd)
initserviceCmd.Flags().StringP("transport", "t", "http", "The transport you want your service to be initiated with")
initserviceCmd.Flags().StringP("pb_path", "p", "", "Specify path to store pb dir")
initserviceCmd.Flags().StringP("pb_import_path", "i", "", "Specify path to import pb")
initserviceCmd.Flags().BoolP("dmw", "w", false, "Generate default middleware for service and endpoint")
initserviceCmd.Flags().Bool("gorilla", false, "Generate http using gorilla mux")
initserviceCmd.Flags().StringArrayVarP(&methods, "methods", "m", []string{}, "Specify methods to be generated")
initserviceCmd.Flags().Bool("svc-mdw", false, "If set a default Logging and Instrumental middleware will be created and attached to the service")
initserviceCmd.Flags().Bool("endpoint-mdw", false, "If set a default Logging and Tracking middleware will be created and attached to the endpoint")
viper.BindPFlag("g_s_transport", initserviceCmd.Flags().Lookup("transport"))
viper.BindPFlag("g_s_pb_path", initserviceCmd.Flags().Lookup("pb_path"))
viper.BindPFlag("g_s_pb_import_path", initserviceCmd.Flags().Lookup("pb_import_path"))
viper.BindPFlag("g_s_dmw", initserviceCmd.Flags().Lookup("dmw"))
viper.BindPFlag("g_s_gorilla", initserviceCmd.Flags().Lookup("gorilla"))
viper.BindPFlag("g_s_svc_mdw", initserviceCmd.Flags().Lookup("svc-mdw"))
Expand Down
40 changes: 27 additions & 13 deletions generator/add_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ type GenerateTransport struct {
gorillaMux bool
interfaceName string
destPath string
pbPath string
pbImportPath string
methods []string
filePath string
file *parser.File
serviceInterface parser.Interface
}

// NewGenerateTransport returns a transport generator.
func NewGenerateTransport(name string, gorillaMux bool, transport string, methods []string) Gen {
func NewGenerateTransport(name string, gorillaMux bool, transport, pbPath, pbImportPath string, methods []string) Gen {
i := &GenerateTransport{
name: name,
gorillaMux: gorillaMux,
Expand All @@ -50,6 +52,10 @@ func NewGenerateTransport(name string, gorillaMux bool, transport string, method
}
i.filePath = path.Join(i.destPath, viper.GetString("gk_service_file_name"))
i.transport = transport
// Customize pb path
i.pbPath = pbPath
i.pbImportPath = pbImportPath

// Not used.
i.srcFile = jen.NewFilePath("")
i.InitPg()
Expand Down Expand Up @@ -99,17 +105,17 @@ func (g *GenerateTransport) Generate() (err error) {
return err
}
case "grpc":
gp := newGenerateGRPCTransportProto(g.name, g.serviceInterface, g.methods)
gp := newGenerateGRPCTransportProto(g.name, g.pbPath, g.serviceInterface, g.methods)
err = gp.Generate()
if err != nil {
return err
}
gt := newGenerateGRPCTransport(g.name, g.serviceInterface, g.methods)
gt := newGenerateGRPCTransport(g.name, g.pbImportPath, g.serviceInterface, g.methods)
err = gt.Generate()
if err != nil {
return err
}
gb := newGenerateGRPCTransportBase(g.name, g.serviceInterface, g.methods, mth)
gb := newGenerateGRPCTransportBase(g.name, g.pbImportPath, g.serviceInterface, g.methods, mth)
err = gb.Generate()
if err != nil {
return err
Expand Down Expand Up @@ -212,7 +218,8 @@ func (g *generateHTTPTransport) Generate() (err error) {
if err != nil {
return err
}
endpImports, err := utils.GetEndpointImportPath(g.name)
endpointImport, err := utils.GetEndpointImportPath(g.name)
defer func() { recover() }()
if err != nil {
return err
}
Expand Down Expand Up @@ -320,7 +327,7 @@ func (g *generateHTTPTransport) Generate() (err error) {
nil,
[]jen.Code{
param,
jen.Id("endpoints").Qual(endpImports, "Endpoints"),
jen.Id("endpoints").Qual(endpointImport, "Endpoints"),
jen.Id("options").Index().Qual(
"github.com/go-kit/kit/transport/http",
"ServerOption",
Expand Down Expand Up @@ -352,7 +359,7 @@ func (g *generateHTTPTransport) Generate() (err error) {
jen.Error(),
},
"",
jen.Id("req").Op(":=").Qual(endpImports, m.Name+"Request").Block(),
jen.Id("req").Op(":=").Qual(endpointImport, m.Name+"Request").Block(),
jen.Err().Op(":=").Qual("encoding/json", "NewDecoder").Call(
jen.Id("r").Dot("Body"),
).Dot("Decode").Call(jen.Id("&req")),
Expand All @@ -373,7 +380,7 @@ func (g *generateHTTPTransport) Generate() (err error) {
jen.If(
jen.List(jen.Id("f"), jen.Id("ok")).Op(":=").Id("response.").Call(
jen.Qual(
endpImports,
endpointImport,
"Failure",
),
).Id(";").Id("ok").Id("&&").Id("f").Dot("Failed").Call().Op("!=").Nil(),
Expand Down Expand Up @@ -643,14 +650,17 @@ type generateGRPCTransportProto struct {
serviceInterface parser.Interface
}

func newGenerateGRPCTransportProto(name string, serviceInterface parser.Interface, methods []string) Gen {
func newGenerateGRPCTransportProto(name, pbPath string, serviceInterface parser.Interface, methods []string) Gen {
t := &generateGRPCTransportProto{
name: name,
methods: methods,
interfaceName: utils.ToCamelCase(name + "Service"),
destPath: fmt.Sprintf(viper.GetString("gk_grpc_pb_path_format"), utils.ToLowerSnakeCase(name)),
serviceInterface: serviceInterface,
}
if pbPath != "" {
t.destPath = path.Join(pbPath, "pb")
}
t.pbFilePath = path.Join(
t.destPath,
fmt.Sprintf(viper.GetString("gk_grpc_pb_file_name"), utils.ToLowerSnakeCase(name)),
Expand Down Expand Up @@ -858,14 +868,15 @@ type generateGRPCTransportBase struct {
methods []string
allMethods []parser.Method
interfaceName string
pbImportPath string
destPath string
filePath string
file *parser.File
grpcFilePath string
serviceInterface parser.Interface
}

func newGenerateGRPCTransportBase(name string, serviceInterface parser.Interface, methods []string, allMethods []parser.Method) Gen {
func newGenerateGRPCTransportBase(name, pbImportPath string, serviceInterface parser.Interface, methods []string, allMethods []parser.Method) Gen {
t := &generateGRPCTransportBase{
name: name,
methods: methods,
Expand All @@ -877,6 +888,7 @@ func newGenerateGRPCTransportBase(name string, serviceInterface parser.Interface
t.filePath = path.Join(t.destPath, viper.GetString("gk_grpc_base_file_name"))
t.grpcFilePath = path.Join(t.destPath, viper.GetString("gk_grpc_file_name"))
t.srcFile = jen.NewFilePath(t.destPath)
t.pbImportPath = pbImportPath
t.InitPg()
t.fs = fs.Get()
return t
Expand All @@ -891,7 +903,7 @@ func (g *generateGRPCTransportBase) Generate() (err error) {
if err != nil {
return err
}
pbImport, err := utils.GetPbImportPath(g.name)
pbImport, err := utils.GetPbImportPath(g.name, g.pbImportPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -963,13 +975,14 @@ type generateGRPCTransport struct {
methods []string
interfaceName string
destPath string
pbImportPath string
generateFirstTime bool
file *parser.File
filePath string
serviceInterface parser.Interface
}

func newGenerateGRPCTransport(name string, serviceInterface parser.Interface, methods []string) Gen {
func newGenerateGRPCTransport(name, pbImportPath string, serviceInterface parser.Interface, methods []string) Gen {
t := &generateGRPCTransport{
name: name,
methods: methods,
Expand All @@ -979,6 +992,7 @@ func newGenerateGRPCTransport(name string, serviceInterface parser.Interface, me
}
t.filePath = path.Join(t.destPath, viper.GetString("gk_grpc_file_name"))
t.srcFile = jen.NewFilePath(t.destPath)
t.pbImportPath = pbImportPath
t.InitPg()
t.fs = fs.Get()
return t
Expand All @@ -992,7 +1006,7 @@ func (g *generateGRPCTransport) Generate() (err error) {
if err != nil {
return err
}
pbImport, err := utils.GetPbImportPath(g.name)
pbImport, err := utils.GetPbImportPath(g.name, g.pbImportPath)
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions generator/add_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestNewGenerateTransport(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewGenerateTransport(tt.args.name, tt.args.gorillaMux, tt.args.transport, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
if got := NewGenerateTransport(tt.args.name, tt.args.gorillaMux, tt.args.transport, "", "", tt.args.methods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewGenerateTransport() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -837,6 +837,7 @@ func Test_generateHTTPTransportBase_Generate(t *testing.T) {
func Test_newGenerateGRPCTransportProto(t *testing.T) {
type args struct {
name string
pbPath string
serviceInterface parser.Interface
methods []string
}
Expand All @@ -849,7 +850,7 @@ func Test_newGenerateGRPCTransportProto(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newGenerateGRPCTransportProto(tt.args.name, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
if got := newGenerateGRPCTransportProto(tt.args.name, tt.args.pbPath, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGenerateGRPCTransportProto() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -1021,6 +1022,7 @@ func Test_generateGRPCTransportProto_getServiceRPC(t *testing.T) {
func Test_newGenerateGRPCTransportBase(t *testing.T) {
type args struct {
name string
pbImportPath string
serviceInterface parser.Interface
methods []string
allMethods []parser.Method
Expand All @@ -1034,7 +1036,7 @@ func Test_newGenerateGRPCTransportBase(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newGenerateGRPCTransportBase(tt.args.name, tt.args.serviceInterface, tt.args.methods, tt.args.allMethods); !reflect.DeepEqual(got, tt.want) {
if got := newGenerateGRPCTransportBase(tt.args.name, tt.args.pbImportPath, tt.args.serviceInterface, tt.args.methods, tt.args.allMethods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGenerateGRPCTransportBase() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -1085,6 +1087,7 @@ func Test_generateGRPCTransportBase_Generate(t *testing.T) {
func Test_newGenerateGRPCTransport(t *testing.T) {
type args struct {
name string
pbImportPath string
serviceInterface parser.Interface
methods []string
}
Expand All @@ -1097,7 +1100,7 @@ func Test_newGenerateGRPCTransport(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newGenerateGRPCTransport(tt.args.name, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
if got := newGenerateGRPCTransport(tt.args.name, tt.args.pbImportPath, tt.args.serviceInterface, tt.args.methods); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGenerateGRPCTransport() = %v, want %v", got, tt.want)
}
})
Expand Down
12 changes: 8 additions & 4 deletions generator/generate_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type GenerateClient struct {
transport string
interfaceName string
destPath string
pbImportPath string
filePath string
serviceDestPath string
serviceFilePath string
Expand All @@ -30,7 +31,7 @@ type GenerateClient struct {
}

// NewGenerateClient returns a client generator.
func NewGenerateClient(name string, transport string) Gen {
func NewGenerateClient(name string, transport, pbImportPath string) Gen {
i := &GenerateClient{
name: name,
interfaceName: utils.ToCamelCase(name + "Service"),
Expand All @@ -41,6 +42,7 @@ func NewGenerateClient(name string, transport string) Gen {
i.serviceFilePath = path.Join(i.serviceDestPath, viper.GetString("gk_service_file_name"))
i.filePath = path.Join(i.destPath, viper.GetString("gk_service_file_name"))
i.srcFile = jen.NewFilePath(i.destPath)
i.pbImportPath = pbImportPath
i.InitPg()
i.fs = fs.Get()
return i
Expand Down Expand Up @@ -83,7 +85,7 @@ func (g *GenerateClient) Generate() (err error) {
return err
}
case "grpc":
cg := newGenerateGRPCClient(g.name, g.serviceInterface, g.serviceFile)
cg := newGenerateGRPCClient(g.name, g.pbImportPath, g.serviceInterface, g.serviceFile)
err = cg.Generate()
if err != nil {
return err
Expand Down Expand Up @@ -339,12 +341,13 @@ type generateGRPCClient struct {
name string
interfaceName string
destPath string
pbImportPath string
filePath string
serviceInterface parser.Interface
serviceFile *parser.File
}

func newGenerateGRPCClient(name string, serviceInterface parser.Interface, serviceFile *parser.File) Gen {
func newGenerateGRPCClient(name, pbImportPath string, serviceInterface parser.Interface, serviceFile *parser.File) Gen {
i := &generateGRPCClient{
name: name,
interfaceName: utils.ToCamelCase(name + "Service"),
Expand All @@ -354,6 +357,7 @@ func newGenerateGRPCClient(name string, serviceInterface parser.Interface, servi
}
i.filePath = path.Join(i.destPath, viper.GetString("gk_grpc_client_file_name"))
i.srcFile = jen.NewFilePath(i.destPath)
i.pbImportPath = pbImportPath
i.InitPg()
i.fs = fs.Get()
return i
Expand All @@ -368,7 +372,7 @@ func (g *generateGRPCClient) Generate() (err error) {
if err != nil {
return err
}
pbImport, err := utils.GetPbImportPath(g.name)
pbImport, err := utils.GetPbImportPath(g.name, g.pbImportPath)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5f7661d

Please sign in to comment.