diff --git a/pkg/config/app.go b/pkg/config/app.go index 1b8d6881..aba264d7 100644 --- a/pkg/config/app.go +++ b/pkg/config/app.go @@ -71,11 +71,10 @@ type AppConfig struct { // 3. Override with values from the configuration file. // 4. Override with command-line flags. // -// In case of any errors it returns nill with an error object +// In case of any errors it returns nil with an error object func GetConfig() (*AppConfig, error) { instance := &AppConfig{} instance.setDefaultConfig() - // TODO Shouldn't the env vars override the file config, instead of the other way around? instance.setEnvVariableConfig() instance.setFileConfig(viper.GetString("FILE_PATH")) @@ -87,6 +86,9 @@ func GetConfig() (*AppConfig, error) { return nil, fmt.Errorf("Unable to unmarshal config into struct, %v", err) } + if instance.PrivateKeyFile == DefaultPrivKeyFile { + instance.PrivateKeyFile = filepath.Join(instance.MasaDir, DefaultPrivKeyFile) + } instance.APIEnabled = viper.GetBool("api_enabled") keyManager, err := masacrypto.NewKeyManager(instance.PrivateKey, instance.PrivateKeyFile) @@ -121,9 +123,11 @@ func (c *AppConfig) setDefaultConfig() { viper.SetDefault(AllowedPeer, true) viper.SetDefault(LogLevel, "info") viper.SetDefault(LogFilePath, "masa_node.log") - viper.SetDefault(PrivKeyFile, filepath.Join(viper.GetString(MasaDir), "masa_oracle_key")) + // It should be ${MASA_DIR}/masa_oracle_key. However, we really don't know the value of MASA_DIR at this point. + // We set it up with a marker value which we will have to fix once we load the full config. + viper.SetDefault(PrivKeyFile, DefaultPrivKeyFile) - viper.SetDefault("api_enabled", false) + viper.SetDefault(APIEnabled, false) } // setFileConfig loads configuration from a YAML file. @@ -185,7 +189,7 @@ func (c *AppConfig) setCommandLineConfig() error { pflag.BoolVar(&c.TelegramScraper, "telegramScraper", viper.GetBool(TelegramScraper), "Telegram Scraper") pflag.BoolVar(&c.WebScraper, "webScraper", viper.GetBool(WebScraper), "Web Scraper") pflag.BoolVar(&c.Faucet, "faucet", viper.GetBool(Faucet), "Faucet") - pflag.BoolVar(&c.APIEnabled, "api-enabled", viper.GetBool("api_enabled"), "Enable API server") + pflag.BoolVar(&c.APIEnabled, "api-enabled", viper.GetBool(APIEnabled), "Enable API server") pflag.Parse() diff --git a/pkg/config/constants.go b/pkg/config/constants.go index 3d569ebc..90bb3f4c 100644 --- a/pkg/config/constants.go +++ b/pkg/config/constants.go @@ -31,13 +31,14 @@ const ( Rendezvous = "masa-mdns" PageSize = 25 - TwitterUsername = "TWITTER_USERNAME" - TwitterPassword = "TWITTER_PASSWORD" - Twitter2FaCode = "TWITTER_2FA_CODE" - DiscordBotToken = "DISCORD_BOT_TOKEN" - TwitterScraper = "TWITTER_SCRAPER" - DiscordScraper = "DISCORD_SCRAPER" - TelegramScraper = "TELEGRAM_SCRAPER" - WebScraper = "WEB_SCRAPER" - APIEnabled = "API_ENABLED" + TwitterUsername = "TWITTER_USERNAME" + TwitterPassword = "TWITTER_PASSWORD" + Twitter2FaCode = "TWITTER_2FA_CODE" + DiscordBotToken = "DISCORD_BOT_TOKEN" + TwitterScraper = "TWITTER_SCRAPER" + DiscordScraper = "DISCORD_SCRAPER" + TelegramScraper = "TELEGRAM_SCRAPER" + WebScraper = "WEB_SCRAPER" + APIEnabled = "API_ENABLED" + DefaultPrivKeyFile = "masa_oracle_key" )