Documentation
¶
Overview ¶
Package cli
@author: xwc1125
Package cli ¶
@author: xwc1125
Index ¶
- func LoadConfig(configFile string, key string, result interface{}, ...) error
- func LoadConfigWithViper(_viper *viper.Viper, configFile string, key string, result interface{}, ...) error
- type AppInfo
- type Cli
- func (cli *Cli) AddCommands(cmds ...Command)
- func (cli *Cli) AppInfo() *AppInfo
- func (cli *Cli) Execute() error
- func (cli *Cli) Get(key string) interface{}
- func (cli *Cli) GetBool(key string) bool
- func (cli *Cli) GetCommands() []Command
- func (cli *Cli) GetConfigEnv() string
- func (cli *Cli) GetConfigFile() string
- func (cli *Cli) GetInt(key string) int
- func (cli *Cli) GetInt64(key string) int64
- func (cli *Cli) GetString(key string) string
- func (cli *Cli) InitFlags(useDefaultFlags bool, flagSetFunc func(rootFlags *pflag.FlagSet), ...) (err error)
- func (cli *Cli) RootCmd() *cobra.Command
- func (cli *Cli) Viper() *viper.Viper
- type Command
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadConfig ¶
func LoadConfig(configFile string, key string, result interface{}, event func(e fsnotify.Event)) error
LoadConfig load local profile @params configFile config file pat @params key if key=="",load all the config;else load the key config @params result the result must be ptr @params event the file change event
Types ¶
type AppInfo ¶
type AppInfo struct {
App string `json:"app"`
Version string `json:"version"`
Welcome string `json:"welcome"`
}
AppInfo app Info
type Cli ¶
type Cli struct {
// contains filtered or unexported fields
}
Cli cobra 在使用中,如果执行过中任何的Run或RunE没有执行过,那么cobra.OnInitialize(func func1) 中的func1 就不会执行
func NewCliWithViper ¶
NewCliWithViper 添加viper创建新的命令对象
func (*Cli) AddCommands ¶
AddCommands 往cobra.Command添加多个命令
func (*Cli) InitFlags ¶
func (cli *Cli) InitFlags(useDefaultFlags bool, flagSetFunc func(rootFlags *pflag.FlagSet), readConfigFunc func(viper *viper.Viper)) (err error)
InitFlags 初始化flag viper 按照如下顺序查找flag key: - pflag里面的被命令行显式设置的key - 环境变量显式设置的 - 配置文件显式设置的 - KV存储的 - 通过viper设置的default flag - 如果前面都没有变化,最后使用pflag的默认值
所以在Unmarshal的时候命令行里面显式设置的flag会覆盖配置文件里面的flag 如果配置文件没有这个flag,会用pflag的默认值 @params useDefaultFlags 是否使用默认的flag:config,env @params flagSetFunc flag设置的回调函数,函数中为viper及rootFlags @params readConfigFunc 读取配置的回调函数