test-errcode
wangchuxiao 2 years ago
parent dec9761a17
commit 993d048989

@ -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)
} }
} }

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

Loading…
Cancel
Save