test cobra

This commit is contained in:
wangchuxiao
2023-03-08 11:45:54 +08:00
parent 60876e865b
commit e55167d0bf
10 changed files with 223 additions and 144 deletions
+7 -11
View File
@@ -7,12 +7,10 @@ import (
)
type RootCmd struct {
Command cobra.Command
port int
portFlag bool
Command cobra.Command
port int
prometheusPort int
prometheusPortFlag bool
prometheusPort int
}
func NewRootCmd() (rootCmd *RootCmd) {
@@ -22,12 +20,8 @@ func NewRootCmd() (rootCmd *RootCmd) {
Short: "Start the server",
Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
//if rootCmd.portFlag {
rootCmd.port = rootCmd.getPortFlag(cmd)
//}
//if rootCmd.prometheusPortFlag {
rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
//}
return rootCmd.getConfFromCmdAndInit(cmd)
},
}
@@ -54,7 +48,6 @@ func (r *RootCmd) init() {
func (r *RootCmd) AddPortFlag() {
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
r.portFlag = true
}
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
@@ -68,7 +61,6 @@ func (r *RootCmd) GetPortFlag() int {
func (r *RootCmd) AddPrometheusPortFlag() {
r.Command.Flags().StringP(constant.PrometheusPort, "pp", "", "server listen port")
r.prometheusPortFlag = true
}
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
@@ -76,6 +68,10 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
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)