mirror of https://github.com/miguelmota/cointop
Add command to read bitcoin dominance percentage
parent
8d3d026b50
commit
261f893c61
@ -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
|
||||||
|
}
|
@ -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…
Reference in New Issue