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

78 lines
1.9 KiB

2 years ago
package cmd
import (
2 years ago
"fmt"
2 years ago
"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"
2 years ago
"github.com/spf13/cobra"
)
2 years ago
type RootCmd struct {
2 years ago
Command cobra.Command
2 years ago
Name string
2 years ago
port int
prometheusPort int
2 years ago
}
2 years ago
2 years ago
func NewRootCmd(name string) (rootCmd *RootCmd) {
rootCmd = &RootCmd{Name: name}
2 years ago
c := cobra.Command{
2 years ago
Use: "start",
2 years ago
Short: fmt.Sprintf(`Start %s server`, name),
Long: fmt.Sprintf(`Start %s server`, name),
2 years ago
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2 years ago
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
return err
}
return log.InitFromConfig(name)
2 years ago
},
2 years ago
}
2 years ago
rootCmd.Command = c
2 years ago
rootCmd.addConfFlag()
2 years ago
return rootCmd
2 years ago
}
2 years ago
func (r *RootCmd) addConfFlag() {
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) GetPortFlag() int {
return r.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) GetPrometheusPortFlag() int {
return r.prometheusPort
}
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...)
}