test cobra

This commit is contained in:
wangchuxiao 2023-03-08 10:51:45 +08:00
parent 30e9b44054
commit 60876e865b

View File

@ -22,12 +22,12 @@ func NewRootCmd() (rootCmd *RootCmd) {
Short: "Start the server", Short: "Start the server",
Long: `Start the server`, Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if rootCmd.portFlag { //if rootCmd.portFlag {
rootCmd.port = rootCmd.getPortFlag(cmd) rootCmd.port = rootCmd.getPortFlag(cmd)
} //}
if rootCmd.prometheusPortFlag { //if rootCmd.prometheusPortFlag {
rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd) rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
} //}
return rootCmd.getConfFromCmdAndInit(cmd) return rootCmd.getConfFromCmdAndInit(cmd)
}, },
} }
@ -42,6 +42,12 @@ 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)
}
}
func (r *RootCmd) init() { func (r *RootCmd) init() {
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
} }