test cobra

This commit is contained in:
wangchuxiao 2023-03-07 20:41:09 +08:00
parent f0943276cb
commit 30e9b44054

View File

@ -15,14 +15,13 @@ type RootCmd struct {
prometheusPortFlag bool prometheusPortFlag bool
} }
func NewRootCmd() *RootCmd { func NewRootCmd() (rootCmd *RootCmd) {
rootCmd = &RootCmd{}
c := cobra.Command{ c := cobra.Command{
Use: "start", Use: "start",
Short: "Start the server", Short: "Start the server",
Long: `Start the server`, Long: `Start the server`,
} PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
rootCmd := &RootCmd{}
c.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if rootCmd.portFlag { if rootCmd.portFlag {
rootCmd.port = rootCmd.getPortFlag(cmd) rootCmd.port = rootCmd.getPortFlag(cmd)
} }
@ -30,6 +29,7 @@ func NewRootCmd() *RootCmd {
rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd) rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
} }
return rootCmd.getConfFromCmdAndInit(cmd) return rootCmd.getConfFromCmdAndInit(cmd)
},
} }
rootCmd.Command = c rootCmd.Command = c
rootCmd.init() rootCmd.init()