From 261f893c61a0a7687a85c784a69fedcf0e32df4b Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Mon, 10 Aug 2020 23:12:56 -0700 Subject: [PATCH] Add command to read bitcoin dominance percentage --- cointop/cmd/cmd.go | 3 ++- cointop/cmd/dominance.go | 28 ++++++++++++++++++++++++++++ cointop/cmd/price.go | 4 ++-- cointop/cointop.go | 30 ------------------------------ cointop/dominance.go | 39 +++++++++++++++++++++++++++++++++++++++ cointop/price.go | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 cointop/cmd/dominance.go create mode 100644 cointop/dominance.go create mode 100644 cointop/price.go diff --git a/cointop/cmd/cmd.go b/cointop/cmd/cmd.go index 9647569..0f8672d 100644 --- a/cointop/cmd/cmd.go +++ b/cointop/cmd/cmd.go @@ -7,8 +7,9 @@ func Execute() { VersionCmd(), CleanCmd(), ResetCmd(), - PriceCmd(), HoldingsCmd(), + PriceCmd(), + DominanceCmd(), ServerCmd(), TestCmd(), ) diff --git a/cointop/cmd/dominance.go b/cointop/cmd/dominance.go new file mode 100644 index 0000000..bc5ef44 --- /dev/null +++ b/cointop/cmd/dominance.go @@ -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(¤cy, "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 +} diff --git a/cointop/cmd/price.go b/cointop/cmd/price.go index 23d8a4c..3751cb1 100644 --- a/cointop/cmd/price.go +++ b/cointop/cmd/price.go @@ -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(¤cy, "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(¤cy, "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 diff --git a/cointop/cointop.go b/cointop/cointop.go index 33688ed..8b4c15f 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -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 diff --git a/cointop/dominance.go b/cointop/dominance.go new file mode 100644 index 0000000..fb2ded4 --- /dev/null +++ b/cointop/dominance.go @@ -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 +} diff --git a/cointop/price.go b/cointop/price.go new file mode 100644 index 0000000..b02595b --- /dev/null +++ b/cointop/price.go @@ -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 +}