From 993d048989ffec4435faad35bb1af276498ec684 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Thu, 13 Apr 2023 10:05:28 +0800 Subject: [PATCH] cmd utils --- cmd/cmdutils/main.go | 41 +++++-- pkg/common/cmd/msg_utils.go | 219 +++++++++++++++++++----------------- 2 files changed, 149 insertions(+), 111 deletions(-) diff --git a/cmd/cmdutils/main.go b/cmd/cmdutils/main.go index 2363ea777..8c68fca6d 100644 --- a/cmd/cmdutils/main.go +++ b/cmd/cmdutils/main.go @@ -5,16 +5,41 @@ import ( ) func main() { - msgUtilsCmd := cmd.NewMsgUtilsCmd() - msgUtilsCmd.AddSuperGroupIDFlag() - msgUtilsCmd.AddUserIDFlag() + msgUtilsCmd := cmd.NewMsgUtilsCmd("openIMCmdUtils", "openIM cmd utils", nil) + getCmd := cmd.NewGetCmd() + fixCmd := cmd.NewFixCmd() + clearCmd := cmd.NewClearCmd() 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) + getCmd.AddCommand(seqCmd.GetSeqCmd(), msgCmd.GetMsgCmd()) + getCmd.AddSuperGroupIDFlag() + getCmd.AddUserIDFlag() + 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 { - panic(err.Error()) + panic(err) } } diff --git a/pkg/common/cmd/msg_utils.go b/pkg/common/cmd/msg_utils.go index 5683240a0..f762976e7 100644 --- a/pkg/common/cmd/msg_utils.go +++ b/pkg/common/cmd/msg_utils.go @@ -1,157 +1,170 @@ package cmd import ( - "context" "github.com/OpenIMSDK/Open-IM-Server/internal/tools" "github.com/spf13/cobra" ) type MsgUtilsCmd struct { - *RootCmd - userID string + cobra.Command + 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 { - return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")} +func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool { + fixAll, _ := cmdLines.Flags().GetBool("fixAll") + return fixAll } -func (m *MsgUtilsCmd) AddUserIDFlag() { - m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID") +func (m *MsgUtilsCmd) AddClearAllFlag() { + m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs") } -func (m *MsgUtilsCmd) GetUserIDFlag() string { - return m.userID +func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool { + clearAll, _ := cmdLines.Flags().GetBool("clearAll") + return clearAll } -func (m *MsgUtilsCmd) AddFixAllFlag() { - m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs") +func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { + m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID") } -func (m *MsgUtilsCmd) GetFixAllFlag() bool { - return m.fixAll +func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string { + superGroupID, _ := cmdLines.Flags().GetString("superGroupID") + return superGroupID } -func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { - m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID") +func (m *MsgUtilsCmd) AddBeginSeqFlag() { + m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq") } -func (m *MsgUtilsCmd) GetSuperGroupIDFlag() string { - return m.superGroupID +func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 { + beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq") + return beginSeq } -func (m *MsgUtilsCmd) AddClearAllFlag() bool { - return m.clearAll +func (m *MsgUtilsCmd) AddLimitFlag() { + m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit") } -func (m *MsgUtilsCmd) GetClearAllFlag() bool { - return m.clearAll +func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 { + limit, _ := cmdLines.Flags().GetInt64("limit") + return limit } -type SeqCmd struct { - Command *cobra.Command +func (m *MsgUtilsCmd) Execute() error { + return m.Command.Execute() } -func (SeqCmd) RunCommand(cmdLines *cobra.Command, args []string) error { - msgTool, err := tools.InitMsgTool() - if err != nil { - return err +func NewMsgUtilsCmd(use, short string, args cobra.PositionalArgs) *MsgUtilsCmd { + return &MsgUtilsCmd{ + Command: cobra.Command{ + Use: use, + Short: short, + Args: args, + }, } - 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) - } +} + +type GetCmd struct { + *MsgUtilsCmd +} + +func NewGetCmd() *GetCmd { + return &GetCmd{ + NewMsgUtilsCmd("get [resource]", "get action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)), } - 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 +type FixCmd struct { + *MsgUtilsCmd } -type MsgCmd struct { - Command *cobra.Command +func NewFixCmd() *FixCmd { + return &FixCmd{ + NewMsgUtilsCmd("fix [resource]", "fix action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)), + } } -func NewMsgCmd() MsgCmd { - msgCmd := MsgCmd{&cobra.Command{ - Use: "msg", - Short: "msg operation", - }} - msgCmd.Command.RunE = msgCmd.RunCommand - msgCmd.Command.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs") - return msgCmd +type ClearCmd struct { + *MsgUtilsCmd } -func (*MsgCmd) RunCommand(cmdLines *cobra.Command, args []string) error { - msgTool, err := tools.InitMsgTool() - if err != nil { - return err +func NewClearCmd() *ClearCmd { + return &ClearCmd{ + NewMsgUtilsCmd("clear [resource]", "clear action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)), } - 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) +} + +type SeqCmd struct { + *MsgUtilsCmd +} + +func NewSeqCmd() *SeqCmd { + seqCmd := &SeqCmd{ + NewMsgUtilsCmd("seq", "seq", nil), + } + 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: - switch { - case userID != "": - msgTool.ClearUsersMsg(ctx, []string{userID}) - case superGroupID != "": - msgTool.ClearSuperGroupMsg(ctx, []string{superGroupID}) - case clearAll: - msgTool.AllUserClearMsgAndFixSeq() + 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 nil + return &s.Command +} + +func (s *SeqCmd) FixSeqCmd() *cobra.Command { + return &s.Command +} + +type MsgCmd struct { + *MsgUtilsCmd } -var GetCmd = &cobra.Command{ - Use: "get", - Short: "get operation", +func NewMsgCmd() *MsgCmd { + msgCmd := &MsgCmd{ + NewMsgUtilsCmd("msg", "msg", nil), + } + return msgCmd } -var FixCmd = &cobra.Command{ - Use: "fix", - Short: "fix seq operation", +func (m *MsgCmd) GetMsgCmd() *cobra.Command { + return &m.Command } -var ClearCmd = &cobra.Command{ - Use: "clear", - Short: "clear operation", +func (m *MsgCmd) ClearMsgCmd() *cobra.Command { + return &m.Command }