Use config.json by default if config file isn't encrypted

Fixes issue: https://github.com/thrasher-/gocryptotrader/issues/48
Allows for auto syntax highlighing in text editors/IDEs
This commit is contained in:
Adrian Gallagher
2017-11-14 14:15:01 +11:00
parent 4799c73317
commit ce908ee939
18 changed files with 65 additions and 43 deletions

View File

@@ -79,7 +79,7 @@ Download and install Go from [Go Downloads](https://golang.org/dl/)
go get github.com/thrasher-/gocryptotrader
cd $GOPATH/src/github.com/thrasher-/gocryptotrader
go install
cp $GOPATH/src/github.com/thrasher-/gocryptotrader/config_example.dat $GOPATH/bin/config.dat
cp $GOPATH/src/github.com/thrasher-/gocryptotrader/config_example.json $GOPATH/bin/config.json
```
Make any neccessary changes to the config file.

View File

@@ -19,9 +19,9 @@ import (
// Constants declared here are filename strings and test strings
const (
ConfigFile = "config.dat"
OldConfigFile = "config.json"
ConfigTestFile = "../testdata/configtest.dat"
EncryptedConfigFile = "config.dat"
ConfigFile = "config.json"
ConfigTestFile = "../testdata/configtest.json"
configFileEncryptionPrompt = 0
configFileEncryptionEnabled = 1
configFileEncryptionDisabled = -1
@@ -47,7 +47,6 @@ var (
WarningWebserverRootWebFolderNotFound = "WARNING -- Webserver support disabled due to missing web folder."
WarningExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
WarningCurrencyExchangeProvider = "WARNING -- Currency exchange provider invalid valid. Reset to Fixer."
RenamingConfigFile = "Renaming config file %s to %s."
Cfg Config
)
@@ -317,34 +316,37 @@ func GetFilePath(file string) string {
return file
}
if flag.Lookup("test.v") == nil {
return ConfigFile
data, err := common.ReadFile(EncryptedConfigFile)
if err == nil {
if ConfirmECS(data) {
return EncryptedConfigFile
}
err = os.Rename(EncryptedConfigFile, ConfigFile)
if err != nil {
log.Fatalf("Unable to rename config file: %s", err)
}
log.Printf("Renaming non-encrypted config file from %s to %s",
EncryptedConfigFile, ConfigFile)
return ConfigFile
}
if !ConfirmECS(data) {
return ConfigFile
}
err = os.Rename(ConfigFile, EncryptedConfigFile)
if err != nil {
log.Fatalf("Unable to rename config file: %s", err)
}
log.Printf("Renaming encrypted config file from %s to %s", ConfigFile,
EncryptedConfigFile)
return EncryptedConfigFile
}
return ConfigTestFile
}
// CheckConfig checks to see if there is an old configuration filename and path
// if found it will change it to correct filename.
func CheckConfig() error {
_, err := common.ReadFile(OldConfigFile)
if err == nil {
err = os.Rename(OldConfigFile, ConfigFile)
if err != nil {
return err
}
log.Printf(RenamingConfigFile+"\n", OldConfigFile, ConfigFile)
}
return nil
}
// ReadConfig verifies and checks for encryption and verifies the unencrypted
// file contains JSON.
func (c *Config) ReadConfig(configPath string) error {
defaultPath := GetFilePath(configPath)
err := CheckConfig()
if err != nil {
return err
}
file, err := common.ReadFile(defaultPath)
if err != nil {
return err

View File

@@ -37,7 +37,7 @@ func TestSetup(t *testing.T) {
setup := ANX{}
setup.Name = "ANX"
anxSetupConfig := config.GetConfig()
anxSetupConfig.LoadConfig("../../testdata/configtest.dat")
anxSetupConfig.LoadConfig("../../testdata/configtest.json")
anxConfig, err := anxSetupConfig.GetExchangeConfig("ANX")
if err != nil {
t.Error("Test Failed - ANX Setup() init error")

View File

@@ -32,7 +32,7 @@ func TestSetup(t *testing.T) {
setup := Bitfinex{}
setup.Name = "Bitfinex"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
bfxConfig, err := cfg.GetExchangeConfig("Bitfinex")
if err != nil {
t.Error("Test Failed - Bitfinex Setup() init error")

View File

@@ -79,10 +79,10 @@ func (b *Bitfinex) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderb
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bitfinex) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
var vals url.Values
vals.Set("limit_bids", "100")
vals.Set("limit_asks", "100")
orderbookNew, err := b.GetOrderbook(p.Pair().String(), vals)
urlVals := url.Values{}
urlVals.Set("limit_bids", "100")
urlVals.Set("limit_asks", "100")
orderbookNew, err := b.GetOrderbook(p.Pair().String(), urlVals)
if err != nil {
return orderBook, err
}

View File

@@ -42,7 +42,7 @@ func TestSetup(t *testing.T) {
b := Bitstamp{}
b.Name = "Bitstamp"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
bConfig, err := cfg.GetExchangeConfig("Bitstamp")
if err != nil {
t.Error("Test Failed - Bitstamp Setup() init error")

View File

@@ -27,7 +27,7 @@ func TestSetup(t *testing.T) {
b := Bittrex{}
b.Name = "Bittrex"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
bConfig, err := cfg.GetExchangeConfig("Bittrex")
if err != nil {
t.Error("Test Failed - Bittrex Setup() init error")

View File

@@ -23,7 +23,7 @@ func TestSetup(t *testing.T) {
t.Parallel()
b.Name = "BTCC"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
bConfig, err := cfg.GetExchangeConfig("BTCC")
if err != nil {
t.Error("Test Failed - BTCC Setup() init error")

View File

@@ -25,7 +25,7 @@ func TestSetup(t *testing.T) {
b := BTCMarkets{}
b.Name = "BTC Markets"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
bConfig, err := cfg.GetExchangeConfig("BTC Markets")
if err != nil {
t.Error("Test Failed - BTC Markets Setup() init error")

View File

@@ -21,7 +21,7 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
gdxConfig, err := cfg.GetExchangeConfig("Bitfinex")
if err != nil {
t.Error("Test Failed - GDAX Setup() init error")

View File

@@ -47,7 +47,7 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
geminiConfig, err := cfg.GetExchangeConfig("Gemini")
if err != nil {
t.Error("Test Failed - Gemini Setup() init error")

View File

@@ -22,7 +22,7 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
itbitConfig, err := cfg.GetExchangeConfig("ITBIT")
if err != nil {
t.Error("Test Failed - Gemini Setup() init error")
@@ -124,6 +124,7 @@ func TestGetOrder(t *testing.T) {
}
func TestCancelOrder(t *testing.T) {
t.Skip()
err := i.CancelOrder("1337", "1337order")
if err == nil {
t.Error("Test Failed - CancelOrder() error", err)

View File

@@ -20,7 +20,7 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
cfg.LoadConfig("../../testdata/configtest.json")
liquiConfig, err := cfg.GetExchangeConfig("Liqui")
if err != nil {
t.Error("Test Failed - liqui Setup() init error")

View File

@@ -20,7 +20,7 @@ func TestSetDefaults(t *testing.T) {
func TestSetup(t *testing.T) {
wexConfig := config.GetConfig()
wexConfig.LoadConfig("../../testdata/configtest.dat")
wexConfig.LoadConfig("../../testdata/configtest.json")
conf, err := wexConfig.GetExchangeConfig("WEX")
if err != nil {
t.Error("Test Failed - WEX init error")

View File

@@ -62,6 +62,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -84,6 +85,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -104,6 +106,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -125,6 +128,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -147,6 +151,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -167,6 +172,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -189,6 +195,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -209,6 +216,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -231,6 +239,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -251,6 +260,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -271,6 +281,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -292,6 +303,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -313,6 +325,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -333,6 +346,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -356,6 +370,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -376,6 +391,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -397,6 +413,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
@@ -418,6 +435,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",

View File

@@ -20,8 +20,9 @@ func main() {
var inFile, outFile, key string
var encrypt bool
var err error
flag.StringVar(&inFile, "infile", "config.dat", "The config input file to process.")
flag.StringVar(&outFile, "outfile", "config.dat.out", "The config output file.")
configFile := config.GetFilePath("")
flag.StringVar(&inFile, "infile", configFile, "The config input file to process.")
flag.StringVar(&outFile, "outfile", configFile+".out", "The config output file.")
flag.BoolVar(&encrypt, "encrypt", true, "Wether to encrypt or decrypt.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()

View File

@@ -57,7 +57,7 @@ func getOnlineOfflinePortfolio(coins []portfolio.Coin, online bool) {
func main() {
var inFile, key string
flag.StringVar(&inFile, "infile", "config.dat", "The config input file to process.")
flag.StringVar(&inFile, "infile", config.GetFilePath(""), "The config input file to process.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()