2020-04-27 22:26:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-12-26 13:11:49 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
2020-12-26 13:11:49 +00:00
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/path"
|
2020-04-27 22:26:15 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func CreateConfig(flags *pflag.FlagSet) Config {
|
2020-04-27 22:26:15 +00:00
|
|
|
logger, _ := logging.CreateLogger()
|
|
|
|
configInstance := &config{
|
|
|
|
logger: logger.Named("Config"),
|
|
|
|
cfg: viper.New(),
|
|
|
|
}
|
|
|
|
|
|
|
|
configInstance.cfg.SetConfigName("config")
|
|
|
|
configInstance.cfg.SetConfigType("yaml")
|
|
|
|
configInstance.cfg.AddConfigPath("/etc/inetmock/")
|
|
|
|
configInstance.cfg.AddConfigPath("$HOME/.inetmock")
|
|
|
|
configInstance.cfg.AddConfigPath(".")
|
|
|
|
configInstance.cfg.SetEnvPrefix("INetMock")
|
|
|
|
_ = configInstance.cfg.BindPFlags(flags)
|
|
|
|
configInstance.cfg.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
|
|
configInstance.cfg.AutomaticEnv()
|
|
|
|
|
|
|
|
for k, v := range registeredDefaults {
|
|
|
|
configInstance.cfg.SetDefault(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range registeredAliases {
|
|
|
|
configInstance.cfg.RegisterAlias(k, v)
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
return configInstance
|
2020-04-27 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config interface {
|
|
|
|
ReadConfig(configFilePath string) error
|
|
|
|
ReadConfigString(config, format string) error
|
|
|
|
Viper() *viper.Viper
|
|
|
|
TLSConfig() CertOptions
|
2020-05-03 08:15:24 +00:00
|
|
|
APIConfig() RPC
|
2020-12-26 13:11:49 +00:00
|
|
|
EndpointConfigs() map[string]EndpointConfig
|
2020-04-27 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2020-06-15 10:04:08 +00:00
|
|
|
cfg *viper.Viper
|
|
|
|
logger logging.Logger
|
|
|
|
TLS CertOptions
|
2020-12-26 13:11:49 +00:00
|
|
|
Endpoints map[string]EndpointConfig
|
2020-06-15 10:04:08 +00:00
|
|
|
API RPC
|
2020-05-03 08:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) APIConfig() RPC {
|
|
|
|
return c.API
|
2020-04-27 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) ReadConfigString(config, format string) (err error) {
|
|
|
|
c.cfg.SetConfigType(format)
|
|
|
|
if err = c.cfg.ReadConfig(strings.NewReader(config)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.cfg.Unmarshal(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func (c *config) EndpointConfigs() map[string]EndpointConfig {
|
2020-04-27 22:26:15 +00:00
|
|
|
return c.Endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) TLSConfig() CertOptions {
|
|
|
|
return c.TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Viper() *viper.Viper {
|
|
|
|
return c.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) ReadConfig(configFilePath string) (err error) {
|
|
|
|
if configFilePath != "" && path.FileExists(configFilePath) {
|
|
|
|
c.logger.Info(
|
|
|
|
"loading config from passed config file path",
|
|
|
|
zap.String("configFilePath", configFilePath),
|
|
|
|
)
|
|
|
|
c.cfg.SetConfigFile(configFilePath)
|
|
|
|
}
|
|
|
|
if err = c.cfg.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
err = nil
|
|
|
|
c.logger.Warn("failed to load config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.cfg.Unmarshal(c)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|