Skip to content

Commit

Permalink
✨ feat:add grpc command
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Sep 6, 2021
1 parent 911fde7 commit e48ff4a
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 27 deletions.
44 changes: 44 additions & 0 deletions ginny/command/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package command

import (
"errors"

"github.com/gorillazer/ginny-cli/ginny/handle"
"github.com/gorillazer/ginny-cli/ginny/util"
"github.com/spf13/cobra"
)

func init() {
grpcCmd.Flags().StringP("type", "t", "server", "GRPC service type created, server or client")
rootCmd.AddCommand(grpcCmd)
}

var grpcCmd = &cobra.Command{
Use: "grpc",
Short: "Create grpc server/client file",
Long: "Create grpc server/client file from template",
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if err := CheckArgs(args); err != nil {
return err
}
// 获取参数
serverName := args[0]
flags := cmd.Flags()
types, err := flags.GetString("type")
if err != nil {
return err
}
if types != "server" && types != "client" {
return errors.New("The wrong type was entered")
}
if err := handle.CreateGrpc(serverName, types); err != nil {
return err
}

util.Info("Create new service file success!")
return nil
},
}
19 changes: 18 additions & 1 deletion ginny/command/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

func init() {
newCmd.Flags().StringP("module", "m", "", "Define the project module, ex: git.tencent.com/demo")
newCmd.Flags().Bool("grpc", false, "Create a grpc service project")
newCmd.Flags().Bool("http", true, "Create a http service project")
rootCmd.AddCommand(newCmd)
}

Expand All @@ -29,7 +31,22 @@ var newCmd = &cobra.Command{
if err != nil {
return err
}
if err := handle.CreateProject(projectName, module); err != nil {
enableGrpc, err := flags.GetBool("grpc")
if err != nil {
return err
}
enableHttp, err := flags.GetBool("http")
if err != nil {
return err
}
arg := []string{}
if enableHttp || (!enableGrpc && !enableHttp) {
arg = append(arg, "http")
}
if enableGrpc {
arg = append(arg, "grpc")
}
if err := handle.CreateProject(projectName, module, arg...); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion ginny/command/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var protoCmd = &cobra.Command{
}

util.Info("Create new proto file success!")
util.Info("You can modify the proto file, and then execute `make proto` to generate pb code.")
return nil
},
}
4 changes: 3 additions & 1 deletion ginny/handle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func GetProjectInfo() (*options.ProjectInfo, error) {
if conf.ProjectName == "" {
return nil, errors.New("The project flags file is corrupted .")
}
conf.ProjectPath = dir
if conf.ProjectPath == "" {
conf.ProjectPath = dir
}

return conf, nil
}
Expand Down
144 changes: 144 additions & 0 deletions ginny/handle/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package handle

import (
"errors"
"fmt"

"github.com/fatih/structs"
"github.com/gorillazer/ginny-cli/ginny/options"
"github.com/gorillazer/ginny-cli/ginny/util"
"github.com/iancoleman/strcase"
)

func CreateGrpc(serverName, types string) error {
conf, err := GetProjectInfo()
if err != nil {
util.Error("Failed to get project info, ", err.Error())
return err
}

tmpPath := fmt.Sprintf("%s/%s", conf.ProjectPath, options.TempPath)
if err := PullTemplate(tmpPath, options.ComponentTemplateRepo); err != nil {
return err
}

if types == "server" {
if err := createServer(serverName, tmpPath, conf); err != nil {
return err
}
} else {
if err := createClient(serverName, tmpPath, conf); err != nil {
return err
}
}

if err := ExecCommand(conf.ProjectPath, "go", "mod", "tidy"); err != nil {
return err
}

return nil
}

