Add command to read bitcoin dominance percentage

pull/61/head
Miguel Mota 4 years ago
parent 8d3d026b50
commit 261f893c61

@ -7,8 +7,9 @@ func Execute() {
VersionCmd(),
CleanCmd(),
ResetCmd(),
PriceCmd(),
HoldingsCmd(),
PriceCmd(),
DominanceCmd(),
ServerCmd(),
TestCmd(),
)

@ -0,0 +1,28 @@
package cmd
import (
"github.com/miguelmota/cointop/cointop"
"github.com/spf13/cobra"
)
// DominanceCmd ...
func DominanceCmd() *cobra.Command {
var apiChoice, currency string
dominanceCmd := &cobra.Command{
Use: "dominance",
Short: "Displays bitcoin dominance",
Long: `The dominance command display the dominance percentage of bitcoin`,
RunE: func(cmd *cobra.Command, args []string) error {
return cointop.PrintBitcoinDominance(&cointop.DominanceConfig{
Currency: currency,
APIChoice: apiChoice,
})
},
}
dominanceCmd.Flags().StringVarP(&currency, "currency", "f", "USD", "The currency to convert to")
dominanceCmd.Flags().StringVarP(&apiChoice, "api", "a", cointop.CoinGecko, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")
return dominanceCmd
}

@ -22,8 +22,8 @@ func PriceCmd() *cobra.Command {
},
}
priceCmd.Flags().StringVarP(&coin, "coin", "c", "bitcoin", "Full name of the coin (default \"bitcoin\")")
priceCmd.Flags().StringVarP(&currency, "currency", "f", "USD", "The currency to convert to (default \"USD\")")
priceCmd.Flags().StringVarP(&coin, "coin", "c", "bitcoin", "Full name of the coin")
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\"")
return priceCmd

@ -13,7 +13,6 @@ import (
"github.com/miguelmota/cointop/cointop/common/api/types"
"github.com/miguelmota/cointop/cointop/common/filecache"
"github.com/miguelmota/cointop/cointop/common/gizak/termui"
"github.com/miguelmota/cointop/cointop/common/humanize"
"github.com/miguelmota/cointop/cointop/common/pathutil"
"github.com/miguelmota/cointop/cointop/common/table"
"github.com/miguelmota/gocui"
@ -408,35 +407,6 @@ func (ct *Cointop) IsRunning() bool {
return ct.State.running
}
// PriceConfig is the config options for the price command
type PriceConfig struct {
Coin string
Currency string
APIChoice string
}
// PrintPrice outputs the current price of the coin
func PrintPrice(config *PriceConfig) error {
var priceAPI api.Interface
if config.APIChoice == CoinMarketCap {
priceAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko {
priceAPI = api.NewCG()
} else {
return ErrInvalidAPIChoice
}
price, err := priceAPI.Price(config.Coin, config.Currency)
if err != nil {
return err
}
symbol := CurrencySymbol(config.Currency)
fmt.Fprintf(os.Stdout, "%s%s\n", symbol, humanize.Commaf(price))
return nil
}
// CleanConfig is the config for the clean function
type CleanConfig struct {
Log bool

@ -0,0 +1,39 @@
package cointop
import (
"fmt"
"github.com/miguelmota/cointop/cointop/common/api"
)
// DominanceConfig is the config options for the dominance command
type DominanceConfig struct {
Currency string
APIChoice string
}
// PrintBitcoinDominance outputs the dominance percentage of bitcoin
func PrintBitcoinDominance(config *DominanceConfig) error {
if config == nil {
config = &DominanceConfig{}
}
var coinAPI api.Interface
if config.APIChoice == CoinMarketCap {
coinAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko {
coinAPI = api.NewCG()
} else {
return ErrInvalidAPIChoice
}
data, err := coinAPI.GetGlobalMarketData(config.Currency)
if err != nil {
return err
}
value := fmt.Sprintf("Bitcoin: %.2f%%", data.BitcoinPercentageOfMarketCap)
fmt.Println(value)
return nil
}

@ -0,0 +1,38 @@
package cointop
import (
"fmt"
"github.com/miguelmota/cointop/cointop/common/api"
"github.com/miguelmota/cointop/cointop/common/humanize"
)
// PriceConfig is the config options for the price command
type PriceConfig struct {
Coin string
Currency string
APIChoice string
}
// PrintPrice outputs the current price of the coin
func PrintPrice(config *PriceConfig) error {
var priceAPI api.Interface
if config.APIChoice == CoinMarketCap {
priceAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko {
priceAPI = api.NewCG()
} else {
return ErrInvalidAPIChoice
}
price, err := priceAPI.Price(config.Coin, config.Currency)
if err != nil {
return err
}
symbol := CurrencySymbol(config.Currency)
value := fmt.Sprintf("%s%s", symbol, humanize.Commaf(price))
fmt.Println(value)
return nil
}
Loading…
Cancel
Save