Skip to content

Commit

Permalink
Bugfix: Introduces a new config.DefaultFilePath func (thrasher-corp#415)
Browse files Browse the repository at this point in the history
* Introduces a new config.DefaultFilePath func

* FiX GrAmMeRiNo
  • Loading branch information
thrasher- authored Jan 15, 2020
1 parent 838b378 commit 7a90aec
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 51 deletions.
18 changes: 6 additions & 12 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,19 @@ func EncryptOrDecrypt(encrypt bool) string {
func main() {
var inFile, outFile, key string
var encrypt bool
var err error

configFile, err := config.GetFilePath("")
if err != nil {
log.Fatal(err)
}

flag.StringVar(&inFile, "infile", configFile, "The config input file to process.")
flag.StringVar(&outFile, "outfile", configFile+".out", "The config output file.")
defaultCfgFile := config.DefaultFilePath()
flag.StringVar(&inFile, "infile", defaultCfgFile, "The config input file to process.")
flag.StringVar(&outFile, "outfile", defaultCfgFile+".out", "The config output file.")
flag.BoolVar(&encrypt, "encrypt", true, "Whether to encrypt or decrypt.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()

log.Println("GoCryptoTrader: config-helper tool.")

if key == "" {
result, errf := config.PromptForConfigKey(false)
if errf != nil {
log.Fatal("Unable to obtain encryption/decryption key.")
result, err := config.PromptForConfigKey(false)
if err != nil {
log.Fatalf("Unable to obtain encryption/decryption key: %s", err)
}
key = string(result)
}
Expand Down
15 changes: 4 additions & 11 deletions cmd/dbmigrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,16 @@ func main() {
fmt.Println(core.Copyright)
fmt.Println()

defaultPath, err := config.GetFilePath("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

flag.StringVar(&command, "command", "", "command to run status|up|up-by-one|up-to|down|create")
flag.StringVar(&args, "args", "", "arguments to pass to goose")

flag.StringVar(&configFile, "config", defaultPath, "config file to load")
flag.StringVar(&configFile, "config", config.DefaultFilePath(), "config file to load")
flag.StringVar(&defaultDataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
flag.StringVar(&migrationDir, "migrationdir", database.MigrationDir, "override migration folder")

flag.Parse()

conf := config.GetConfig()

err = conf.LoadConfig(configFile, true)
var conf config.Config
err := conf.LoadConfig(configFile, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -75,6 +67,7 @@ func main() {
fmt.Println("Database support is disabled")
os.Exit(1)
}

err = openDbConnection(conf.Database.Driver)
if err != nil {
fmt.Println(err)
Expand Down
7 changes: 1 addition & 6 deletions cmd/gen_otp/otp_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ func main() {
var single bool
var err error

defaultCfg, err := config.GetFilePath("")
if err != nil {
log.Fatal(err)
}

flag.StringVar(&cfgFile, "config", defaultCfg, "The config input file to process.")
flag.StringVar(&cfgFile, "config", config.DefaultFilePath(), "The config input file to process.")
flag.BoolVar(&single, "single", false, "prompt for single use OTP code gen")
flag.Parse()

Expand Down
15 changes: 4 additions & 11 deletions cmd/gen_sqlboiler_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,19 @@ func main() {
fmt.Println(core.Copyright)
fmt.Println()

defaultPath, err := config.GetFilePath("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

flag.StringVar(&configFile, "config", defaultPath, "config file to load")
flag.StringVar(&configFile, "config", config.DefaultFilePath(), "config file to load")
flag.StringVar(&defaultDataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
flag.StringVar(&outputFolder, "outdir", "", "overwrite default output folder")
flag.Parse()

conf := config.GetConfig()

err = conf.LoadConfig(configFile, true)
var cfg config.Config
err := cfg.LoadConfig(configFile, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

convertGCTtoSQLBoilerConfig(&conf.Database)
convertGCTtoSQLBoilerConfig(&cfg.Database)

jsonOutput, err := json.MarshalIndent(sqlboilerConfig, "", " ")
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions cmd/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,14 @@ func getOnlineOfflinePortfolio(coins []portfolio.Coin, online bool) {

func main() {
var inFile, key string

defaultCfg, err := config.GetFilePath("")
if err != nil {
log.Println(err)
os.Exit(1)
}

flag.StringVar(&inFile, "infile", defaultCfg, "The config input file to process.")
flag.StringVar(&inFile, "infile", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()

log.Println("GoCryptoTrader: portfolio tool.")

var cfg config.Config
err = cfg.LoadConfig(inFile, true)
err := cfg.LoadConfig(inFile, true)
if err != nil {
log.Println(err)
os.Exit(1)
Expand Down
28 changes: 27 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,8 +1328,34 @@ func (c *Config) CheckConnectionMonitorConfig() {
}
}

// DefaultFilePath returns the default config file path
// MacOS/Linux: $HOME/.gocryptotrader/config.json or config.dat
// Windows: %APPDATA%\GoCryptoTrader\config.json or config.dat
// Helpful for printing application usage
func DefaultFilePath() string {
f := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), File)
_, err := os.Stat(f)
if os.IsNotExist(err) {
encFile := filepath.Join(common.GetDefaultDataDir(runtime.GOOS), EncryptedFile)
_, err = os.Stat(encFile)
if !os.IsNotExist(err) {
return encFile
}
}
return f
}

// GetFilePath returns the desired config file or the default config file name
// based on if the application is being run under test or normal mode.
// based on if the application is being run under test or normal mode. It will
// also move/rename the config file under the following conditions:
// 1) If a config file is found in the executable path directory and no explicit
// config path is set, plus no config is found in the GCT data dir, it will
// move it to the GCT data dir. If a config already exists in the GCT data
// dir, it will warn the user and load the config found in the GCT data dir
// 2) If a config file in the GCT data dir has the file extension .dat but
// contains json data, it will rename to the file to config.json
// 3) If a config file in the GCT data dir has the file extension .json but
// contains encrypted data, it will rename the file to config.dat
func GetFilePath(configfile string) (string, error) {
if configfile != "" {
return configfile, nil
Expand Down
14 changes: 14 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,20 @@ func TestCheckConnectionMonitorConfig(t *testing.T) {
}
}

func TestDefaultFilePath(t *testing.T) {
// This is tricky to test because we're dealing with a config file stored
// in a persons default directory and to properly test it, it would
// require causing os.Stat to return !os.IsNotExist and os.IsNotExist (which
// means moving a users config file around), a way of getting around this is
// to pass the datadir as a param line but adds a burden to everyone who
// uses it
result := DefaultFilePath()
if !strings.Contains(result, File) &&
!strings.Contains(result, EncryptedFile) {
t.Error("result should have contained config.json or config.dat")
}
}

func TestGetFilePath(t *testing.T) {
expected := "blah.json"
result, _ := GetFilePath("blah.json")
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/engine"
Expand All @@ -22,7 +23,7 @@ func main() {
versionFlag := flag.Bool("version", false, "retrieves current GoCryptoTrader version")

// Core settings
flag.StringVar(&settings.ConfigFile, "config", "", "config file to load")
flag.StringVar(&settings.ConfigFile, "config", config.DefaultFilePath(), "config file to load")
flag.StringVar(&settings.DataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
flag.IntVar(&settings.GoMaxProcs, "gomaxprocs", runtime.GOMAXPROCS(-1), "sets the runtime GOMAXPROCS value")
flag.BoolVar(&settings.EnableDryRun, "dryrun", false, "dry runs bot, doesn't save config file")
Expand Down

0 comments on commit 7a90aec

Please sign in to comment.