Ajouter config.RegisterInt et config.RegisterString
This commit is contained in:
parent
54b79d7db3
commit
5b109ae973
1 changed files with 22 additions and 0 deletions
|
@ -26,6 +26,28 @@ func UnmarshalConfig() (cfg Config, err error) {
|
|||
return cfg, viper.Unmarshal(&cfg)
|
||||
}
|
||||
|
||||
// RegisterInt registers a new cobra int flag and associated viper config option
|
||||
func RegisterInt(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault int) error {
|
||||
if isPersistent {
|
||||
cmd.PersistentFlags().Int(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
||||
} else {
|
||||
cmd.Flags().Int(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterString registers a new cobra string flag and associated viper config option
|
||||
func RegisterString(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault string) error {
|
||||
if isPersistent {
|
||||
cmd.PersistentFlags().String(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
||||
} else {
|
||||
cmd.Flags().String(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command
|
||||
func RegisterFlags(cmd *cobra.Command) error {
|
||||
// api.port ; --api-port
|
||||
|
|
Loading…
Reference in a new issue