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

89 lines
2.0 KiB
Go
Raw Normal View History

2023-03-07 12:12:16 +08:00
package cmd
import (
2023-03-07 17:59:34 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
2023-03-10 18:37:36 +08:00
log "OpenIM/pkg/common/logger"
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
port int
prometheusPort int
2023-03-07 18:06:57 +08:00
}
2023-03-07 17:59:34 +08:00
2023-03-07 20:41:09 +08:00
func NewRootCmd() (rootCmd *RootCmd) {
rootCmd = &RootCmd{}
2023-03-07 17:59:34 +08:00
c := cobra.Command{
2023-03-07 12:12:16 +08:00
Use: "start",
Short: "Start the server",
Long: `Start the server`,
2023-03-07 20:41:09 +08:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2023-03-10 18:37:36 +08:00
log.InitFromConfig(log.Config{
JSON: true,
2023-03-10 18:45:23 +08:00
Level: "-1",
2023-03-10 18:37:36 +08:00
Sample: false,
SampleInitial: 0,
SampleInterval: 0,
ItemSampleSeconds: 0,
ItemSampleInitial: 0,
ItemSampleInterval: 0,
}, "newlog")
2023-03-07 20:41:09 +08:00
return rootCmd.getConfFromCmdAndInit(cmd)
},
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-10 18:37:36 +08:00
func (r *RootCmd) SetDesc(use, short, long string) {
r.Command.Use = use
r.Command.Short = short
r.Command.Long = long
}
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...)
}