mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-09 03:25:59 +08:00
feat: use robot to migrate code
Signed-off-by: kubbot & kubecub <3293172751ysy@gmail.com>
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("api")}
|
||||
}
|
||||
|
||||
func (a *ApiCmd) AddApi(f func(port int) error) {
|
||||
a.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return f(a.getPortFlag(cmd))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
type CronTaskCmd struct {
|
||||
*RootCmd
|
||||
}
|
||||
|
||||
func NewCronTaskCmd() *CronTaskCmd {
|
||||
return &CronTaskCmd{NewRootCmd("cronTask")}
|
||||
}
|
||||
|
||||
func (c *CronTaskCmd) addRunE(f func() error) {
|
||||
c.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return f()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CronTaskCmd) Exec(f func() error) error {
|
||||
c.addRunE(f)
|
||||
return c.Execute()
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/internal/msggateway"
|
||||
//"github.com/OpenIMSDK/Open-IM-Server/internal/msggateway"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type MsgGatewayCmd struct {
|
||||
*RootCmd
|
||||
}
|
||||
|
||||
func NewMsgGatewayCmd() MsgGatewayCmd {
|
||||
return MsgGatewayCmd{NewRootCmd("msgGateway")}
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) AddWsPortFlag() {
|
||||
m.Command.Flags().IntP(constant.FlagWsPort, "w", 0, "ws server listen port")
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int {
|
||||
port, _ := cmd.Flags().GetInt(constant.FlagWsPort)
|
||||
return port
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) addRunE() {
|
||||
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return msggateway.RunWsAndServer(m.getPortFlag(cmd), m.getWsPortFlag(cmd), m.getPrometheusPortFlag(cmd))
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) Exec() error {
|
||||
m.addRunE()
|
||||
return m.Execute()
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/internal/msgtransfer"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type MsgTransferCmd struct {
|
||||
*RootCmd
|
||||
}
|
||||
|
||||
func NewMsgTransferCmd() MsgTransferCmd {
|
||||
return MsgTransferCmd{NewRootCmd("msgTransfer")}
|
||||
}
|
||||
|
||||
func (m *MsgTransferCmd) addRunE() {
|
||||
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return msgtransfer.StartTransfer(m.getPrometheusPortFlag(cmd))
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MsgTransferCmd) Exec() error {
|
||||
m.addRunE()
|
||||
return m.Execute()
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/internal/tools"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type MsgUtilsCmd struct {
|
||||
cobra.Command
|
||||
msgTool *tools.MsgTool
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
||||
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
|
||||
userID, _ := cmdLines.Flags().GetString("userID")
|
||||
return userID
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddFixAllFlag() {
|
||||
m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool {
|
||||
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() {
|
||||
m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
|
||||
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||
return superGroupID
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddBeginSeqFlag() {
|
||||
m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 {
|
||||
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 {
|
||||
*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)
|
||||
}
|
||||
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 (s *SeqCmd) FixSeqCmd() *cobra.Command {
|
||||
return &s.Command
|
||||
}
|
||||
|
||||
type MsgCmd struct {
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func NewMsgCmd() *MsgCmd {
|
||||
msgCmd := &MsgCmd{
|
||||
NewMsgUtilsCmd("msg", "msg", nil),
|
||||
}
|
||||
return msgCmd
|
||||
}
|
||||
|
||||
func (m *MsgCmd) GetMsgCmd() *cobra.Command {
|
||||
return &m.Command
|
||||
}
|
||||
|
||||
func (m *MsgCmd) ClearMsgCmd() *cobra.Command {
|
||||
return &m.Command
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type RootCmd struct {
|
||||
Command cobra.Command
|
||||
Name string
|
||||
port int
|
||||
prometheusPort int
|
||||
}
|
||||
|
||||
func NewRootCmd(name string) (rootCmd *RootCmd) {
|
||||
rootCmd = &RootCmd{Name: name}
|
||||
c := cobra.Command{
|
||||
Use: "start",
|
||||
Short: fmt.Sprintf(`Start %s server`, name),
|
||||
Long: fmt.Sprintf(`Start %s server`, name),
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := log.InitFromConfig("OpenIM.log.all", name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
rootCmd.Command = c
|
||||
rootCmd.addConfFlag()
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
func (r *RootCmd) addConfFlag() {
|
||||
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
||||
}
|
||||
|
||||
func (r *RootCmd) AddPortFlag() {
|
||||
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
|
||||
}
|
||||
|
||||
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
|
||||
port, _ := cmd.Flags().GetInt(constant.FlagPort)
|
||||
return port
|
||||
}
|
||||
|
||||
func (r *RootCmd) GetPortFlag() int {
|
||||
return r.port
|
||||
}
|
||||
|
||||
func (r *RootCmd) AddPrometheusPortFlag() {
|
||||
r.Command.Flags().IntP(constant.FlagPrometheusPort, "", 0, "server prometheus listen port")
|
||||
}
|
||||
|
||||
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
|
||||
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
|
||||
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)
|
||||
}
|
||||
|
||||
func (r *RootCmd) Execute() error {
|
||||
return r.Command.Execute()
|
||||
}
|
||||
|
||||
func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
|
||||
r.Command.AddCommand(cmds...)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/startrpc"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type RpcCmd struct {
|
||||
*RootCmd
|
||||
}
|
||||
|
||||
func NewRpcCmd(name string) *RpcCmd {
|
||||
authCmd := &RpcCmd{NewRootCmd(name)}
|
||||
return authCmd
|
||||
}
|
||||
|
||||
func (a *RpcCmd) Exec() error {
|
||||
a.Command.Run = func(cmd *cobra.Command, args []string) {
|
||||
a.port = a.getPortFlag(cmd)
|
||||
a.prometheusPort = a.getPrometheusPortFlag(cmd)
|
||||
}
|
||||
return a.Execute()
|
||||
}
|
||||
|
||||
func (a *RpcCmd) StartSvr(name string, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
||||
if a.GetPortFlag() == 0 {
|
||||
return errors.New("port is required")
|
||||
}
|
||||
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn)
|
||||
}
|
||||
Reference in New Issue
Block a user