Skip to content

Commit

Permalink
Store cache dir preference in config file and soft fail if cache dir …
Browse files Browse the repository at this point in the history
…error
  • Loading branch information
miguelmota committed Aug 31, 2020
1 parent f82c113 commit 890e1c5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Rank order for low market cap coins

### Added
- Colorschemes directory flag

## [1.5.3] - 2020-08-14
### Fixed
- Build error
Expand Down
4 changes: 2 additions & 2 deletions cointop/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func RootCmd() *cobra.Command {
var apiChoice string
var colorscheme string
var perPage = cointop.DefaultPerPage
var cacheDir = cointop.DefaultCacheDir
var cacheDir string
var colorsDir string

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -116,7 +116,7 @@ See git.io/cointop for more info.`,
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")
rootCmd.Flags().StringVarP(&apiChoice, "api", "", "", "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")
rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", "", fmt.Sprintf("Colorscheme to use (default \"cointop\").\n%s", cointop.ColorschemeHelpString()))
rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, "Cache directory")
rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, fmt.Sprintf("Cache directory (default %s)", cointop.DefaultCacheDir))
rootCmd.Flags().StringVarP(&colorsDir, "colors-dir", "", colorsDir, "Colorschemes directory")

return rootCmd
Expand Down
31 changes: 22 additions & 9 deletions cointop/cointop.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Views struct {
type State struct {
allCoins []*Coin
allCoinsSlugMap sync.Map
cacheDir string
coins []*Coin
chartPoints [][]termui.Cell
currencyConversion string
Expand Down Expand Up @@ -175,13 +176,6 @@ func NewCointop(config *Config) (*Cointop, error) {
configFilepath = config.ConfigFilepath
}

var fcache *filecache.FileCache
if !config.NoCache {
fcache = filecache.NewFileCache(&filecache.Config{
CacheDir: config.CacheDir,
})
}

perPage := DefaultPerPage
if config.PerPage != 0 {
perPage = config.PerPage
Expand All @@ -200,9 +194,10 @@ func NewCointop(config *Config) (*Cointop, error) {
debug: debug,
chartRangesMap: ChartRangesMap(),
limiter: time.Tick(2 * time.Second),
filecache: fcache,
filecache: nil,
State: &State{
allCoins: []*Coin{},
cacheDir: DefaultCacheDir,
currencyConversion: "USD",
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
favoritesBySymbol: make(map[string]bool),
Expand Down Expand Up @@ -258,6 +253,24 @@ func NewCointop(config *Config) (*Cointop, error) {
ct.refreshTicker = time.NewTicker(ct.State.refreshRate)
}

if config.CacheDir != "" {
ct.State.cacheDir = pathutil.NormalizePath(config.CacheDir)
if err := ct.SaveConfig(); err != nil {
return nil, err
}
}

if !config.NoCache {
fcache, err := filecache.NewFileCache(&filecache.Config{
CacheDir: ct.State.cacheDir,
})
if err != nil {
fmt.Printf("error: %s\nproceeding without filecache.", err)
}

ct.filecache = fcache
}

// prompt for CoinMarketCap api key if not found
if config.CoinMarketCapAPIKey != "" {
ct.apiKeys.cmc = config.CoinMarketCapAPIKey
Expand Down Expand Up @@ -440,7 +453,7 @@ func Clean(config *CleanConfig) error {

cacheDir := DefaultCacheDir
if config.CacheDir != "" {
cacheDir = config.CacheDir
cacheDir = pathutil.NormalizePath(config.CacheDir)
}

if _, err := os.Stat(cacheDir); !os.IsNotExist(err) {
Expand Down
8 changes: 4 additions & 4 deletions cointop/common/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// DefaultCacheDir ...
var DefaultCacheDir = "/tmp"
var DefaultCacheDir = "/tmp1"

// FileCache ...
type FileCache struct {
Expand All @@ -30,7 +30,7 @@ type Config struct {
}

// NewFileCache ...
func NewFileCache(config *Config) *FileCache {
func NewFileCache(config *Config) (*FileCache, error) {
if config == nil {
config = &Config{}
}
Expand All @@ -42,14 +42,14 @@ func NewFileCache(config *Config) *FileCache {

if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0700); err != nil {
panic(err)
return nil, err
}
}

return &FileCache{
muts: make(map[string]*sync.Mutex),
cacheDir: cacheDir,
}
}, nil
}

// Set writes item to cache
Expand Down
16 changes: 16 additions & 0 deletions cointop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type config struct {
API interface{} `toml:"api"`
Colorscheme interface{} `toml:"colorscheme"`
RefreshRate interface{} `toml:"refresh_rate"`
CacheDir interface{} `toml:"cache_dir"`
}

// SetupConfig loads config file
Expand Down Expand Up @@ -69,6 +70,9 @@ func (ct *Cointop) SetupConfig() error {
if err := ct.loadRefreshRateFromConfig(); err != nil {
return err
}
if err := ct.loadCacheDirFromConfig(); err != nil {
return err
}
if err := ct.loadPortfolioFromConfig(); err != nil {
return err
}
Expand Down Expand Up @@ -217,6 +221,7 @@ func (ct *Cointop) configToToml() ([]byte, error) {
var defaultViewIfc interface{} = ct.State.defaultView
var colorschemeIfc interface{} = ct.colorschemeName
var refreshRateIfc interface{} = uint(ct.State.refreshRate.Seconds())
var cacheDirIfc interface{} = ct.State.cacheDir

cmcIfc := map[string]interface{}{
"pro_api_key": ct.apiKeys.cmc,
Expand All @@ -233,6 +238,7 @@ func (ct *Cointop) configToToml() ([]byte, error) {
RefreshRate: refreshRateIfc,
Shortcuts: shortcutsIfcs,
Portfolio: portfolioIfc,
CacheDir: cacheDirIfc,
}

var b bytes.Buffer
Expand Down Expand Up @@ -325,6 +331,16 @@ func (ct *Cointop) loadRefreshRateFromConfig() error {
return nil
}

// LoadCacheDirFromConfig loads cache dir from config file to struct
func (ct *Cointop) loadCacheDirFromConfig() error {
ct.debuglog("loadCacheDirFromConfig()")
if cacheDir, ok := ct.config.CacheDir.(string); ok {
ct.State.cacheDir = cacheDir
}

return nil
}

// GetColorschemeColors loads colors from colorsheme file to struct
func (ct *Cointop) getColorschemeColors() (map[string]interface{}, error) {
ct.debuglog("getColorschemeColors()")
Expand Down

0 comments on commit 890e1c5

Please sign in to comment.