78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"gitter.top/common/gobuf"
|
|
)
|
|
|
|
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
|
|
}
|