mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-25 19:22:46 +08:00
test cobra
This commit is contained in:
parent
d6704aed80
commit
ea614167a6
@ -19,22 +19,24 @@ var startCmd = &cobra.Command{
|
|||||||
Short: "Start the server",
|
Short: "Start the server",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
port, _ := cmd.Flags().GetInt(constant.FlagPort)
|
port, _ := cmd.Flags().GetInt(constant.FlagPort)
|
||||||
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
|
if err := run(port); err != nil {
|
||||||
if err := run(configFolderPath, port); err != nil {
|
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func main() {
|
||||||
startCmd.Flags().IntP(constant.FlagPort, "p", 0, "Port to listen on")
|
startCmd.Flags().IntP(constant.FlagPort, "p", 0, "Port to listen on")
|
||||||
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
||||||
|
rootCmd := cmd.NewRootCmd()
|
||||||
|
cobra.Command(rootCmd).AddCommand(startCmd)
|
||||||
|
if err := startCmd.Execute(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(configFolderPath string, port int) error {
|
func run(port int) error {
|
||||||
if err := config.InitConfig(configFolderPath); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = config.Config.Api.GinPort[0]
|
port = config.Config.Api.GinPort[0]
|
||||||
}
|
}
|
||||||
@ -52,12 +54,3 @@ func run(configFolderPath string, port int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
rootCmd := cmd.NewRootCmd()
|
|
||||||
rootCmd.AddCommand(startCmd)
|
|
||||||
if err := startCmd.Execute(); err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -5,93 +5,106 @@ import (
|
|||||||
"OpenIM/pkg/common/cmd"
|
"OpenIM/pkg/common/cmd"
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
"flag"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var seqCmd = &cobra.Command{
|
||||||
var showSeqCmd = &cobra.Command{
|
Use: "seq",
|
||||||
Use: "show-seq",
|
Short: "seq operation",
|
||||||
Short: "Start the server",
|
RunE: func(cmdLines *cobra.Command, args []string) error {
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
msgTool, err := tools.InitMsgTool()
|
||||||
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
|
if err != nil {
|
||||||
config.InitConfig(configFolderPath)
|
return err
|
||||||
|
}
|
||||||
|
userID, _ := cmdLines.Flags().GetString("userID")
|
||||||
|
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||||
|
fixAll, _ := cmdLines.Flags().GetBool("fixAll")
|
||||||
|
ctx := context.Background()
|
||||||
|
switch {
|
||||||
|
case cmdLines.Parent() == getCmd:
|
||||||
|
switch {
|
||||||
|
case userID != "":
|
||||||
|
msgTool.ShowUserSeqs(ctx, userID)
|
||||||
|
case superGroupID != "":
|
||||||
|
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
|
||||||
|
}
|
||||||
|
case cmdLines.Parent() == fixCmd:
|
||||||
|
switch {
|
||||||
|
case userID != "":
|
||||||
|
_, _, err = msgTool.GetAndFixUserSeqs(ctx, userID)
|
||||||
|
case superGroupID != "":
|
||||||
|
err = msgTool.FixGroupSeq(ctx, userID)
|
||||||
|
case fixAll:
|
||||||
|
err = msgTool.FixAllSeq(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var
|
var msgCmd = &cobra.Command{
|
||||||
|
Use: "msg",
|
||||||
|
Short: "msg operation",
|
||||||
func init() {
|
RunE: func(cmdLines *cobra.Command, args []string) error {
|
||||||
showSeqCmd.Flags().StringP("userID", "u", "", "openIM userID")
|
msgTool, err := tools.InitMsgTool()
|
||||||
showSeqCmd.Flags().StringP("groupID", "g", "", "openIM groupID")
|
if err != nil {
|
||||||
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func run(configFolderPath string, cmd *cobra.Command) error {
|
|
||||||
if err := config.InitConfig(configFolderPath); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
userID, _ := cmdLines.Flags().GetString("userID")
|
||||||
|
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||||
|
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
|
||||||
|
ctx := context.Background()
|
||||||
|
switch {
|
||||||
|
case cmdLines.Parent() == getCmd:
|
||||||
|
switch {
|
||||||
|
case userID != "":
|
||||||
|
msgTool.ShowUserSeqs(ctx, userID)
|
||||||
|
case superGroupID != "":
|
||||||
|
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
|
||||||
|
}
|
||||||
|
case cmdLines.Parent() == clearCmd:
|
||||||
|
switch {
|
||||||
|
case userID != "":
|
||||||
|
msgTool.ClearUsersMsg(ctx, []string{userID})
|
||||||
|
case superGroupID != "":
|
||||||
|
msgTool.ClearSuperGroupMsg(ctx, []string{superGroupID})
|
||||||
|
case clearAll:
|
||||||
|
msgTool.AllUserClearMsgAndFixSeq()
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var getCmd = &cobra.Command{
|
||||||
|
Use: "get",
|
||||||
|
Short: "get operation",
|
||||||
|
}
|
||||||
|
|
||||||
|
var fixCmd = &cobra.Command{
|
||||||
|
Use: "fix",
|
||||||
|
Short: "fix seq operation",
|
||||||
|
}
|
||||||
|
|
||||||
|
var clearCmd = &cobra.Command{
|
||||||
|
Use: "clear",
|
||||||
|
Short: "clear operation",
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rootCmd := cmd.NewRootCmd()
|
cmd.RootCmd.PersistentFlags().StringP("userID", "u", "", "openIM userID")
|
||||||
rootCmd.AddCommand(showSeqCmd)
|
cmd.RootCmd.PersistentFlags().StringP("groupID", "u", "", "openIM superGroupID")
|
||||||
if err := rootCmd.Execute(); err != nil {
|
seqCmd.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs")
|
||||||
|
msgCmd.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
|
||||||
|
cmd.RootCmd.AddCommand(getCmd, fixCmd, clearCmd)
|
||||||
|
getCmd.AddCommand(seqCmd, msgCmd)
|
||||||
|
fixCmd.AddCommand(seqCmd)
|
||||||
|
clearCmd.AddCommand(msgCmd)
|
||||||
|
if err := cmd.RootCmd.Execute(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
func main() {
|
|
||||||
if err := config.InitConfig(); err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
// clear msg by id
|
|
||||||
var userIDClearMsg = flag.String("user_id_fix_seq", "", "userID to clear msg and reset seq")
|
|
||||||
var superGroupIDClearMsg = flag.String("super_group_id_fix_seq", "", "superGroupID to clear msg and reset seq")
|
|
||||||
// show seq by id
|
|
||||||
var userIDShowSeq = flag.String("user_id_show_seq", "", "show userID")
|
|
||||||
var superGroupIDShowSeq = flag.String("super_group_id_show_seq", "", "userID to clear msg and reset seq")
|
|
||||||
// fix seq by id
|
|
||||||
var userIDFixSeq = flag.String("user_id_fix_seq", "", "userID to Fix Seq")
|
|
||||||
var superGroupIDFixSeq = flag.String("super_group_id_fix_seq", "", "super groupID to fix Seq")
|
|
||||||
var fixAllSeq = flag.Bool("fix_all_seq", false, "fix seq")
|
|
||||||
flag.Parse()
|
|
||||||
msgTool, err := tools.InitMsgTool()
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
|
||||||
if userIDFixSeq != nil {
|
|
||||||
msgTool.GetAndFixUserSeqs(ctx, *userIDFixSeq)
|
|
||||||
}
|
|
||||||
if superGroupIDFixSeq != nil {
|
|
||||||
msgTool.FixGroupSeq(ctx, *superGroupIDFixSeq)
|
|
||||||
}
|
|
||||||
if fixAllSeq != nil {
|
|
||||||
msgTool.FixAllSeq(ctx)
|
|
||||||
}
|
|
||||||
if userIDClearMsg != nil {
|
|
||||||
msgTool.ClearUsersMsg(ctx, []string{*userIDClearMsg})
|
|
||||||
}
|
|
||||||
|
|
||||||
if superGroupIDClearMsg != nil {
|
|
||||||
msgTool.ClearSuperGroupMsg(ctx, []string{*superGroupIDClearMsg})
|
|
||||||
}
|
|
||||||
if userIDShowSeq != nil {
|
|
||||||
msgTool.ShowUserSeqs(ctx, *userIDShowSeq)
|
|
||||||
}
|
|
||||||
|
|
||||||
if superGroupIDShowSeq != nil {
|
|
||||||
msgTool.ShowSuperGroupSeqs(ctx, *superGroupIDShowSeq)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -180,10 +180,10 @@ func (c *MsgTool) ShowSuperGroupUserSeqs(ctx context.Context, groupID, userID st
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MsgTool) FixAllSeq(ctx context.Context) {
|
func (c *MsgTool) FixAllSeq(ctx context.Context) error {
|
||||||
userIDs, err := c.userDatabase.GetAllUserID(ctx)
|
userIDs, err := c.userDatabase.GetAllUserID(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
userCurrentMinSeq, err := c.msgDatabase.GetUserMinSeq(ctx, userID)
|
userCurrentMinSeq, err := c.msgDatabase.GetUserMinSeq(ctx, userID)
|
||||||
@ -204,7 +204,7 @@ func (c *MsgTool) FixAllSeq(ctx context.Context) {
|
|||||||
fmt.Println("fix users seq success")
|
fmt.Println("fix users seq success")
|
||||||
groupIDs, err := c.groupDatabase.GetGroupIDsByGroupType(ctx, constant.WorkingGroup)
|
groupIDs, err := c.groupDatabase.GetGroupIDsByGroupType(ctx, constant.WorkingGroup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
maxSeq, err := c.msgDatabase.GetGroupMaxSeq(ctx, groupID)
|
maxSeq, err := c.msgDatabase.GetGroupMaxSeq(ctx, groupID)
|
||||||
@ -232,4 +232,5 @@ func (c *MsgTool) FixAllSeq(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("fix all seq finished")
|
fmt.Println("fix all seq finished")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,31 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"OpenIM/pkg/common/config"
|
||||||
|
"OpenIM/pkg/common/constant"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRootCmd() *cobra.Command {
|
type RootCmd cobra.Command
|
||||||
return &cobra.Command{
|
|
||||||
|
func NewRootCmd() RootCmd {
|
||||||
|
c := cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start the server",
|
Short: "Start the server",
|
||||||
Long: `Start the server`,
|
Long: `Start the server`,
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return getConfFromCmdAndInit(cmd)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
c.Flags()
|
||||||
|
return RootCmd(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r RootCmd) Init() {
|
||||||
|
cobra.Command(r).Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfFromCmdAndInit(cmdLines *cobra.Command) error {
|
||||||
|
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
|
||||||
|
return config.InitConfig(configFolderPath)
|
||||||
}
|
}
|
||||||
|
@ -527,6 +527,9 @@ func (c *config) GetConfFromRegistry(registry discoveryregistry.SvcDiscoveryRegi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(configFolderPath string) error {
|
func InitConfig(configFolderPath string) error {
|
||||||
|
defer func() {
|
||||||
|
fmt.Println("use config folder", configFolderPath)
|
||||||
|
}()
|
||||||
err := Config.initConfig(&Config, FileName, configFolderPath)
|
err := Config.initConfig(&Config, FileName, configFolderPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -13,3 +13,8 @@ type SvcDiscoveryRegistry interface {
|
|||||||
RegisterConf2Registry(key string, conf []byte) error
|
RegisterConf2Registry(key string, conf []byte) error
|
||||||
GetConfFromRegistry(key string) ([]byte, error)
|
GetConfFromRegistry(key string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConns(serviceName string) {
|
||||||
|
GetConns(serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error)
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user