Remove fatal logs

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

@ -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()

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

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

@ -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)
}
}

@ -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) {

@ -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()

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

@ -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
})
}

Loading…
Cancel
Save