-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
linyyyang
committed
Sep 6, 2021
1 parent
911fde7
commit e48ff4a
Showing
11 changed files
with
432 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.