From d9ea155004a6ec5e1dbf8ad3cfd07244fe08e8eb Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Tue, 4 Aug 2020 00:07:53 -0700 Subject: [PATCH] Add no-cache option --- Makefile | 10 ++--- cmd/cointop/cointop.go | 9 ++++ cointop/cache.go | 4 +- cointop/chart.go | 20 ++++++--- cmd/cointop.go => cointop/cmd/cmd.go | 4 +- cointop/cointop.go | 64 +++++++++++++++++++++------- cointop/marketbar.go | 12 ++++-- main.go | 2 +- 8 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 cmd/cointop/cointop.go rename cmd/cointop.go => cointop/cmd/cmd.go (98%) diff --git a/Makefile b/Makefile index 434ed47..a0af315 100644 --- a/Makefile +++ b/Makefile @@ -28,18 +28,18 @@ debug: DEBUG=1 go run main.go build: - go build -o bin/cointop main.go + go build -ldflags "-X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop main.go # http://macappstore.org/upx build-mac: clean-mac - env GOARCH=amd64 go build -ldflags "-s -w" -o bin/macos/cointop && upx bin/macos/cointop + env GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/macos/cointop && upx bin/macos/cointop build-linux: clean-linux - env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/linux/cointop && upx bin/linux/cointop + env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/linux/cointop && upx bin/linux/cointop build-multiple: clean - env GOARCH=amd64 go build -ldflags "-s -w" -o bin/cointop64 && upx bin/cointop64 && \ - env GOARCH=386 go build -ldflags "-s -w" -o bin/cointop32 && upx bin/cointop32 + env GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop64 && upx bin/cointop64 && \ + env GOARCH=386 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop32 && upx bin/cointop32 clean-mac: go clean && \ diff --git a/cmd/cointop/cointop.go b/cmd/cointop/cointop.go new file mode 100644 index 0000000..4466657 --- /dev/null +++ b/cmd/cointop/cointop.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/miguelmota/cointop/cointop/cmd" +) + +func main() { + cmd.Execute() +} diff --git a/cointop/cache.go b/cointop/cache.go index b156f61..a46eef2 100644 --- a/cointop/cache.go +++ b/cointop/cache.go @@ -25,6 +25,8 @@ func (ct *Cointop) CacheAllCoinsSlugMap() { if len(allCoinsSlugMap) != 0 { cachekey := ct.CacheKey("allCoinsSlugMap") ct.cache.Set(cachekey, allCoinsSlugMap, 10*time.Second) - ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour) + if ct.filecache != nil { + ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour) + } } } diff --git a/cointop/chart.go b/cointop/chart.go index 29df813..04bcfec 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -174,9 +174,11 @@ func (ct *Cointop) ChartPoints(symbol string, name string) error { } ct.cache.Set(cachekey, data, 10*time.Second) - go func() { - ct.filecache.Set(cachekey, data, 24*time.Hour) - }() + if ct.filecache != nil { + go func() { + ct.filecache.Set(cachekey, data, 24*time.Hour) + }() + } } chart.Data = data @@ -260,7 +262,9 @@ func (ct *Cointop) PortfolioChart() error { graphData, _ = cached.([]float64) ct.debuglog("soft cache hit") } else { - ct.filecache.Get(cachekey, &graphData) + if ct.filecache != nil { + ct.filecache.Get(cachekey, &graphData) + } if len(graphData) == 0 { time.Sleep(2 * time.Second) @@ -277,9 +281,11 @@ func (ct *Cointop) PortfolioChart() error { } ct.cache.Set(cachekey, graphData, 10*time.Second) - go func() { - ct.filecache.Set(cachekey, graphData, 24*time.Hour) - }() + if ct.filecache != nil { + go func() { + ct.filecache.Set(cachekey, graphData, 24*time.Hour) + }() + } } for i := range graphData { diff --git a/cmd/cointop.go b/cointop/cmd/cmd.go similarity index 98% rename from cmd/cointop.go rename to cointop/cmd/cmd.go index 7e2fdf8..c01f93c 100644 --- a/cmd/cointop.go +++ b/cointop/cmd/cmd.go @@ -12,7 +12,7 @@ import ( // Execute executes the program func Execute() { - var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent bool + var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent, noCache bool var refreshRate uint var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string cacheDir := filecache.DefaultCacheDir @@ -68,6 +68,7 @@ For more information, visit: https://github.com/miguelmota/cointop`, ct, err := cointop.NewCointop(&cointop.Config{ CacheDir: cacheDir, + NoCache: noCache, ConfigFilepath: config, CoinMarketCapAPIKey: cmcAPIKey, APIChoice: apiChoice, @@ -95,6 +96,7 @@ For more information, visit: https://github.com/miguelmota/cointop`, rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", false, "Hide the bottom statusbar") rootCmd.Flags().BoolVarP(&onlyTable, "only-table", "", false, "Show only the table. Hides the chart and top and bottom bars") rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silence log ouput") + rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache") rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh") rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key") diff --git a/cointop/cointop.go b/cointop/cointop.go index 76aa045..313527f 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -131,6 +131,7 @@ type Config struct { HideMarketbar bool HideChart bool HideStatusbar bool + NoCache bool OnlyTable bool RefreshRate *uint } @@ -162,6 +163,13 @@ func NewCointop(config *Config) (*Cointop, error) { configFilepath = config.ConfigFilepath } + var fcache *filecache.FileCache + if !config.NoCache { + fcache = filecache.NewFileCache(&filecache.Config{ + CacheDir: config.CacheDir, + }) + } + ct := &Cointop{ apiChoice: CoinGecko, apiKeys: new(APIKeys), @@ -174,9 +182,7 @@ func NewCointop(config *Config) (*Cointop, error) { debug: debug, chartRangesMap: ChartRangesMap(), limiter: time.Tick(2 * time.Second), - filecache: filecache.NewFileCache(&filecache.Config{ - CacheDir: config.CacheDir, - }), + filecache: fcache, State: &State{ allCoins: []*Coin{}, currencyConversion: "USD", @@ -293,7 +299,9 @@ func NewCointop(config *Config) (*Cointop, error) { allCoinsSlugMap := make(map[string]*Coin) coinscachekey := ct.CacheKey("allCoinsSlugMap") - ct.filecache.Get(coinscachekey, &allCoinsSlugMap) + if ct.filecache != nil { + ct.filecache.Get(coinscachekey, &allCoinsSlugMap) + } for k, v := range allCoinsSlugMap { ct.State.allCoinsSlugMap.Store(k, v) @@ -332,12 +340,16 @@ func NewCointop(config *Config) (*Cointop, error) { var globaldata []float64 chartcachekey := ct.CacheKey(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.State.selectedChartRange, " ", "", -1))) - ct.filecache.Get(chartcachekey, &globaldata) + if ct.filecache != nil { + ct.filecache.Get(chartcachekey, &globaldata) + } ct.cache.Set(chartcachekey, globaldata, 10*time.Second) var market types.GlobalMarketData marketcachekey := ct.CacheKey("market") - ct.filecache.Get(marketcachekey, &market) + if ct.filecache != nil { + ct.filecache.Get(marketcachekey, &market) + } ct.cache.Set(marketcachekey, market, 10*time.Second) // TODO: notify offline status in status bar @@ -417,6 +429,8 @@ func Clean(config *CleanConfig) error { config = &CleanConfig{} } + cacheCleaned := false + cacheDir := filecache.DefaultCacheDir if config.CacheDir != "" { cacheDir = config.CacheDir @@ -437,12 +451,16 @@ func Clean(config *CleanConfig) error { if err := os.Remove(file); err != nil { return err } + + cacheCleaned = true } } } if config.Log { - fmt.Println("cointop cache has been cleaned") + if cacheCleaned { + fmt.Println("cointop cache has been cleaned") + } } return nil @@ -467,19 +485,33 @@ func Reset(config *ResetConfig) error { return err } - // default config path - configPath := fmt.Sprintf("%s%s", pathutil.UserPreferredHomeDir(), "/.cointop") - if _, err := os.Stat(configPath); !os.IsNotExist(err) { - if config.Log { - fmt.Printf("removing %s\n", configPath) - } - if err := os.RemoveAll(configPath); err != nil { - return err + configDeleted := false + + possibleConfigPaths := []string{ + "~/.config/cointop/config.toml", + "~/.config/cointop/config", + "~/.cointop/config", + "~/.cointop/config.toml", + } + + for _, configPath := range possibleConfigPaths { + normalizedPath := pathutil.NormalizePath(configPath) + if _, err := os.Stat(normalizedPath); !os.IsNotExist(err) { + if config.Log { + fmt.Printf("removing %s\n", normalizedPath) + } + if err := os.RemoveAll(normalizedPath); err != nil { + return err + } + + configDeleted = true } } if config.Log { - fmt.Println("cointop has been reset") + if configDeleted { + fmt.Println("cointop has been reset") + } } return nil diff --git a/cointop/marketbar.go b/cointop/marketbar.go index 1799571..9b0f94f 100644 --- a/cointop/marketbar.go +++ b/cointop/marketbar.go @@ -106,13 +106,17 @@ func (ct *Cointop) UpdateMarketbar() error { if market.TotalMarketCapUSD == 0 { market, err = ct.api.GetGlobalMarketData(ct.State.currencyConversion) if err != nil { - ct.filecache.Get(cachekey, &market) + if ct.filecache != nil { + ct.filecache.Get(cachekey, &market) + } } ct.cache.Set(cachekey, market, 10*time.Second) - go func() { - ct.filecache.Set(cachekey, market, 24*time.Hour) - }() + if ct.filecache != nil { + go func() { + ct.filecache.Set(cachekey, market, 24*time.Hour) + }() + } } timeframe := ct.State.selectedChartRange diff --git a/main.go b/main.go index 806466f..4466657 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/miguelmota/cointop/cmd" + "github.com/miguelmota/cointop/cointop/cmd" ) func main() {