From be84b5e5bd56f95c235f8d27fd14196fb9fedeb8 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Tue, 23 Apr 2019 14:37:00 -0700 Subject: [PATCH] Prompt for CoinMarketCap Pro API Key --- CHANGELOG.md | 8 ++++++ cmd/cointop.go | 10 ++++--- cointop/cointop.go | 26 ++++++++++++++++++- cointop/stdin.go | 21 +++++++++++++++ cointop/version.go | 2 +- go.mod | 2 +- go.sum | 2 ++ .../go-coinmarketcap/pro/v1/coinmarketcap.go | 5 ---- vendor/modules.txt | 2 +- 9 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 cointop/stdin.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7517af5..5da6d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.6] - 2019-04-23 +### Added +- Prompt for CoinMarketCap Pro API Key + +## [1.1.5] - 2019-04-22 +### Fixed +- Release archive to contain latest source code + ## [1.1.4] - 2019-04-21 ### Changed - CoinMarketCap legacy V2 API to Pro V1 API diff --git a/cmd/cointop.go b/cmd/cointop.go index 5a6bfe2..ae1b668 100644 --- a/cmd/cointop.go +++ b/cmd/cointop.go @@ -10,13 +10,14 @@ import ( // Run ... func Run() { var v, ver, test, clean, reset bool - var config string + var config, cmcAPIKey string flag.BoolVar(&v, "v", false, "Version") flag.BoolVar(&ver, "version", false, "Version") flag.BoolVar(&test, "test", false, "Run test") flag.BoolVar(&clean, "clean", false, "Clean cache") flag.BoolVar(&reset, "reset", false, "Reset config") flag.StringVar(&config, "config", "", "Config filepath") + flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "CoinMarketCap API key") flag.Parse() if v || ver { fmt.Printf("cointop v%s", cointop.Version()) @@ -28,11 +29,14 @@ func Run() { cointop.Reset() } else { cointop.NewCointop(&cointop.Config{ - ConfigFilepath: config, + ConfigFilepath: config, + CoinMarketCapAPIKey: cmcAPIKey, }).Run() } } func doTest() { - cointop.NewCointop(nil).Exit() + cointop.NewCointop(&cointop.Config{ + NoPrompts: true, + }).Exit() } diff --git a/cointop/cointop.go b/cointop/cointop.go index ab54666..8c6b45b 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -99,7 +99,9 @@ type portfolio struct { // Config config options type Config struct { - ConfigFilepath string + ConfigFilepath string + CoinMarketCapAPIKey string + NoPrompts bool } // apiKeys is api keys structure @@ -206,6 +208,28 @@ func NewCointop(config *Config) *Cointop { log.Fatal(err) } + // prompt for CoinMarketCap api key if not found + if config.CoinMarketCapAPIKey != "" { + ct.apiKeys.cmc = config.CoinMarketCapAPIKey + if err := ct.saveConfig(); err != nil { + log.Fatal(err) + } + } + + if ct.apiKeys.cmc == "" { + apiKey := os.Getenv("CMC_PRO_API_KEY") + if apiKey == "" { + if !config.NoPrompts { + ct.apiKeys.cmc = ct.readAPIKeyFromStdin("CoinMarketCap Pro") + } + } else { + ct.apiKeys.cmc = apiKey + } + if err := ct.saveConfig(); err != nil { + log.Fatal(err) + } + } + ct.api = api.NewCMC(ct.apiKeys.cmc) coinscachekey := "allcoinsslugmap" diff --git a/cointop/stdin.go b/cointop/stdin.go new file mode 100644 index 0000000..f7dece9 --- /dev/null +++ b/cointop/stdin.go @@ -0,0 +1,21 @@ +package cointop + +import ( + "bufio" + "fmt" + "os" + "strings" + + log "github.com/sirupsen/logrus" +) + +func (ct *Cointop) readAPIKeyFromStdin(name string) string { + reader := bufio.NewReader(os.Stdin) + fmt.Printf("Enter %s API Key: ", name) + text, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + + return strings.TrimSpace(text) +} diff --git a/cointop/version.go b/cointop/version.go index 067141a..f5df71c 100644 --- a/cointop/version.go +++ b/cointop/version.go @@ -1,7 +1,7 @@ package cointop // TODO: make dynamic based on git tag -const version = "1.1.5" +const version = "1.1.6" func (ct *Cointop) version() string { return version diff --git a/go.mod b/go.mod index feb01d4..48ee4ab 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/mattn/go-isatty v0.0.6 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect - github.com/miguelmota/go-coinmarketcap v0.1.3 + github.com/miguelmota/go-coinmarketcap v0.1.4 github.com/mitchellh/go-wordwrap v1.0.0 // indirect github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect github.com/patrickmn/go-cache v2.1.0+incompatible diff --git a/go.sum b/go.sum index b637c73..95f714f 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,8 @@ github.com/miguelmota/go-coinmarketcap v0.1.2 h1:XGhLhzruXD14sVS3kuXAtAinvwJK3m1 github.com/miguelmota/go-coinmarketcap v0.1.2/go.mod h1:Jdv/kqtKclIElmoNAZMMJn0DSQv+j7p/H1te/GGnxhA= github.com/miguelmota/go-coinmarketcap v0.1.3 h1:6/TvCnvq6tNVa8NG33X5uiIfIHI55mRmmArnUQ7Hdeg= github.com/miguelmota/go-coinmarketcap v0.1.3/go.mod h1:Jdv/kqtKclIElmoNAZMMJn0DSQv+j7p/H1te/GGnxhA= +github.com/miguelmota/go-coinmarketcap v0.1.4 h1:x3AXc/b8MbFtfAEI5hQphtcbwTm4OAXJPjVOHf1ETt0= +github.com/miguelmota/go-coinmarketcap v0.1.4/go.mod h1:Jdv/kqtKclIElmoNAZMMJn0DSQv+j7p/H1te/GGnxhA= github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840= diff --git a/vendor/github.com/miguelmota/go-coinmarketcap/pro/v1/coinmarketcap.go b/vendor/github.com/miguelmota/go-coinmarketcap/pro/v1/coinmarketcap.go index 8b53181..bcdbf1a 100644 --- a/vendor/github.com/miguelmota/go-coinmarketcap/pro/v1/coinmarketcap.go +++ b/vendor/github.com/miguelmota/go-coinmarketcap/pro/v1/coinmarketcap.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "os" "strings" ) @@ -248,10 +247,6 @@ func NewClient(cfg *Config) *Client { cfg.ProAPIKey = os.Getenv("CMC_PRO_API_KEY") } - if cfg.ProAPIKey == "" { - log.Fatal("Pro API Key is required") - } - c := &Client{ proAPIKey: cfg.ProAPIKey, } diff --git a/vendor/modules.txt b/vendor/modules.txt index 71a799a..5e488c5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -18,7 +18,7 @@ github.com/mattn/go-colorable github.com/mattn/go-isatty # github.com/mattn/go-runewidth v0.0.4 github.com/mattn/go-runewidth -# github.com/miguelmota/go-coinmarketcap v0.1.3 +# github.com/miguelmota/go-coinmarketcap v0.1.4 github.com/miguelmota/go-coinmarketcap/pro/v1 github.com/miguelmota/go-coinmarketcap/v2 github.com/miguelmota/go-coinmarketcap/v2/types