Add debug logger and default data directory support

This commit is contained in:
Adrian Gallagher
2018-10-19 17:26:00 +11:00
parent 415332b446
commit c2c7032858
4 changed files with 145 additions and 26 deletions

View File

@@ -21,6 +21,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
@@ -607,3 +608,33 @@ func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}
// GetDefaultDataDir returns the default data directory
// Windows - C:\Users\%USER%\AppData\Roaming\GoCryptoTrader
// Linux/Unix or OSX - $HOME/.gocryptotrader
func GetDefaultDataDir(env string) string {
if env == "windows" {
return os.Getenv("APPDATA") + GetOSPathSlash() + "GoCryptoTrader"
}
return path.Join(os.ExpandEnv("$HOME"), ".gocryptotrader")
}
// CheckDir checks to see if a particular directory exists
// and attempts to create it if desired, if it doesn't exist
func CheckDir(dir string, create bool) error {
_, err := os.Stat(dir)
if !os.IsNotExist(err) {
return nil
}
if !create {
return fmt.Errorf("directory %s does not exist. Err: %s", dir, err)
}
log.Printf("Directory %s does not exist.. creating.", dir)
err = os.Mkdir(dir, 0777)
if err != nil {
return fmt.Errorf("failed to create dir. Err: %s", err)
}
return nil
}