This commit is contained in:
wangchuxiao
2023-03-08 13:34:12 +08:00
parent 107f4c950b
commit f8ad35a6f7
24 changed files with 209 additions and 125 deletions
+3 -16
View File
@@ -8,9 +8,6 @@ import (
type RootCmd struct {
Command cobra.Command
port int
prometheusPort int
}
func NewRootCmd() (rootCmd *RootCmd) {
@@ -20,8 +17,6 @@ func NewRootCmd() (rootCmd *RootCmd) {
Short: "Start the server",
Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
rootCmd.port = rootCmd.getPortFlag(cmd)
rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
return rootCmd.getConfFromCmdAndInit(cmd)
},
}
@@ -38,7 +33,7 @@ func (r *RootCmd) AddRunE(f func(cmd RootCmd) error) {
func (r *RootCmd) AddRpc(f func(port, prometheusPort int) error) {
r.Command.RunE = func(cmd *cobra.Command, args []string) error {
return f(r.port, r.prometheusPort)
return f(r.getPortFlag(cmd), r.getPrometheusPortFlag(cmd))
}
}
@@ -55,23 +50,15 @@ func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
return port
}
func (r *RootCmd) GetPortFlag() int {
return r.port
}
func (r *RootCmd) AddPrometheusPortFlag() {
r.Command.Flags().StringP(constant.PrometheusPort, "pp", "", "server listen port")
r.Command.Flags().StringP(constant.FlagPrometheusPort, "pp", "", "server prometheus listen port")
}
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
port, _ := cmd.Flags().GetInt(constant.PrometheusPort)
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
return port
}
func (r *RootCmd) GetPrometheusPortFlag() int {
return r.prometheusPort
}
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
return config.InitConfig(configFolderPath)