diff --git a/cointop/cmd/holdings.go b/cointop/cmd/holdings.go index f9e214d..bdc90aa 100644 --- a/cointop/cmd/holdings.go +++ b/cointop/cmd/holdings.go @@ -18,6 +18,7 @@ func HoldingsCmd() *cobra.Command { var format string = "table" var humanReadable bool var filter []string + var convert string holdingsCmd := &cobra.Command{ Use: "holdings", @@ -41,6 +42,7 @@ func HoldingsCmd() *cobra.Command { HumanReadable: humanReadable, Format: format, Filter: filter, + Convert: convert, }) } @@ -50,6 +52,7 @@ func HoldingsCmd() *cobra.Command { HumanReadable: humanReadable, Format: format, Filter: filter, + Convert: convert, }) }, } @@ -62,7 +65,8 @@ func HoldingsCmd() *cobra.Command { holdingsCmd.Flags().StringVarP(&sortBy, "sort-by", "s", sortBy, `Sort by column. Options are "name", "symbol", "price", "holdings", "balance", "24h"`) holdingsCmd.Flags().BoolVarP(&sortDesc, "sort-desc", "d", sortDesc, "Sort in descending order") holdingsCmd.Flags().StringVarP(&format, "format", "", format, `Ouput format. Options are "table", "csv", "json"`) - holdingsCmd.Flags().StringSliceVarP(&filter, "filter", "f", filter, `Filter portfolio entries by coin name or symbol, comma separated. Example: "btc,eth,doge"`) + holdingsCmd.Flags().StringSliceVarP(&filter, "filter", "", filter, `Filter portfolio entries by coin name or symbol, comma separated. Example: "btc,eth,doge"`) + holdingsCmd.Flags().StringVarP(&convert, "convert", "f", convert, "The currency to convert to") return holdingsCmd } diff --git a/cointop/conversion.go b/cointop/conversion.go index c9b1f12..67f0d74 100644 --- a/cointop/conversion.go +++ b/cointop/conversion.go @@ -1,6 +1,7 @@ package cointop import ( + "errors" "fmt" "sort" "strings" @@ -113,6 +114,13 @@ func NewConvertMenuView() *ConvertMenuView { return &ConvertMenuView{NewView("convertmenu")} } +// IsSupportedCurrencyConversion returns true if it's a supported currency conversion +func (ct *Cointop) IsSupportedCurrencyConversion(convert string) bool { + conversions := ct.SupportedCurrencyConversions() + _, ok := conversions[convert] + return ok +} + // SupportedCurrencyConversions returns a map of all supported currencies for conversion func (ct *Cointop) SupportedCurrencyConversions() map[string]string { all := map[string]string{} @@ -208,19 +216,36 @@ func (ct *Cointop) UpdateConvertMenu() { }) } +// SetCurrencyConverstion sets the currency conversion +func (ct *Cointop) SetCurrencyConverstion(convert string) error { + convert = strings.ToUpper(convert) + if convert == "" { + return nil + } + + if !ct.IsSupportedCurrencyConversion(convert) { + return errors.New("unsupported currency conversion") + } + + // NOTE: return if the currency selection wasn't changed + if ct.State.currencyConversion == convert { + return nil + } + + ct.State.currencyConversion = convert + return nil +} + // SetCurrencyConverstionFn sets the currency conversion function func (ct *Cointop) SetCurrencyConverstionFn(convert string) func() error { ct.debuglog("setCurrencyConverstionFn()") return func() error { ct.HideConvertMenu() - // NOTE: return if the currency selection wasn't changed - if ct.State.currencyConversion == convert { - return nil + if err := ct.SetCurrencyConverstion(convert); err != nil { + return err } - ct.State.currencyConversion = convert - if err := ct.Save(); err != nil { return err } diff --git a/cointop/portfolio.go b/cointop/portfolio.go index 7bb612a..410fd66 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -324,6 +324,7 @@ type TablePrintOptions struct { HumanReadable bool Format string Filter []string + Convert string } // outputFormats is list of valid output formats @@ -350,6 +351,10 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error { options = &TablePrintOptions{} } + if err := ct.SetCurrencyConverstion(options.Convert); err != nil { + return err + } + ct.RefreshPortfolioCoins() sortBy := options.SortBy @@ -480,6 +485,10 @@ func (ct *Cointop) PrintTotalHoldings(options *TablePrintOptions) error { options = &TablePrintOptions{} } + if err := ct.SetCurrencyConverstion(options.Convert); err != nil { + return err + } + ct.RefreshPortfolioCoins() humanReadable := options.HumanReadable