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