From 5b109ae9736231c94a34f2af6b6271163f56aa0d Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 18:10:14 -0400 Subject: [PATCH] Ajouter config.RegisterInt et config.RegisterString --- config/config.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/config/config.go b/config/config.go index 3e80868..61b0c6a 100644 --- a/config/config.go +++ b/config/config.go @@ -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