func createServer(serverName, tmpPath string, conf *options.ProjectInfo) error {
srcFile := fmt.Sprintf("%s/rpc/server/tpl.go", tmpPath)
dstFile := fmt.Sprintf("%s/internal/rpc/server/%s.go", conf.ProjectPath, serverName)
if util.Exists(dstFile) {
return errors.New("File already exists and overwriting is not allowed")
}
if err := util.CopyFile(srcFile, dstFile); err != nil {
return err
}
// replace provider
providerFile := fmt.Sprintf("%s/internal/rpc/server/provider.go", conf.ProjectPath)
if !util.Exists(providerFile) {
if err := util.CopyFile(fmt.Sprintf("%s/rpc/server/provider.go", tmpPath), providerFile); err != nil {
return err
}
}
// rpcProviderFile
rpcProviderFile := fmt.Sprintf("%s/internal/rpc/provider.go", conf.ProjectPath)
if !util.Exists(rpcProviderFile) {
if err := util.CopyFile(fmt.Sprintf("%s/rpc/provider.go", tmpPath), rpcProviderFile); err != nil {
return err
}
}
// proto file
protoFile := fmt.Sprintf("%s/api/proto/%s.proto", conf.ProjectPath, serverName)
if !util.Exists(protoFile) {
if err := CreateProto(serverName, true); err != nil {
return err
}
}

// replace service
caseName := strcase.ToCamel(serverName)
r := &options.ReplaceKeywords{
APP_NAME: conf.ProjectName,
MODULE_NAME: conf.ProjectModule,
SERVER_NAME: caseName,
}
m := structs.Map(r)
m[options.ServerReplaceAnchor[1]] = options.ServerReplaceAnchorValue[1]([]string{conf.ProjectModule})
m[options.ServerReplaceAnchor[2]] = options.ServerReplaceAnchorValue[2]([]string{serverName, caseName})
m[options.ServerReplaceAnchor[3]] = options.ServerReplaceAnchorValue[3]([]string{caseName, serverName})
m[options.ServerReplaceAnchor[4]] = options.ServerReplaceAnchorValue[4]([]string{caseName})

// replace /cmd/provider.go
appProviderFile := conf.ProjectPath + "/cmd/provider.go"
m[options.AppReplaceAnchor[1]] = options.AppReplaceAnchorValue[1]([]string{"grpc_server", conf.ProjectModule, appProviderFile})
m[options.AppReplaceAnchor[2]] = options.AppReplaceAnchorValue[2]([]string{"grpc_server", appProviderFile})
m[options.AppReplaceAnchor[3]] = options.AppReplaceAnchorValue[3]([]string{"grpc_server", appProviderFile})
m[options.AppReplaceAnchor[4]] = options.AppReplaceAnchorValue[4]([]string{"grpc_server", appProviderFile})

if err := ReplaceFileKeyword([]string{dstFile, providerFile, appProviderFile}, m); err != nil {
return err
}

return nil
}

func createClient(clientName, tmpPath string, conf *options.ProjectInfo) error {
srcFile := fmt.Sprintf("%s/rpc/client/tpl.go", tmpPath)
dstFile := fmt.Sprintf("%s/internal/rpc/client/%s.go", conf.ProjectPath, clientName)
if util.Exists(dstFile) {
return errors.New("File already exists and overwriting is not allowed")
}
if err := util.CopyFile(srcFile, dstFile); err != nil {
return err
}
// replace provider
providerFile := fmt.Sprintf("%s/internal/rpc/client/provider.go", conf.ProjectPath)
if !util.Exists(providerFile) {
if err := util.CopyFile(fmt.Sprintf("%s/rpc/client/provider.go", tmpPath), providerFile); err != nil {
return err
}
}
// rpcProviderFile
rpcProviderFile := fmt.Sprintf("%s/internal/rpc/provider.go", conf.ProjectPath)
if !util.Exists(rpcProviderFile) {
if err := util.CopyFile(fmt.Sprintf("%s/rpc/provider.go", tmpPath), rpcProviderFile); err != nil {
return err
}
}
// replace service
caseName := strcase.ToCamel(clientName)
r := &options.ReplaceKeywords{
APP_NAME: conf.ProjectName,
MODULE_NAME: conf.ProjectModule,
SERVER_NAME: caseName,
}
m := structs.Map(r)
m[options.ClientReplaceAnchor[1]] = options.ClientReplaceAnchorValue[1]([]string{caseName})

// replace /cmd/provider.go
appProviderFile := conf.ProjectPath + "/cmd/provider.go"
m[options.AppReplaceAnchor[1]] = options.AppReplaceAnchorValue[1]([]string{"grpc_client", conf.ProjectModule, appProviderFile})
m[options.AppReplaceAnchor[2]] = options.AppReplaceAnchorValue[2]([]string{"grpc_client", appProviderFile})
m[options.AppReplaceAnchor[3]] = options.AppReplaceAnchorValue[3]([]string{"grpc_client", appProviderFile})
m[options.AppReplaceAnchor[4]] = options.AppReplaceAnchorValue[4]([]string{"grpc_client", appProviderFile})

if err := ReplaceFileKeyword([]string{dstFile, providerFile, appProviderFile}, m); err != nil {
return err
}
return nil
}
9 changes: 7 additions & 2 deletions ginny/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateHandle(handleName string) error {
return err
}

