github.com/chain5j/chain5j-pkg@v1.0.7/cli/cli_test.go (about) 1 // Package cli 2 // 3 // @author: xwc1125 4 package cli 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/spf13/pflag" 11 "github.com/spf13/viper" 12 ) 13 14 func TestNewCli(t *testing.T) { 15 rootCli := NewCli(&AppInfo{ 16 App: "cli", 17 Version: "v0.0.1", 18 Welcome: "Helle world", 19 }) 20 err := rootCli.InitFlags(true, func(flags *pflag.FlagSet) {}, func(viper *viper.Viper) { 21 // 这一步时,rootFlags已经获取到了值 22 // 当config不为空时,才启用 23 }) 24 if err != nil { 25 t.Log("initCli", "err", err) 26 return 27 } 28 29 // rootCli.AddCommand(GenesisCommand) 30 31 rootCli.Execute() 32 } 33 34 func TestNewCliNoDefault(t *testing.T) { 35 rootCli := NewCli(&AppInfo{ 36 App: "cli", 37 Version: "v0.0.1", 38 Welcome: "Helle world", 39 }) 40 err := rootCli.InitFlags(true, func(flags *pflag.FlagSet) { 41 var configFile string 42 flags.StringVar(&configFile, "config", "./conf/config.yaml", "config file (default is ./conf/config.yaml)") 43 // 初始化配置文件 44 if configFile != "" { 45 rootCli.Viper().SetConfigFile(configFile) 46 } else { 47 rootCli.Viper().SetConfigName("config") 48 // 添加读取的配置文件路径 49 rootCli.Viper().AddConfigPath(".") 50 rootCli.Viper().AddConfigPath("./conf") 51 } 52 if err := rootCli.Viper().ReadInConfig(); err != nil { 53 t.Log("viper.ReadInConfig err", err) 54 os.Exit(1) 55 } 56 }, func(viper *viper.Viper) { 57 // 这一步时,rootFlags已经获取到了值 58 // 当config不为空时,才启用 59 }) 60 if err != nil { 61 t.Log("initCli", "err", err) 62 return 63 } 64 65 // rootCli.AddCommand(GenesisCommand) 66 67 rootCli.Execute() 68 }