You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/cmd/root.go

74 lines
1.8 KiB

2 years ago
package cmd
import (
2 years ago
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
2 years ago
"github.com/spf13/cobra"
)
2 years ago
type RootCmd struct {
2 years ago
Command cobra.Command
2 years ago
}
2 years ago
2 years ago
func NewRootCmd() (rootCmd *RootCmd) {
rootCmd = &RootCmd{}
2 years ago
c := cobra.Command{
2 years ago
Use: "start",
Short: "Start the server",
Long: `Start the server`,
2 years ago
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return rootCmd.getConfFromCmdAndInit(cmd)
},
2 years ago
}
2 years ago
rootCmd.Command = c
2 years ago
rootCmd.init()
2 years ago
return rootCmd
2 years ago
}
2 years ago
func (r *RootCmd) AddRunE(f func(cmd RootCmd) error) {
2 years ago
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
2 years ago
return f(*r)
2 years ago
}
}
2 years ago
func (r *RootCmd) AddRpc(f func(port, prometheusPort int) error) {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
2 years ago
return f(r.getPortFlag(cmd), r.getPrometheusPortFlag(cmd))
2 years ago
}
}
2 years ago
func (r *RootCmd) init() {
2 years ago
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
2 years ago
}
2 years ago
func (r *RootCmd) AddPortFlag() {
2 years ago
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
2 years ago
}
2 years ago
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
2 years ago
port, _ := cmd.Flags().GetInt(constant.FlagPort)
return port
}
2 years ago
func (r *RootCmd) AddPrometheusPortFlag() {
2 years ago
r.Command.Flags().String(constant.FlagPrometheusPort, "", "server prometheus listen port")
2 years ago
}
2 years ago
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
2 years ago
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
2 years ago
return port
}
2 years ago
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
2 years ago
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
return config.InitConfig(configFolderPath)
2 years ago
}
2 years ago
2 years ago
func (r *RootCmd) Execute() error {
2 years ago
return r.Command.Execute()
}
2 years ago
func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
r.Command.AddCommand(cmds...)
}