Ajouter config.RegisterInt et config.RegisterString

This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-16 18:10:14 -04:00
parent 54b79d7db3
commit 5b109ae973

View file

@ -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