cmd utils

This commit is contained in:
wangchuxiao 2023-04-13 10:05:28 +08:00
parent dec9761a17
commit 993d048989
2 changed files with 158 additions and 120 deletions

View File

@ -5,16 +5,41 @@ import (
) )
func main() { func main() {
msgUtilsCmd := cmd.NewMsgUtilsCmd() msgUtilsCmd := cmd.NewMsgUtilsCmd("openIMCmdUtils", "openIM cmd utils", nil)
msgUtilsCmd.AddSuperGroupIDFlag() getCmd := cmd.NewGetCmd()
msgUtilsCmd.AddUserIDFlag() fixCmd := cmd.NewFixCmd()
clearCmd := cmd.NewClearCmd()
seqCmd := cmd.NewSeqCmd() seqCmd := cmd.NewSeqCmd()
msgCmd := cmd.NewMsgCmd() msgCmd := cmd.NewMsgCmd()
cmd.GetCmd.AddCommand(seqCmd.Command, msgCmd.Command) getCmd.AddCommand(seqCmd.GetSeqCmd(), msgCmd.GetMsgCmd())
cmd.FixCmd.AddCommand(seqCmd.Command) getCmd.AddSuperGroupIDFlag()
cmd.GetCmd.AddCommand(msgCmd.Command) getCmd.AddUserIDFlag()
msgUtilsCmd.AddCommand(cmd.GetCmd, cmd.FixCmd, cmd.ClearCmd) getCmd.AddBeginSeqFlag()
getCmd.AddLimitFlag()
// openIM get seq --userID=xxx
// openIM get seq --superGroupID=xxx
// openIM get msg --userID=xxx --beginSeq=100 --limit=10
// openIM get msg --superGroupID=xxx --beginSeq=100 --limit=10
fixCmd.AddCommand(seqCmd.FixSeqCmd())
fixCmd.AddSuperGroupIDFlag()
fixCmd.AddUserIDFlag()
fixCmd.AddFixAllFlag()
// openIM fix seq --userID=xxx
// openIM fix seq --superGroupID=xxx
// openIM fix seq --fixAll
clearCmd.AddCommand(msgCmd.ClearMsgCmd())
clearCmd.AddSuperGroupIDFlag()
clearCmd.AddUserIDFlag()
clearCmd.AddClearAllFlag()
clearCmd.AddBeginSeqFlag()
clearCmd.AddLimitFlag()
// openIM clear msg --userID=xxx --beginSeq=100 --limit=10
// openIM clear msg --superGroupID=xxx --beginSeq=100 --limit=10
// openIM clear msg --clearAll
msgUtilsCmd.AddCommand(&getCmd.Command, &fixCmd.Command, &clearCmd.Command)
if err := msgUtilsCmd.Execute(); err != nil { if err := msgUtilsCmd.Execute(); err != nil {
panic(err.Error()) panic(err)
} }
} }

View File

