From 4d55047def2daf6ac01b675996b01b2f994b9a38 Mon Sep 17 00:00:00 2001 From: Julian Langschaedel Date: Fri, 9 Feb 2018 21:59:54 +0100 Subject: [PATCH] fix osx bundle path for orderbooks.db when running gdax-bookmap.app we now use ~/Library/Application Support/gdax-bookmap/orderbooks.db as boltdb file --- README.md | 15 +++++++++++++++ main.go | 11 +++++++++-- util/util.go | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 817aea0..23355a4 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,21 @@ cd $GOPATH/src/github.com/lian/gdax-bookmap ./script/build.sh ``` +## command flags +``` +Usage of gdax-bookmap: + -base string + active BaseCurrency (default "BTC") + -db string + database file (default "orderbooks.db") + -h int + window height + -platforms string + active platforms (default "gdax-bitstamp-binance") + -w int + window width +``` + ## current controls ``` diff --git a/main.go b/main.go index bbb6aa4..16ac1ed 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "os" "runtime" "strings" "time" @@ -205,7 +206,8 @@ func main() { var windowHeight int fmt.Printf("Starting gdax-bookmap %s-%s\n", AppVersion, AppGitHash) - flag.StringVar(&ActivePlatform, "platforms", "gdax-bitstamp-binance-bitfinex", "active platforms") + //flag.StringVar(&ActivePlatform, "platforms", "gdax-bitstamp-binance-bitfinex", "active platforms") + flag.StringVar(&ActivePlatform, "platforms", "gdax-bitstamp-binance", "active platforms") flag.StringVar(&ActiveBase, "base", "BTC", "active BaseCurrency") flag.StringVar(&db_path, "db", "orderbooks.db", "database file") flag.IntVar(&windowWidth, "w", 0, "window width") @@ -214,7 +216,12 @@ func main() { //runpprof() - db := util.OpenDB(db_path, []string{}, false) + db, err := util.OpenDB(db_path, []string{}, false) + if err != nil { + fmt.Println("OpenDB Error", err) + os.Exit(0) + } + infos = make([]*product_info.Info, 0) if strings.Contains(strings.ToLower(ActivePlatform), "gdax") { diff --git a/util/util.go b/util/util.go index 86dce4e..c91830f 100644 --- a/util/util.go +++ b/util/util.go @@ -2,24 +2,35 @@ package util import ( "fmt" - "log" + "os" + "os/user" + "path/filepath" "strconv" "strings" "github.com/boltdb/bolt" ) -func OpenDB(path string, buckets []string, readOnly bool) *bolt.DB { +func OpenDB(path string, buckets []string, readOnly bool) (*bolt.DB, error) { db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: readOnly}) if err != nil { - log.Fatal(err) + + path, err = osxBundlePath(path) + if err != nil { + return nil, err + } + + db, err = bolt.Open(path, 0600, &bolt.Options{ReadOnly: readOnly}) + if err != nil { + return nil, err + } } if len(buckets) > 0 { CreateBucketsDB(db, buckets) } - return db + return db, nil } func CreateBucketsDB(db *bolt.DB, buckets []string) { @@ -34,6 +45,27 @@ func CreateBucketsDB(db *bolt.DB, buckets []string) { }) } +func osxBundlePath(db_path string) (string, error) { + usr, err := user.Current() + if err != nil { + return "", err + } + + path := filepath.Join(usr.HomeDir, "Library/Application Support") + if _, err := os.Stat(path); os.IsNotExist(err) { + return "", err + } + + path = filepath.Join(path, "gdax-bookmap") + if _, err := os.Stat(path); os.IsNotExist(err) { + if err = os.MkdirAll(path, 0755); err != nil { + return "", err + } + } + + return filepath.Join(path, db_path), nil +} + func NumDecPlaces(v float64) int { s := strconv.FormatFloat(v, 'f', -1, 64) i := strings.IndexByte(s, '.')