srcFile := fmt.Sprintf("%s/handlers/test.go", tmpPath)
srcFile := fmt.Sprintf("%s/handlers/tpl.go", tmpPath)
dstFile := fmt.Sprintf("%s/internal/handlers/%s.go", conf.ProjectPath, handleName)
if util.Exists(dstFile) {
return errors.New("File already exists and overwriting is not allowed")
Expand All @@ -49,7 +49,12 @@ func CreateHandle(handleName string) error {
m[options.HandleReplaceAnchor[1]] = options.HandleReplaceAnchorValue[1]([]string{handleName, caseName})
m[options.HandleReplaceAnchor[2]] = options.HandleReplaceAnchorValue[2]([]string{caseName})

if err := ReplaceFileKeyword([]string{dstFile, providerFile}, m); err != nil {
// replace /cmd/provider.go
appProviderFile := conf.ProjectPath + "/cmd/provider.go"
m[options.AppReplaceAnchor[1]] = options.AppReplaceAnchorValue[1]([]string{"handler", conf.ProjectModule, appProviderFile})
m[options.AppReplaceAnchor[2]] = options.AppReplaceAnchorValue[2]([]string{"handler", appProviderFile})

if err := ReplaceFileKeyword([]string{dstFile, providerFile, appProviderFile}, m); err != nil {
return err
}

Expand Down
10 changes: 10 additions & 0 deletions ginny/handle/new.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handle

import (
"strings"

"github.com/fatih/structs"
"github.com/gorillazer/ginny-cli/ginny/options"
"github.com/gorillazer/ginny-cli/ginny/util"
Expand All @@ -25,6 +27,14 @@ func CreateProject(projectName, moduleName string, args ...string) error {
if moduleName == "" {
moduleName = projectName
}

argStr := strings.Join(args, ",")
if strings.Contains(argStr, "grpc") {
if err := CreateGrpc(projectName, "server"); err != nil {
return err
}
}

r := &options.ReplaceKeywords{
APP_NAME: projectName,
MODULE_NAME: moduleName,
Expand Down
12 changes: 9 additions & 3 deletions ginny/handle/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func CreateProto(serviceName string, validate bool) error {
if err := PullTemplate(tmpPath, options.ComponentTemplateRepo); err != nil {
return err
}
protoFile := fmt.Sprintf("%s/demo.proto", tmpPath)
protoFile := fmt.Sprintf("%s/proto/demo.proto", tmpPath)
dstFile := fmt.Sprintf("%s/api/proto/%s.proto", conf.ProjectPath, serviceName)
if util.Exists(dstFile) {
return errors.New("File already exists and overwriting is not allowed")
}
if validate {
protoFile = fmt.Sprintf("%s/demo_v.proto", tmpPath)
protoFile = fmt.Sprintf("%s/proto/demo_v.proto", tmpPath)
}
if err := util.CopyFile(protoFile, dstFile); err != nil {
return err
Expand All @@ -45,11 +45,17 @@ func CreateProto(serviceName string, validate bool) error {
}

if validate {
protoFile = fmt.Sprintf("%s/validate.proto", tmpPath)
protoFile = fmt.Sprintf("%s/proto/validate.proto", tmpPath)
dstFile = fmt.Sprintf("%s/api/proto/validate.proto", conf.ProjectPath)
if err := util.CopyFile(protoFile, dstFile); err != nil {
return err
}
}

if err := ExecCommand(conf.ProjectPath, "make", "proto"); err != nil {
util.Error(err)
}

util.Info("You can modify the proto file, and then execute `make proto` to generate pb code.")
return nil
}
15 changes: 10 additions & 5 deletions ginny/handle/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateRepo(repoName string, database []string) error {
return err
}

srcFile := fmt.Sprintf("%s/repositories/test.go", tmpPath)
srcFile := fmt.Sprintf("%s/repositories/tpl.go", tmpPath)
dstFile := fmt.Sprintf("%s/internal/repositories/%s.go", conf.ProjectPath, repoName)
if util.Exists(dstFile) {
return errors.New("File already exists and overwriting is not allowed")
Expand Down Expand Up @@ -82,12 +82,17 @@ func CreateRepo(repoName string, database []string) error {
m[options.RepoReplaceAnchor[5]] = fmt.Sprintf("%v \n%s", m[options.RepoReplaceAnchor[5]], options.RepoReplaceAnchor[5])
m[options.RepoReplaceAnchor[6]] = fmt.Sprintf("%v \n%s", m[options.RepoReplaceAnchor[6]], options.RepoReplaceAnchor[6])

if err := ReplaceFileKeyword([]string{dstFile, providerFile}, m); err != nil {
// replace /cmd/provider.go
appProviderFile := conf.ProjectPath + "/cmd/provider.go"
m[options.AppReplaceAnchor[1]] = options.AppReplaceAnchorValue[1]([]string{"repo", conf.ProjectModule, appProviderFile})
m[options.AppReplaceAnchor[2]] = options.AppReplaceAnchorValue[2]([]string{"repo", appProviderFile})

if err := ReplaceFileKeyword([]string{dstFile, providerFile, appProviderFile}, m); err != nil {
return err
}

// if err := ExecCommand(conf.ProjectPath, "go", "mod", "tidy"); err != nil {
// return err
// }
if err := ExecCommand(conf.ProjectPath, "go", "mod", "tidy"); err != nil {
return err
}
return nil
}
Loading

0 comments on commit e48ff4a

Please sign in to comment.