Support multiple coins for price command

pull/94/head
Miguel Mota 3 years ago
parent 00d654aa74
commit 99bbeadc2d

@ -44,6 +44,9 @@ build-multiple: clean
install: build
sudo mv bin/cointop /usr/local/bin
uninstall:
sudo rm /usr/local/bin/cointop
clean-mac:
go clean && \
rm -rf bin/mac

@ -8,7 +8,7 @@ import (
// PriceCmd ...
func PriceCmd() *cobra.Command {
var apiChoice string
var coin string
var coins []string
var currency string
priceCmd := &cobra.Command{
@ -16,15 +16,15 @@ func PriceCmd() *cobra.Command {
Short: "Displays the current price of a coin",
Long: `The price command display the current price of a coin`,
RunE: func(cmd *cobra.Command, args []string) error {
return cointop.PrintPrice(&cointop.PriceConfig{
Coin: coin,
return cointop.PrintPrices(&cointop.PricesConfig{
Coins: coins,
Currency: currency,
APIChoice: apiChoice,
})
},
}
priceCmd.Flags().StringVarP(&coin, "coin", "c", "bitcoin", "Full name of the coin")
priceCmd.Flags().StringSliceVarP(&coins, "coins", "c", nil, "Name or symbol of coin(s), comma separated. E.g. \"Bitcoin\" Eg. \"btc,eth,doge\"")
priceCmd.Flags().StringVarP(&currency, "currency", "f", "USD", "The currency to convert to")
priceCmd.Flags().StringVarP(&apiChoice, "api", "a", cointop.CoinGecko, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")

@ -1,7 +1,6 @@
package cointop
import (
"errors"
"fmt"
"io/ioutil"
"os"
@ -21,9 +20,6 @@ import (
// TODO: clean up and optimize codebase
// ErrInvalidAPIChoice is error for invalid API choice
var ErrInvalidAPIChoice = errors.New("invalid API choice")
// Views are all views in cointop
type Views struct {
Chart *ChartView

@ -0,0 +1,9 @@
package cointop
import "errors"
// ErrInvalidAPIChoice is error for invalid API choice
var ErrInvalidAPIChoice = errors.New("invalid API choice")
// ErrCoinNameOrSymbolRequired is error for when coin name or symbol is required
var ErrCoinNameOrSymbolRequired = errors.New("coin name or symbol is required")

@ -2,37 +2,76 @@ package cointop
import (
"fmt"
"strings"
"github.com/miguelmota/cointop/pkg/api"
"github.com/miguelmota/cointop/pkg/humanize"
)
// PriceConfig is the config options for the price command
// PriceConfig is the config options for the coin price method
type PriceConfig struct {
Coin string
Currency string
APIChoice string
}
// PricesConfig is the config options for the coin prices method
type PricesConfig struct {
Coins []string
Currency string
APIChoice string
}
// PrintPrices outputs the current price of the coins
func PrintPrices(config *PricesConfig) error {
prices, err := GetCoinPrices(config)
if err != nil {
return err
}
fmt.Println(strings.Join(prices, "\n"))
return nil
}
// PrintPrice outputs the current price of the coin
func PrintPrice(config *PriceConfig) error {
prices, err := GetCoinPrices(&PricesConfig{
Coins: []string{config.Coin},
Currency: config.Currency,
APIChoice: config.APIChoice,
})
if err != nil {
return err
}
fmt.Println(prices[0])
return nil
}
// GetCoinPrices returns the current price of the specified coins
func GetCoinPrices(config *PricesConfig) ([]string, error) {
if len(config.Coins) == 0 {
return nil, ErrCoinNameOrSymbolRequired
}
var priceAPI api.Interface
if config.APIChoice == CoinMarketCap {
priceAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko {
priceAPI = api.NewCG()
} else {
return ErrInvalidAPIChoice
return nil, ErrInvalidAPIChoice
}
price, err := priceAPI.Price(config.Coin, config.Currency)
if err != nil {
return err
}
var prices []string
for _, coin := range config.Coins {
price, err := priceAPI.Price(coin, config.Currency)
if err != nil {
return nil, err
}
symbol := CurrencySymbol(config.Currency)
value := fmt.Sprintf("%s%s", symbol, humanize.Commaf(price))
fmt.Println(value)
symbol := CurrencySymbol(config.Currency)
value := fmt.Sprintf("%s%s", symbol, humanize.Commaf(price))
prices = append(prices, value)
}
return nil
return prices, nil
}

Loading…
Cancel
Save