Skip to content

Commit

Permalink
Use config.json by default if config file isn't encrypted
Browse files Browse the repository at this point in the history
Fixes issue: https://github.com/thrasher-/gocryptotrader/issues/48
Allows for auto syntax highlighing in text editors/IDEs
  • Loading branch information
thrasher- committed Nov 14, 2017
1 parent 4799c73 commit ce908ee
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 25 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand Down Expand Up @@ -317,34 +316,37 @@ func GetFilePath(file string) string {
return file
}
if flag.Lookup("test.v") == nil {
return ConfigFile
}
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)
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 {
return err
log.Fatalf("Unable to rename config file: %s", err)
}
log.Printf(RenamingConfigFile+"\n", OldConfigFile, ConfigFile)
log.Printf("Renaming encrypted config file from %s to %s", ConfigFile,
EncryptedConfigFile)
return EncryptedConfigFile
}
return nil
return ConfigTestFile
}

// 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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion exchanges/anx/anx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bitstamp/bitstamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bittrex/bittrex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/btcc/btcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/btcmarkets/btcmarkets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/gdax/gdax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/gemini/gemini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion exchanges/itbit/itbit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion exchanges/liqui/liqui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exchanges/wex/wex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions testdata/configtest.dat → testdata/configtest.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -84,6 +85,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -104,6 +106,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -125,6 +128,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -147,6 +151,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -167,6 +172,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -189,6 +195,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -209,6 +216,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -231,6 +239,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -251,6 +260,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -271,6 +281,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -292,6 +303,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -313,6 +325,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -333,6 +346,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -356,6 +370,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -376,6 +391,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -397,6 +413,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand All @@ -418,6 +435,7 @@
"Enabled": true,
"Verbose": false,
"Websocket": false,
"UseSandbox": false,
"RESTPollingDelay": 10,
"AuthenticatedAPISupport": false,
"APIKey": "Key",
Expand Down
5 changes: 3 additions & 2 deletions tools/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tools/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit ce908ee

Please sign in to comment.