Skip to content

Commit

Permalink
fix osx bundle path for orderbooks.db
Browse files Browse the repository at this point in the history
when running gdax-bookmap.app we now use ~/Library/Application Support/gdax-bookmap/orderbooks.db as boltdb file
  • Loading branch information
lian committed Feb 9, 2018
1 parent 449fb54 commit 4d55047
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -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")
Expand All @@ -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") {
Expand Down
40 changes: 36 additions & 4 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, '.')
Expand Down

0 comments on commit 4d55047

Please sign in to comment.