Files
open-im-server/pkg/common/cmd/root.go
T

78 lines
1.9 KiB
Go
Raw Normal View History

2023-03-07 12:12:16 +08:00
package cmd
import (
2023-03-13 12:34:56 +08:00
"fmt"
2023-03-16 10:46:06 +08:00
"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"
2023-03-07 12:12:16 +08:00
"github.com/spf13/cobra"
)
2023-03-07 18:06:57 +08:00
type RootCmd struct {
2023-03-09 13:26:58 +08:00
Command cobra.Command
2023-03-13 12:34:56 +08:00
Name string
2023-03-09 13:26:58 +08:00
port int
prometheusPort int
2023-03-07 18:06:57 +08:00
}
2023-03-07 17:59:34 +08:00
2023-03-13 12:34:56 +08:00
func NewRootCmd(name string) (rootCmd *RootCmd) {
rootCmd = &RootCmd{Name: name}
2023-03-07 17:59:34 +08:00
c := cobra.Command{
2023-03-07 12:12:16 +08:00
Use: "start",
2023-03-13 12:34:56 +08:00
Short: fmt.Sprintf(`Start %s server`, name),
Long: fmt.Sprintf(`Start %s server`, name),
2023-03-07 20:41:09 +08:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2023-03-13 12:34:56 +08:00
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
return err
}
return log.InitFromConfig(name)
2023-03-07 20:41:09 +08:00
},
2023-03-07 19:56:22 +08:00
}
2023-03-07 20:07:44 +08:00
rootCmd.Command = c
2023-03-09 14:54:05 +08:00
rootCmd.addConfFlag()
2023-03-07 18:06:57 +08:00
return rootCmd
2023-03-07 17:59:34 +08:00
}
2023-03-09 14:54:05 +08:00
func (r *RootCmd) addConfFlag() {
2023-03-07 18:06:57 +08:00
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
2023-03-07 17:59:34 +08:00
}
2023-03-07 20:12:36 +08:00
func (r *RootCmd) AddPortFlag() {
2023-03-07 20:39:07 +08:00
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
2023-03-07 19:56:22 +08:00
}
2023-03-07 20:12:36 +08:00
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
2023-03-07 19:56:22 +08:00
port, _ := cmd.Flags().GetInt(constant.FlagPort)
return port
}
2023-03-09 13:26:58 +08:00
func (r *RootCmd) GetPortFlag() int {
return r.port
}
2023-03-07 20:12:36 +08:00
func (r *RootCmd) AddPrometheusPortFlag() {
2023-03-08 18:19:56 +08:00
r.Command.Flags().String(constant.FlagPrometheusPort, "", "server prometheus listen port")
2023-03-07 19:56:22 +08:00
}
2023-03-07 20:31:40 +08:00
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
2023-03-08 13:34:12 +08:00
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
2023-03-07 19:56:22 +08:00
return port
}
2023-03-09 13:26:58 +08:00
func (r *RootCmd) GetPrometheusPortFlag() int {
return r.prometheusPort
}
2023-03-07 20:12:36 +08:00
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
2023-03-07 17:59:34 +08:00
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
return config.InitConfig(configFolderPath)
2023-03-07 12:12:16 +08:00
}
2023-03-07 20:07:13 +08:00
2023-03-07 20:12:36 +08:00
func (r *RootCmd) Execute() error {
2023-03-07 20:07:13 +08:00
return r.Command.Execute()
}
2023-03-07 20:31:40 +08:00
func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
r.Command.AddCommand(cmds...)
}