diff --git a/cmd/api/main.go b/cmd/api/main.go index bf62ffa70..c8602b3d6 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -15,17 +15,16 @@ import ( ) func main() { - rootCmd := cmd.NewRootCmd() - rootCmd.AddPortFlag() - rootCmd.AddRunE(run) - if err := rootCmd.Execute(); err != nil { + apiCmd := cmd.NewApiCmd() + apiCmd.AddPortFlag() + apiCmd.AddApi(run) + if err := apiCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } -func run(rootCmd cmd.RootCmd) error { - port := rootCmd.GetPortFlag() +func run(port int) error { if port == 0 { port = config.Config.Api.GinPort[0] } diff --git a/cmd/cmdutils/main.go b/cmd/cmdutils/main.go index 2cf801ba4..577eda1c6 100644 --- a/cmd/cmdutils/main.go +++ b/cmd/cmdutils/main.go @@ -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) } diff --git a/cmd/crontask/main.go b/cmd/crontask/main.go index 8962e1a34..7d7793c09 100644 --- a/cmd/crontask/main.go +++ b/cmd/crontask/main.go @@ -2,17 +2,16 @@ package main import ( "OpenIM/internal/tools" - "OpenIM/pkg/common/config" + "OpenIM/pkg/common/cmd" "fmt" - "time" + "os" ) func main() { - fmt.Println(time.Now(), "start cronTask") - if err := config.InitConfig(""); err != nil { - panic(err.Error()) - } - if err := tools.StartCronTask(); err != nil { - panic(err.Error()) + cronTaskCmd := cmd.NewCronTaskCmd() + cronTaskCmd.AddRunE(tools.StartCronTask) + if err := cronTaskCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/pkg/common/cmd/api.go b/pkg/common/cmd/api.go new file mode 100644 index 000000000..46c8f6011 --- /dev/null +++ b/pkg/common/cmd/api.go @@ -0,0 +1,17 @@ +package cmd + +import "github.com/spf13/cobra" + +type ApiCmd struct { + *RootCmd +} + +func NewApiCmd() *ApiCmd { + return &ApiCmd{NewRootCmd()} +} + +func (a *ApiCmd) AddApi(f func(port int) error) { + a.Command.RunE = func(cmd *cobra.Command, args []string) error { + return f(a.GetPortFlag()) + } +} diff --git a/pkg/common/cmd/cron_task.go b/pkg/common/cmd/cron_task.go new file mode 100644 index 000000000..ba577dac9 --- /dev/null +++ b/pkg/common/cmd/cron_task.go @@ -0,0 +1,17 @@ +package cmd + +import "github.com/spf13/cobra" + +type CronTaskCmd struct { + *RootCmd +} + +func NewCronTaskCmd() *CronTaskCmd { + return &CronTaskCmd{NewRootCmd()} +} + +func (c *CronTaskCmd) AddRunE(f func() error) { + c.Command.RunE = func(cmd *cobra.Command, args []string) error { + return f() + } +} diff --git a/pkg/common/cmd/msg.go b/pkg/common/cmd/msg.go index d1725eb03..e1ea27185 100644 --- a/pkg/common/cmd/msg.go +++ b/pkg/common/cmd/msg.go @@ -1,18 +1,20 @@ package cmd +import ( + "OpenIM/internal/tools" + "context" + "github.com/spf13/cobra" +) + type MsgUtilsCmd struct { *RootCmd - userID string - userIDFlag bool + userID string - superGroupID string - superGroupIDFlag bool + superGroupID string - clearAll bool - clearAllFlag bool + clearAll bool - fixAll bool - fixAllFlag bool + fixAll bool } func NewMsgUtilsCmd() MsgUtilsCmd { @@ -21,22 +23,135 @@ func NewMsgUtilsCmd() MsgUtilsCmd { func (m *MsgUtilsCmd) AddUserIDFlag() { m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID") - m.userIDFlag = true } -func (m *MsgUtilsCmd) getUserIDFlag() { - m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID") - -} - -func (m *MsgUtilsCmd) AddGroupIDFlag() { - m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID") -} - -func (m *MsgUtilsCmd) AddClearAllFlag() { - m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs") +func (m *MsgUtilsCmd) GetUserIDFlag() string { + return m.userID } func (m *MsgUtilsCmd) AddFixAllFlag() { m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs") } + +func (m *MsgUtilsCmd) GetFixAllFlag() bool { + return m.fixAll +} + +func (m *MsgUtilsCmd) AddSuperGroupIDFlag() { + m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID") +} + +func (m *MsgUtilsCmd) GetSuperGroupIDFlag() string { + return m.superGroupID +} + +func (m *MsgUtilsCmd) AddClearAllFlag() bool { + return m.clearAll +} + +func (m *MsgUtilsCmd) GetClearAllFlag() bool { + return m.clearAll +} + +type SeqCmd struct { + Command *cobra.Command +} + +func (SeqCmd) RunCommand(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 +} + +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 MsgCmd struct { + Command *cobra.Command +} + +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 +} + +func (*MsgCmd) RunCommand(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", +} diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go new file mode 100644 index 000000000..1d619dd05 --- /dev/null +++ b/pkg/common/cmd/msg_gateway.go @@ -0,0 +1 @@ +package cmd diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index e1269fe9f..09f7ea18e 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -7,12 +7,10 @@ import ( ) type RootCmd struct { - Command cobra.Command - port int - portFlag bool + Command cobra.Command + port int - prometheusPort int - prometheusPortFlag bool + prometheusPort int } func NewRootCmd() (rootCmd *RootCmd) { @@ -22,12 +20,8 @@ func NewRootCmd() (rootCmd *RootCmd) { Short: "Start the server", Long: `Start the server`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - //if rootCmd.portFlag { rootCmd.port = rootCmd.getPortFlag(cmd) - //} - //if rootCmd.prometheusPortFlag { rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd) - //} return rootCmd.getConfFromCmdAndInit(cmd) }, } @@ -54,7 +48,6 @@ func (r *RootCmd) init() { func (r *RootCmd) AddPortFlag() { r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port") - r.portFlag = true } func (r *RootCmd) getPortFlag(cmd *cobra.Command) int { @@ -68,7 +61,6 @@ func (r *RootCmd) GetPortFlag() int { func (r *RootCmd) AddPrometheusPortFlag() { r.Command.Flags().StringP(constant.PrometheusPort, "pp", "", "server listen port") - r.prometheusPortFlag = true } func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int { @@ -76,6 +68,10 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int { return port } +func (r *RootCmd) GetPrometheusPortFlag() int { + return r.prometheusPort +} + func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error { configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf) return config.InitConfig(configFolderPath) diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go new file mode 100644 index 000000000..a1a267ecc --- /dev/null +++ b/pkg/common/cmd/rpc.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "OpenIM/pkg/discoveryregistry" + "github.com/spf13/cobra" + "google.golang.org/grpc" +) + +type RpcCmd struct { + *RootCmd +} + +func NewRpcCmd() *RpcCmd { + return &RpcCmd{NewRootCmd()} +} + +func (r *RpcCmd) AddRpc(f func(port, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error) { + r.Command.RunE = func(cmd *cobra.Command, args []string) error { + return f(r.port, r.prometheusPort) + } +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 5dc227243..f9ff6dc55 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -502,6 +502,9 @@ func (c *config) initConfig(config interface{}, configName, configFolderPath str configFolderPath = DefaultFolderPath } configPath := filepath.Join(configFolderPath, configName) + defer func() { + fmt.Println("use config", configPath) + }() _, err := os.Stat(configPath) if err != nil { if !os.IsNotExist(err) { @@ -527,9 +530,6 @@ func (c *config) GetConfFromRegistry(registry discoveryregistry.SvcDiscoveryRegi } func InitConfig(configFolderPath string) error { - defer func() { - fmt.Println("use config folder", configFolderPath) - }() err := Config.initConfig(&Config, FileName, configFolderPath) if err != nil { return err