test cobra

This commit is contained in:
wangchuxiao
2023-03-07 17:59:34 +08:00
parent d6704aed80
commit ea614167a6
6 changed files with 125 additions and 92 deletions
+84 -71
View File
@@ -5,93 +5,106 @@ import (
"OpenIM/pkg/common/cmd"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"flag"
"context"
"fmt"
"github.com/spf13/cobra"
"os"
)
var showSeqCmd = &cobra.Command{
Use: "show-seq",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
config.InitConfig(configFolderPath)
var seqCmd = &cobra.Command{
Use: "seq",
Short: "seq operation",
RunE: func(cmdLines *cobra.Command, args []string) error {
msgTool, err := tools.InitMsgTool()
if err != nil {
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
func init() {
showSeqCmd.Flags().StringP("userID", "u", "", "openIM userID")
showSeqCmd.Flags().StringP("groupID", "g", "", "openIM groupID")
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
var msgCmd = &cobra.Command{
Use: "msg",
Short: "msg operation",
RunE: func(cmdLines *cobra.Command, args []string) error {
msgTool, err := tools.InitMsgTool()
if err != nil {
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
},
}
func run(configFolderPath string, cmd *cobra.Command) error {
if err := config.InitConfig(configFolderPath); err != nil {
return err
}
var getCmd = &cobra.Command{
Use: "get",
Short: "get operation",
}
var fixCmd = &cobra.Command{
Use: "fix",
Short: "fix seq operation",
}
return nil
var clearCmd = &cobra.Command{
Use: "clear",
Short: "clear operation",
}
func main() {
rootCmd := cmd.NewRootCmd()
rootCmd.AddCommand(showSeqCmd)
if err := rootCmd.Execute(); err != nil {
cmd.RootCmd.PersistentFlags().StringP("userID", "u", "", "openIM userID")
cmd.RootCmd.PersistentFlags().StringP("groupID", "u", "", "openIM superGroupID")
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)
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)
}
}