test cobra

This commit is contained in:
wangchuxiao
2023-03-07 17:59:34 +08:00
parent d6704aed80
commit ea614167a6
6 changed files with 125 additions and 92 deletions
+20 -2
View File
@@ -1,13 +1,31 @@
package cmd
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"github.com/spf13/cobra"
)
func NewRootCmd() *cobra.Command {
return &cobra.Command{
type RootCmd cobra.Command
func NewRootCmd() RootCmd {
c := cobra.Command{
Use: "start",
Short: "Start the server",
Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return getConfFromCmdAndInit(cmd)
},
}
c.Flags()
return RootCmd(c)
}
func (r RootCmd) Init() {
cobra.Command(r).Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
}
func getConfFromCmdAndInit(cmdLines *cobra.Command) error {
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
return config.InitConfig(configFolderPath)
}