From 70748c8e665e2608cdf88af91a180bcfd97dfcb1 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sat, 12 May 2018 18:47:06 -0700 Subject: [PATCH] cmc ping --- README.md | 8 +++++++ cointop/cointop.go | 26 +++++++++++++-------- main.go | 23 ++++++------------ pkg/api/impl/coinmarketcap/coinmarketcap.go | 15 ++++++++++++ pkg/api/interface.go | 1 + 5 files changed, 47 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 98abc9e..d17172c 100644 --- a/README.md +++ b/README.md @@ -503,6 +503,14 @@ Action|Description - A: Press q to quit the open view/window. +- Q: I'm getting the error `open /dev/tty: no such device or address`. + + -A: Usually this error occurs when cointop is running as a daemon or slave which means that there is no terminal allocated, so `/dev/tty` doesn't exist for that process. Try running it with the following environment variables: + + ```bash + DEV_IN=/dev/stdout DEV_OUT=/dev/stdin cointop + ``` + ## Development ### Go diff --git a/cointop/cointop.go b/cointop/cointop.go index 02afc13..47681e3 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -69,16 +69,13 @@ type Cointop struct { convertmenuvisible bool } -// Instance running cointop instance -var Instance *Cointop - -// Run runs cointop -func Run() { +// New initializes cointop +func New() *Cointop { var debug bool if os.Getenv("DEBUG") != "" { debug = true } - ct := Cointop{ + ct := &Cointop{ api: api.NewCMC(), refreshticker: time.NewTicker(1 * time.Minute), sortby: "rank", @@ -142,7 +139,6 @@ func Run() { convertmenuviewname: "convertmenu", currencyconversion: "USD", } - Instance = &ct err := ct.setupConfig() if err != nil { log.Fatal(err) @@ -162,7 +158,15 @@ func Run() { marketcachekey := "market" fcache.Get(marketcachekey, &market) ct.cache.Set(marketcachekey, market, 10*time.Second) + err = ct.api.Ping() + if err != nil { + log.Fatal(err) + } + return ct +} +// Run runs cointop +func (ct *Cointop) Run() { g, err := gocui.NewGui(gocui.Output256) if err != nil { log.Fatalf("new gocui: %v", err) @@ -195,8 +199,10 @@ func (ct *Cointop) quitView() error { } // Exit safely exit application -func Exit() { - if Instance != nil { - Instance.g.Close() +func (ct *Cointop) Exit() { + if ct.g != nil { + ct.g.Close() + } else { + os.Exit(0) } } diff --git a/main.go b/main.go index 5823b93..0c398a5 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "time" "github.com/miguelmota/cointop/cointop" ) @@ -11,27 +10,19 @@ import ( var version = "1.0.1" func main() { - var ver bool + var ver, test bool flag.BoolVar(&ver, "v", false, "Version") - var test bool flag.BoolVar(&test, "test", false, "Run test") flag.Parse() if ver { fmt.Println(version) - return + } else if test { + doTest() + } else { + cointop.New().Run() } - if test { - runTest() - return - } - - cointop.Run() } -func runTest() { - go func() { - cointop.Run() - }() - time.Sleep(1 * time.Second) - cointop.Exit() +func doTest() { + cointop.New().Exit() } diff --git a/pkg/api/impl/coinmarketcap/coinmarketcap.go b/pkg/api/impl/coinmarketcap/coinmarketcap.go index 1f342b4..b226c64 100644 --- a/pkg/api/impl/coinmarketcap/coinmarketcap.go +++ b/pkg/api/impl/coinmarketcap/coinmarketcap.go @@ -1,6 +1,7 @@ package coinmarketcap import ( + "errors" "fmt" "strconv" "strings" @@ -19,6 +20,20 @@ func New() *Service { return &Service{} } +// Ping ping API +func (s *Service) Ping() error { + ticker, err := cmc.Ticker(&cmc.TickerOptions{ + Symbol: "ETH", + }) + if err != nil { + return errors.New("failed to ping") + } + if ticker == nil { + return errors.New("failed to ping") + } + return nil +} + func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, error) { ret := make(map[string]apitypes.Coin) max := 100 diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 6cd8fe6..199ead8 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -6,6 +6,7 @@ import ( // Interface interface type Interface interface { + Ping() error GetAllCoinData(convert string) (map[string]types.Coin, error) GetCoinGraphData(coin string, start int64, end int64) (types.CoinGraph, error) GetGlobalMarketGraphData(start int64, end int64) (types.MarketGraph, error)