@ -1,157 +1,170 @@
package cmd package cmd
import ( import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/internal/tools" "github.com/OpenIMSDK/Open-IM-Server/internal/tools"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type MsgUtilsCmd struct { type MsgUtilsCmd struct {
*RootCmd cobra.Command
userID string msgTool *tools.MsgTool
superGroupID string
clearAll bool
fixAll bool
}
func NewMsgUtilsCmd() MsgUtilsCmd {
return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")}
} }
func (m *MsgUtilsCmd) AddUserIDFlag() { func (m *MsgUtilsCmd) AddUserIDFlag() {
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID") m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
} }
func (m *MsgUtilsCmd) GetUserIDFlag() string { func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
return m.userID userID, _ := cmdLines.Flags().GetString("userID")
return userID
} }
func (m *MsgUtilsCmd) AddFixAllFlag() { func (m *MsgUtilsCmd) AddFixAllFlag() {
m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs") m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs")
} }
func (m *MsgUtilsCmd) GetFixAllFlag() bool { func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool {
return m.fixAll fixAll, _ := cmdLines.Flags().GetBool("fixAll")
return fixAll
}
func (m *MsgUtilsCmd) AddClearAllFlag() {
m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs")
}
func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool {
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
return clearAll
} }
func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { func (m *MsgUtilsCmd) AddSuperGroupIDFlag() {
m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID") m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID")
} }
func (m *MsgUtilsCmd) GetSuperGroupIDFlag() string { func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
return m.superGroupID superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
return superGroupID
} }
func (m *MsgUtilsCmd) AddClearAllFlag() bool { func (m *MsgUtilsCmd) AddBeginSeqFlag() {
return m.clearAll m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
} }
func (m *MsgUtilsCmd) GetClearAllFlag() bool { func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 {
return m.clearAll beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq")
return beginSeq
}
func (m *MsgUtilsCmd) AddLimitFlag() {
m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit")
}
func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 {
limit, _ := cmdLines.Flags().GetInt64("limit")
return limit
}
func (m *MsgUtilsCmd) Execute() error {
return m.Command.Execute()
}
func NewMsgUtilsCmd(use, short string, args cobra.PositionalArgs) *MsgUtilsCmd {
return &MsgUtilsCmd{
Command: cobra.Command{
Use: use,
Short: short,
Args: args,
},
}
}
type GetCmd struct {
*MsgUtilsCmd
}
func NewGetCmd() *GetCmd {
return &GetCmd{
NewMsgUtilsCmd("get [resource]", "get action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
}
}
type FixCmd struct {
*MsgUtilsCmd
}
func NewFixCmd() *FixCmd {
return &FixCmd{
NewMsgUtilsCmd("fix [resource]", "fix action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
}
}
type ClearCmd struct {
*MsgUtilsCmd
}
func NewClearCmd() *ClearCmd {
return &ClearCmd{
NewMsgUtilsCmd("clear [resource]", "clear action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
}
} }
type SeqCmd struct { type SeqCmd struct {
Command *cobra.Command *MsgUtilsCmd
} }
func (SeqCmd) RunCommand(cmdLines *cobra.Command, args []string) error { func NewSeqCmd() *SeqCmd {
msgTool, err := tools.InitMsgTool() seqCmd := &SeqCmd{
if err != nil { NewMsgUtilsCmd("seq", "seq", 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
}
func NewSeqCmd() SeqCmd {
seqCmd := SeqCmd{&cobra.Command{
Use: "seq",
Short: "seq operation",
}}
seqCmd.Command.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs")
seqCmd.Command.RunE = seqCmd.RunCommand
return seqCmd return seqCmd
} }
type MsgCmd struct { func (s *SeqCmd) GetSeqCmd() *cobra.Command {
Command *cobra.Command s.Command.Run = func(cmdLines *cobra.Command, args []string) {
_, err := tools.InitMsgTool()
if err != nil {
panic(err)
}
userID := s.getUserIDFlag(cmdLines)
superGroupID := s.getSuperGroupIDFlag(cmdLines)
// beginSeq := s.getBeginSeqFlag(cmdLines)
// limit := s.getLimitFlag(cmdLines)
if userID != "" {
// seq, err := msgTool.s(context.Background(), userID)
if err != nil {
panic(err)
}
// println(seq)
} else if superGroupID != "" {
// seq, err := msgTool.GetSuperGroupSeq(context.Background(), superGroupID)
if err != nil {
panic(err)
}
// println(seq)
}
}
return &s.Command
} }
func NewMsgCmd() MsgCmd { func (s *SeqCmd) FixSeqCmd() *cobra.Command {
msgCmd := MsgCmd{&cobra.Command{ return &s.Command
Use: "msg", }
Short: "msg operation",
}} type MsgCmd struct {
msgCmd.Command.RunE = msgCmd.RunCommand *MsgUtilsCmd
msgCmd.Command.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs") }
func NewMsgCmd() *MsgCmd {
msgCmd := &MsgCmd{
NewMsgUtilsCmd("msg", "msg", nil),
}
return msgCmd return msgCmd
} }
func (*MsgCmd) RunCommand(cmdLines *cobra.Command, args []string) error { func (m *MsgCmd) GetMsgCmd() *cobra.Command {
msgTool, err := tools.InitMsgTool() return &m.Command
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{ func (m *MsgCmd) ClearMsgCmd() *cobra.Command {
Use: "get", return &m.Command
Short: "get operation",
}
var FixCmd = &cobra.Command{
Use: "fix",
Short: "fix seq operation",
}
var ClearCmd = &cobra.Command{
Use: "clear",
Short: "clear operation",
} }