From 124812ff46b2c863d88a662e9e26024af5d022a7 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sun, 29 Dec 2019 02:15:14 -0800 Subject: [PATCH] Remove fatal logs --- cmd/cointop.go | 9 +++------ cointop/cointop.go | 8 +++++++- cointop/config.go | 5 ++++- cointop/debug.go | 6 ++---- cointop/portfolio.go | 11 +++++++---- cointop/save.go | 4 +--- cointop/stdin.go | 8 +++----- cointop/update.go | 4 ++-- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/cmd/cointop.go b/cmd/cointop.go index 550c26a..7158931 100644 --- a/cmd/cointop.go +++ b/cmd/cointop.go @@ -1,9 +1,6 @@ package cmd import ( - "log" - "os" - "github.com/miguelmota/cointop/cointop" "github.com/spf13/cobra" ) @@ -150,8 +147,7 @@ For more information, visit: https://github.com/miguelmota/cointop`, rootCmd.AddCommand(versionCmd, cleanCmd, resetCmd, priceCmd, testCmd) if err := rootCmd.Execute(); err != nil { - log.Fatal(err) - os.Exit(1) + panic(err) } } @@ -159,8 +155,9 @@ func doTest() { ct, err := cointop.NewCointop(&cointop.Config{ NoPrompts: true, }) + if err != nil { - log.Fatal(err) + panic(err) } ct.Exit() diff --git a/cointop/cointop.go b/cointop/cointop.go index e3500f4..acbc706 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -252,11 +252,17 @@ func NewCointop(config *Config) (*Cointop, error) { apiKey := os.Getenv("CMC_PRO_API_KEY") if apiKey == "" { if !config.NoPrompts { - ct.apiKeys.cmc = ct.ReadAPIKeyFromStdin("CoinMarketCap Pro") + apiKey, err = ct.ReadAPIKeyFromStdin("CoinMarketCap Pro") + if err != nil { + return nil, err + } + + ct.apiKeys.cmc = apiKey } } else { ct.apiKeys.cmc = apiKey } + if err := ct.saveConfig(); err != nil { return nil, err } diff --git a/cointop/config.go b/cointop/config.go index b9fc65f..f7495f4 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -373,7 +373,10 @@ func (ct *Cointop) loadPortfolioFromConfig() error { } } - ct.setPortfolioEntry(name, holdings) + if err := ct.setPortfolioEntry(name, holdings); err != nil { + return err + } } + return nil } diff --git a/cointop/debug.go b/cointop/debug.go index c42d004..d655403 100644 --- a/cointop/debug.go +++ b/cointop/debug.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "time" - - log "github.com/sirupsen/logrus" ) // debuglog writs a debug log to stdout @@ -17,13 +15,13 @@ func (ct *Cointop) debuglog(msg string) { filename := "/tmp/cointop.log" f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { - log.Fatal(err) + panic(err) } defer f.Close() text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg) if _, err = f.WriteString(text); err != nil { - log.Fatal(err) + panic(err) } } diff --git a/cointop/portfolio.go b/cointop/portfolio.go index 27b4afa..2430daf 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/miguelmota/cointop/cointop/common/pad" - log "github.com/sirupsen/logrus" ) // PortfolioUpdateMenuView is structure for portfolio update menu view @@ -154,7 +153,9 @@ func (ct *Cointop) setPortfolioHoldings() error { } } - ct.setPortfolioEntry(coin.Name, holdings) + if err := ct.setPortfolioEntry(coin.Name, holdings); err != nil { + return err + } if shouldDelete { ct.removePortfolioEntry(coin.Name) @@ -193,7 +194,7 @@ func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) { return p, isNew } -func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) { +func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) error { ct.debuglog("setPortfolioEntry()") ic, _ := ct.State.allCoinsSlugMap.Load(strings.ToLower(coin)) c, _ := ic.(*Coin) @@ -209,8 +210,10 @@ func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) { } if err := ct.Save(); err != nil { - log.Fatal(err) + return err } + + return nil } func (ct *Cointop) removePortfolioEntry(coin string) { diff --git a/cointop/save.go b/cointop/save.go index d3e7d08..fb17ba1 100644 --- a/cointop/save.go +++ b/cointop/save.go @@ -1,13 +1,11 @@ package cointop -import "log" - // Save saves the cointop settings to the config file func (ct *Cointop) Save() error { ct.debuglog("Save()") ct.SetSavingStatus() if err := ct.saveConfig(); err != nil { - log.Fatal(err) + return err } ct.CacheAllCoinsSlugMap() diff --git a/cointop/stdin.go b/cointop/stdin.go index 2c64a20..714f938 100644 --- a/cointop/stdin.go +++ b/cointop/stdin.go @@ -5,19 +5,17 @@ import ( "fmt" "os" "strings" - - log "github.com/sirupsen/logrus" ) // ReadAPIKeyFromStdin reads the user inputed API from the stdin prompt -func (ct *Cointop) ReadAPIKeyFromStdin(name string) string { +func (ct *Cointop) ReadAPIKeyFromStdin(name string) (string, error) { ct.debuglog("ReadAPIKeyFromStdin()") reader := bufio.NewReader(os.Stdin) fmt.Printf("Enter %s API Key: ", name) text, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) + return "", err } - return strings.TrimSpace(text) + return strings.TrimSpace(text), nil } diff --git a/cointop/update.go b/cointop/update.go index bc58188..a5183fb 100644 --- a/cointop/update.go +++ b/cointop/update.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/miguelmota/gocui" - log "github.com/sirupsen/logrus" ) // Update takes a callback which updates the view @@ -12,11 +11,12 @@ func (ct *Cointop) Update(f func()) { ct.debuglog(fmt.Sprintf("Update()")) if ct.g == nil { - log.Fatal("gocui is not initialized") + panic("gocui is not initialized") } ct.g.Update(func(g *gocui.Gui) error { f() + return nil }) }