diff --git a/CHANGELOG.md b/CHANGELOG.md index d5b8b0a..503215f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cointop/cmd/root.go b/cointop/cmd/root.go index 609b86a..71ff00d 100644 --- a/cointop/cmd/root.go +++ b/cointop/cmd/root.go @@ -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{ @@ -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 diff --git a/cointop/cointop.go b/cointop/cointop.go index b7a07c2..fbc2e1b 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -42,6 +42,7 @@ type Views struct { type State struct { allCoins []*Coin allCoinsSlugMap sync.Map + cacheDir string coins []*Coin chartPoints [][]termui.Cell currencyConversion string @@ -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 @@ -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), @@ -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 @@ -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) { diff --git a/cointop/common/filecache/filecache.go b/cointop/common/filecache/filecache.go index 62ed14f..0a02d77 100644 --- a/cointop/common/filecache/filecache.go +++ b/cointop/common/filecache/filecache.go @@ -16,7 +16,7 @@ import ( ) // DefaultCacheDir ... -var DefaultCacheDir = "/tmp" +var DefaultCacheDir = "/tmp1" // FileCache ... type FileCache struct { @@ -30,7 +30,7 @@ type Config struct { } // NewFileCache ... -func NewFileCache(config *Config) *FileCache { +func NewFileCache(config *Config) (*FileCache, error) { if config == nil { config = &Config{} } @@ -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 diff --git a/cointop/config.go b/cointop/config.go index 8456f15..b235b88 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -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 @@ -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 } @@ -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, @@ -233,6 +238,7 @@ func (ct *Cointop) configToToml() ([]byte, error) { RefreshRate: refreshRateIfc, Shortcuts: shortcutsIfcs, Portfolio: portfolioIfc, + CacheDir: cacheDirIfc, } var b bytes.Buffer @@ -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()")