mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 17:45:59 +08:00
test cobra
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
+135
-20
@@ -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",
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
package cmd
|
||||
+7
-11
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user