Remove fatal logs

pull/49/head
Miguel Mota 4 years ago
parent a000c96624
commit 124812ff46

@ -1,9 +1,6 @@
package cmd package cmd
import ( import (
"log"
"os"
"github.com/miguelmota/cointop/cointop" "github.com/miguelmota/cointop/cointop"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -150,8 +147,7 @@ For more information, visit: https://github.com/miguelmota/cointop`,
rootCmd.AddCommand(versionCmd, cleanCmd, resetCmd, priceCmd, testCmd) rootCmd.AddCommand(versionCmd, cleanCmd, resetCmd, priceCmd, testCmd)
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) panic(err)
os.Exit(1)
} }
} }
@ -159,8 +155,9 @@ func doTest() {
ct, err := cointop.NewCointop(&cointop.Config{ ct, err := cointop.NewCointop(&cointop.Config{
NoPrompts: true, NoPrompts: true,
}) })
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
ct.Exit() ct.Exit()

@ -252,11 +252,17 @@ func NewCointop(config *Config) (*Cointop, error) {
apiKey := os.Getenv("CMC_PRO_API_KEY") apiKey := os.Getenv("CMC_PRO_API_KEY")
if apiKey == "" { if apiKey == "" {
if !config.NoPrompts { 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 { } else {
ct.apiKeys.cmc = apiKey ct.apiKeys.cmc = apiKey
} }
if err := ct.saveConfig(); err != nil { if err := ct.saveConfig(); err != nil {
return nil, err return nil, err
} }

@ -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 return nil
} }

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"time" "time"
log "github.com/sirupsen/logrus"
) )
// debuglog writs a debug log to stdout // debuglog writs a debug log to stdout
@ -17,13 +15,13 @@ func (ct *Cointop) debuglog(msg string) {
filename := "/tmp/cointop.log" filename := "/tmp/cointop.log"
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
defer f.Close() defer f.Close()
text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg) text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg)
if _, err = f.WriteString(text); err != nil { if _, err = f.WriteString(text); err != nil {
log.Fatal(err) panic(err)
} }
} }

@ -8,7 +8,6 @@ import (
"strings" "strings"
"github.com/miguelmota/cointop/cointop/common/pad" "github.com/miguelmota/cointop/cointop/common/pad"
log "github.com/sirupsen/logrus"
) )
// PortfolioUpdateMenuView is structure for portfolio update menu view // 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 { if shouldDelete {
ct.removePortfolioEntry(coin.Name) ct.removePortfolioEntry(coin.Name)
@ -193,7 +194,7 @@ func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) {
return p, isNew return p, isNew
} }
func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) { func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) error {
ct.debuglog("setPortfolioEntry()") ct.debuglog("setPortfolioEntry()")
ic, _ := ct.State.allCoinsSlugMap.Load(strings.ToLower(coin)) ic, _ := ct.State.allCoinsSlugMap.Load(strings.ToLower(coin))
c, _ := ic.(*Coin) c, _ := ic.(*Coin)
@ -209,8 +210,10 @@ func (ct *Cointop) setPortfolioEntry(coin string, holdings float64) {
} }
if err := ct.Save(); err != nil { if err := ct.Save(); err != nil {
log.Fatal(err) return err
} }
return nil
} }
func (ct *Cointop) removePortfolioEntry(coin string) { func (ct *Cointop) removePortfolioEntry(coin string) {

@ -1,13 +1,11 @@
package cointop package cointop
import "log"
// Save saves the cointop settings to the config file // Save saves the cointop settings to the config file
func (ct *Cointop) Save() error { func (ct *Cointop) Save() error {
ct.debuglog("Save()") ct.debuglog("Save()")
ct.SetSavingStatus() ct.SetSavingStatus()
if err := ct.saveConfig(); err != nil { if err := ct.saveConfig(); err != nil {
log.Fatal(err) return err
} }
ct.CacheAllCoinsSlugMap() ct.CacheAllCoinsSlugMap()

@ -5,19 +5,17 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
log "github.com/sirupsen/logrus"
) )
// ReadAPIKeyFromStdin reads the user inputed API from the stdin prompt // 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()") ct.debuglog("ReadAPIKeyFromStdin()")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Printf("Enter %s API Key: ", name) fmt.Printf("Enter %s API Key: ", name)
text, err := reader.ReadString('\n') text, err := reader.ReadString('\n')
if err != nil { if err != nil {
log.Fatal(err) return "", err
} }
return strings.TrimSpace(text) return strings.TrimSpace(text), nil
} }

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/miguelmota/gocui" "github.com/miguelmota/gocui"
log "github.com/sirupsen/logrus"
) )
// Update takes a callback which updates the view // Update takes a callback which updates the view
@ -12,11 +11,12 @@ func (ct *Cointop) Update(f func()) {
ct.debuglog(fmt.Sprintf("Update()")) ct.debuglog(fmt.Sprintf("Update()"))
if ct.g == nil { if ct.g == nil {
log.Fatal("gocui is not initialized") panic("gocui is not initialized")
} }
ct.g.Update(func(g *gocui.Gui) error { ct.g.Update(func(g *gocui.Gui) error {
f() f()
return nil return nil
}) })
} }

Loading…
Cancel
Save