[#83] Update flag bindings

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-11-30 16:15:54 +03:00 committed by Alex Vanin
parent fc13e5407c
commit 2345fe6441
3 changed files with 37 additions and 167 deletions

View file

@ -134,8 +134,10 @@ func config() *viper.Viper {
v.SetDefault(cfgLoggerLevel, "debug") v.SetDefault(cfgLoggerLevel, "debug")
// Bind flags // Bind flags
if err := bindFlags(v, flagSet); err != nil { for cfg, cmd := range bindings {
panic(fmt.Errorf("bind flags: %w", err)) if err := v.BindPFlag(cfg, flagSet.Lookup(cmd)); err != nil {
panic(fmt.Errorf("bind flags: %w", err))
}
} }
if err := flagSet.Parse(os.Args); err != nil { if err := flagSet.Parse(os.Args); err != nil {
@ -185,34 +187,42 @@ func config() *viper.Viper {
return v return v
} }
func bindFlags(v *viper.Viper, flagSet *pflag.FlagSet) error { func init() {
if err := v.BindPFlag(cfgPprofEnabled, flagSet.Lookup(cmdPprof)); err != nil { for _, flagName := range serverFlags {
return err cfgName := cfgServerSection + flagName
} bindings[cfgName] = flagName
if err := v.BindPFlag(cfgPrometheusEnabled, flagSet.Lookup(cmdMetrics)); err != nil { knownConfigParams[cfgName] = struct{}{}
return err
}
if err := v.BindPFlag(cfgNodeDialTimeout, flagSet.Lookup(cmdNodeDialTimeout)); err != nil {
return err
}
if err := v.BindPFlag(cfgHealthcheckTimeout, flagSet.Lookup(cmdHealthcheckTimeout)); err != nil {
return err
}
if err := v.BindPFlag(cfgRebalance, flagSet.Lookup(cmdRebalance)); err != nil {
return err
}
if err := v.BindPFlag(cfgWalletPath, flagSet.Lookup(cmdWallet)); err != nil {
return err
}
if err := v.BindPFlag(cfgWalletAddress, flagSet.Lookup(cmdAddress)); err != nil {
return err
} }
}
if err := restapi.BindFlagsToConfig(v, flagSet, cfgServerSection); err != nil { var serverFlags = []string{
return err restapi.FlagScheme,
} restapi.FlagCleanupTimeout,
restapi.FlagGracefulTimeout,
restapi.FlagMaxHeaderSize,
restapi.FlagListenAddress,
restapi.FlagListenLimit,
restapi.FlagKeepAlive,
restapi.FlagReadTimeout,
restapi.FlagWriteTimeout,
restapi.FlagTLSListenAddress,
restapi.FlagTLSCertificate,
restapi.FlagTLSKey,
restapi.FlagTLSCa,
restapi.FlagTLSListenLimit,
restapi.FlagTLSKeepAlive,
restapi.FlagTLSReadTimeout,
restapi.FlagTLSWriteTimeout,
}
return nil var bindings = map[string]string{
cfgPprofEnabled: cmdPprof,
cfgPrometheusEnabled: cmdMetrics,
cfgNodeDialTimeout: cmdNodeDialTimeout,
cfgHealthcheckTimeout: cmdHealthcheckTimeout,
cfgRebalance: cmdRebalance,
cfgWalletPath: cmdWallet,
cfgWalletAddress: cmdAddress,
} }
var knownConfigParams = map[string]struct{}{ var knownConfigParams = map[string]struct{}{
@ -228,24 +238,6 @@ var knownConfigParams = map[string]struct{}{
cfgPrometheusAddress: {}, cfgPrometheusAddress: {},
cfgPprofEnabled: {}, cfgPprofEnabled: {},
cfgPprofAddress: {}, cfgPprofAddress: {},
cfgServerSection + restapi.FlagScheme: {},
cfgServerSection + restapi.FlagCleanupTimeout: {},
cfgServerSection + restapi.FlagGracefulTimeout: {},
cfgServerSection + restapi.FlagMaxHeaderSize: {},
cfgServerSection + restapi.FlagListenAddress: {},
cfgServerSection + restapi.FlagListenLimit: {},
cfgServerSection + restapi.FlagKeepAlive: {},
cfgServerSection + restapi.FlagReadTimeout: {},
cfgServerSection + restapi.FlagWriteTimeout: {},
cfgServerSection + restapi.FlagTLSListenAddress: {},
cfgServerSection + restapi.FlagTLSCertificate: {},
cfgServerSection + restapi.FlagTLSKey: {},
cfgServerSection + restapi.FlagTLSCa: {},
cfgServerSection + restapi.FlagTLSListenLimit: {},
cfgServerSection + restapi.FlagTLSKeepAlive: {},
cfgServerSection + restapi.FlagTLSReadTimeout: {},
cfgServerSection + restapi.FlagTLSWriteTimeout: {},
} }
func validateConfig(cfg *viper.Viper, logger *zap.Logger) { func validateConfig(cfg *viper.Viper, logger *zap.Logger) {

View file

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper"
) )
const ( const (
@ -52,63 +51,3 @@ func BindDefaultFlags(flagSet *pflag.FlagSet) {
flagSet.Duration(FlagTLSReadTimeout, 30*time.Second, "maximum duration before timing out read of the request") flagSet.Duration(FlagTLSReadTimeout, 30*time.Second, "maximum duration before timing out read of the request")
flagSet.Duration(FlagTLSWriteTimeout, 30*time.Second, "maximum duration before timing out write of the response") flagSet.Duration(FlagTLSWriteTimeout, 30*time.Second, "maximum duration before timing out write of the response")
} }
// BindFlagsToConfig maps flags to viper config in specific section.
func BindFlagsToConfig(v *viper.Viper, flagSet *pflag.FlagSet, section string) error {
if err := v.BindPFlag(section+FlagScheme, flagSet.Lookup(FlagScheme)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagCleanupTimeout, flagSet.Lookup(FlagCleanupTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagGracefulTimeout, flagSet.Lookup(FlagGracefulTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagMaxHeaderSize, flagSet.Lookup(FlagMaxHeaderSize)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagListenAddress, flagSet.Lookup(FlagListenAddress)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagListenLimit, flagSet.Lookup(FlagListenLimit)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagKeepAlive, flagSet.Lookup(FlagKeepAlive)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagReadTimeout, flagSet.Lookup(FlagReadTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagWriteTimeout, flagSet.Lookup(FlagWriteTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSListenAddress, flagSet.Lookup(FlagTLSListenAddress)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSCertificate, flagSet.Lookup(FlagTLSCertificate)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSKey, flagSet.Lookup(FlagTLSKey)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSCa, flagSet.Lookup(FlagTLSCa)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSListenLimit, flagSet.Lookup(FlagTLSListenLimit)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSKeepAlive, flagSet.Lookup(FlagTLSKeepAlive)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSReadTimeout, flagSet.Lookup(FlagTLSReadTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSWriteTimeout, flagSet.Lookup(FlagTLSWriteTimeout)); err != nil {
return err
}
return nil
}

View file

@ -10,7 +10,6 @@ import (
"time" "time"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper"
) )
const ( const (
@ -56,63 +55,3 @@ func BindDefaultFlags(flagSet *pflag.FlagSet) {
flagSet.Duration(FlagTLSReadTimeout, 30*time.Second, "maximum duration before timing out read of the request") flagSet.Duration(FlagTLSReadTimeout, 30*time.Second, "maximum duration before timing out read of the request")
flagSet.Duration(FlagTLSWriteTimeout, 30*time.Second, "maximum duration before timing out write of the response") flagSet.Duration(FlagTLSWriteTimeout, 30*time.Second, "maximum duration before timing out write of the response")
} }
// BindFlagsToConfig maps flags to viper config in specific section.
func BindFlagsToConfig(v *viper.Viper, flagSet *pflag.FlagSet, section string) error {
if err := v.BindPFlag(section+FlagScheme, flagSet.Lookup(FlagScheme)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagCleanupTimeout, flagSet.Lookup(FlagCleanupTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagGracefulTimeout, flagSet.Lookup(FlagGracefulTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagMaxHeaderSize, flagSet.Lookup(FlagMaxHeaderSize)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagListenAddress, flagSet.Lookup(FlagListenAddress)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagListenLimit, flagSet.Lookup(FlagListenLimit)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagKeepAlive, flagSet.Lookup(FlagKeepAlive)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagReadTimeout, flagSet.Lookup(FlagReadTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagWriteTimeout, flagSet.Lookup(FlagWriteTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSListenAddress, flagSet.Lookup(FlagTLSListenAddress)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSCertificate, flagSet.Lookup(FlagTLSCertificate)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSKey, flagSet.Lookup(FlagTLSKey)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSCa, flagSet.Lookup(FlagTLSCa)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSListenLimit, flagSet.Lookup(FlagTLSListenLimit)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSKeepAlive, flagSet.Lookup(FlagTLSKeepAlive)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSReadTimeout, flagSet.Lookup(FlagTLSReadTimeout)); err != nil {
return err
}
if err := v.BindPFlag(section+FlagTLSWriteTimeout, flagSet.Lookup(FlagTLSWriteTimeout)); err != nil {
return err
}
return nil
}