2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-05 00:00:14 +00:00

Prompt for CoinMarketCap Pro API Key

This commit is contained in:
Miguel Mota 2019-04-23 14:37:00 -07:00
parent a118bd38aa
commit be84b5e5bd
9 changed files with 66 additions and 12 deletions

View File

@ -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

View File

@ -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()
}

View File

@ -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"

21
cointop/stdin.go Normal file
View File

@ -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)
}

View File

@ -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

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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,
}

2
vendor/modules.txt vendored
View File

@ -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