test cobra

This commit is contained in:
wangchuxiao
2023-03-08 11:45:54 +08:00
parent 60876e865b
commit e55167d0bf
10 changed files with 223 additions and 144 deletions
+10 -96
View File
@@ -1,108 +1,22 @@
package main
import (
"OpenIM/internal/tools"
"OpenIM/pkg/common/cmd"
"context"
"fmt"
"github.com/spf13/cobra"
"os"
)
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 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
},
}
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() {
rootCmd := cmd.NewMsgUtilsCmd()
rootCmd.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
rootCmd.Command.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")
getCmd.AddCommand(seqCmd, msgCmd)
fixCmd.AddCommand(seqCmd)
clearCmd.AddCommand(msgCmd)
rootCmd.AddCommand(getCmd, fixCmd, clearCmd)
if err := rootCmd.Execute(); err != nil {
msgUtilsCmd := cmd.NewMsgUtilsCmd()
msgUtilsCmd.AddSuperGroupIDFlag()
msgUtilsCmd.AddUserIDFlag()
seqCmd := cmd.NewSeqCmd()
msgCmd := cmd.NewMsgCmd()
cmd.GetCmd.AddCommand(seqCmd.Command, msgCmd.Command)
cmd.FixCmd.AddCommand(seqCmd.Command)
cmd.GetCmd.AddCommand(msgCmd.Command)
msgUtilsCmd.AddCommand(cmd.GetCmd, cmd.FixCmd, cmd.ClearCmd)
if err := msgUtilsCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}