feat: support add api and router group command

This commit is contained in:
Young Xu 2023-03-21 01:32:09 +08:00
parent d5854a4c62
commit a7316847a9
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
4 changed files with 92 additions and 0 deletions

View File

@ -25,6 +25,8 @@ func init() {
})
rootCmd.AddCommand(bootstrap.Update())
rootCmd.AddCommand(bootstrap.CreateProject())
rootCmd.AddCommand(bootstrap.AddAPICommand())
rootCmd.AddCommand(bootstrap.AddServiceCommand())
}
func main() {

76
create_api.go Normal file
View File

@ -0,0 +1,76 @@
package bootstrap
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gitter.top/coco/gobuf"
"os"
)
func AddAPICommand() *cobra.Command {
pbPath, _ := os.Getwd()
var routerGroup = ""
var routerName = ""
cmd := &cobra.Command{
Use: "addapi",
Short: "add api for service",
Run: func(cmd *cobra.Command, args []string) {
if routerName == "" {
logrus.Errorf("router name --name empty")
return
}
routerName = FirstUpper(routerName)
buf, err := gobuf.NewParser(pbPath)
if err != nil {
logrus.Errorf("read proto file failed: %v", err)
return
}
if buf.ExistRPC(routerGroup, routerName) {
logrus.Warnf("router name exist")
return
}
err = buf.AddRPC(routerGroup, routerName)
if err != nil {
logrus.Warnf("add router failed: %v", err)
return
}
},
}
cmd.Flags().StringVar(&pbPath, "path", pbPath, "proto file path")
cmd.Flags().StringVar(&routerName, "name", routerName, "api name")
cmd.Flags().StringVar(&routerGroup, "service", routerGroup, "service name")
return cmd
}
func AddServiceCommand() *cobra.Command {
var pbPath = ""
var svcName = ""
cmd := &cobra.Command{
Use: "addgroup",
Short: "add router group",
Run: func(cmd *cobra.Command, args []string) {
if svcName == "" {
logrus.Errorf("router group name -- name empty")
return
}
buf, err := gobuf.NewParser(pbPath)
if err != nil {
logrus.Errorf("read proto file failed: %v", err)
return
}
if buf.ExistService(svcName) {
logrus.Errorf("router group name exist")
return
}
if err := buf.AddService(svcName); err != nil {
logrus.Errorf("add router group failed: %+v", err)
return
}
},
}
cmd.Flags().StringVar(&pbPath, "path", pbPath, "proto file path")
cmd.Flags().StringVar(&svcName, "name", svcName, "router group name, such as UserModule")
return cmd
}

3
go.mod
View File

@ -10,9 +10,12 @@ require (
require (
code.gitea.io/sdk/gitea v0.15.1 // indirect
github.com/emicklei/proto v1.11.1 // indirect
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gitter.top/coco/gobuf v0.0.0-20230320172921-67ae76c9b96b // indirect
gitter.top/sync/proto-contrib v0.15.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

11
util.go Normal file
View File

@ -0,0 +1,11 @@
package bootstrap
import "strings"
// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
if s == "" {
return ""
}
return strings.ToUpper(s[:1]) + s[1:]
}