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

85 lines
1.9 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
port int
portFlag bool
prometheusPort int
prometheusPortFlag bool
2 years ago
}
2 years ago
2 years ago
func NewRootCmd() *RootCmd {
2 years ago
c := cobra.Command{
2 years ago
Use: "start",
Short: "Start the server",
Long: `Start the server`,
}
2 years ago
rootCmd := &RootCmd{}
2 years ago
c.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if rootCmd.portFlag {
rootCmd.port = rootCmd.getPortFlag(cmd)
}
if rootCmd.prometheusPortFlag {
2 years ago
rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
2 years ago
}
return rootCmd.getConfFromCmdAndInit(cmd)
}
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) 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
r.portFlag = true
}
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) GetPortFlag() int {
2 years ago
return r.port
}
2 years ago
func (r *RootCmd) AddPrometheusPortFlag() {
2 years ago
r.Command.Flags().StringP(constant.PrometheusPort, "pp", "", "server listen port")
r.prometheusPortFlag = true
}
2 years ago
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
2 years ago
port, _ := cmd.Flags().GetInt(constant.PrometheusPort)
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...)
}