From bf6232598f5724d2245cd937e58f98f6cde290b7 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 10 Jun 2015 19:27:46 +1000 Subject: [PATCH] Moved check for enabled exchanges > 0 to config value checker. --- config.go | 24 ++++++++++++++++++++---- main.go | 26 +++++--------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/config.go b/config.go index de2f0345..acea0e0f 100644 --- a/config.go +++ b/config.go @@ -18,8 +18,9 @@ var ( ErrExchangeAvailablePairsEmpty = "Exchange %s: Available pairs is empty." ErrExchangeEnabledPairsEmpty = "Exchange %s: Enabled pairs is empty." ErrExchangeBaseCurrenciesEmpty = "Exchange %s: Base currencies is empty." - ErrExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values." + WarningExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values." ErrExchangeNotFound = "Exchange %s: Not found." + ErrNoEnabledExchanges = "No Exchanges enabled." WarningSMSGlobalDefaultOrEmptyValues = "WARNING -- SMS Support disabled due to default or empty Username/Password values." WarningSSMSGlobalSMSContactDefaultOrEmptyValues = "WARNING -- SMS contact #%d Name/Number disabled due to default or empty values." WarningSSMSGlobalSMSNoContacts = "WARNING -- SMS Support disabled due to no enabled contacts." @@ -57,6 +58,16 @@ type Exchanges struct { BaseCurrencies string } +func GetEnabledExchanges() int { + counter := 0 + for i := range bot.config.Exchanges { + if bot.config.Exchanges[i].Enabled { + counter++ + } + } + return counter +} + func GetExchangeConfig(name string) (Exchanges, error) { for i, _ := range bot.config.Exchanges { if bot.config.Exchanges[i].Name == name { @@ -101,7 +112,8 @@ func CheckSMSGlobalConfigValues() error { return nil } -func CheckConfigValues() error { +func CheckExchangeConfigValues() error { + exchanges := 0 for i, exch := range bot.config.Exchanges { if exch.Enabled { if exch.Name == "" { @@ -119,18 +131,22 @@ func CheckConfigValues() error { if exch.AuthenticatedAPISupport { // non-fatal error if exch.APIKey == "" || exch.APISecret == "" || exch.APIKey == "Key" || exch.APISecret == "Secret" { bot.config.Exchanges[i].AuthenticatedAPISupport = false - log.Printf(ErrExchangeAuthAPIDefaultOrEmptyValues, exch.Name) + log.Printf(WarningExchangeAuthAPIDefaultOrEmptyValues, exch.Name) continue } else if exch.Name == "ITBIT" || exch.Name == "Bitstamp" || exch.Name == "Coinbase" { if exch.ClientID == "" || exch.ClientID == "ClientID" { bot.config.Exchanges[i].AuthenticatedAPISupport = false - log.Printf(ErrExchangeAuthAPIDefaultOrEmptyValues, exch.Name) + log.Printf(WarningExchangeAuthAPIDefaultOrEmptyValues, exch.Name) continue } } } + exchanges++ } } + if exchanges == 0 { + return errors.New(ErrNoEnabledExchanges) + } return nil } diff --git a/main.go b/main.go index 732ac250..7ece4ebf 100644 --- a/main.go +++ b/main.go @@ -43,49 +43,33 @@ func main() { err := errors.New("") bot.config, err = ReadConfig() - if err != nil { - log.Println("Fatal error opening config.json file. Error:", err) + log.Printf("Fatal error opening config.json file. Error: %s", err) return } + log.Println("Config file loaded. Checking settings.. ") - err = CheckConfigValues() - + err = CheckExchangeConfigValues() if err != nil { log.Println("Fatal error checking config values. Error:", err) return } err = CheckSMSGlobalConfigValues() - if err != nil { + // non fatal event log.Println(err) } - log.Println("Config file loaded.") log.Printf("Bot '%s' started.\n", bot.config.Name) - if bot.config.SMS.Enabled { log.Printf("SMS support enabled. Number of SMS contacts %d.\n", GetEnabledSMSContacts()) } else { log.Println("SMS support disabled.") } - enabledExchanges := 0 - for _, exch := range bot.config.Exchanges { - if exch.Enabled { - enabledExchanges++ - } - } - - if enabledExchanges == 0 { - log.Println("Bot started with no exchanges supported. Exiting.") - return - } - AdjustGoMaxProcs() - - log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), enabledExchanges) + log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), GetEnabledExchanges()) log.Println("Bot Exchange support:") bot.exchange.anx.SetDefaults()