Merge pull request #192 from cointop-sh/fix/clean-cache-dir

fix: Delete cache_dir from config when using clean/reset commands
pull/194/head^2
Miguel Mota 3 years ago committed by GitHub
commit 9b03adc2bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,22 +1,30 @@
package cmd
import (
"fmt"
"os"
"github.com/cointop-sh/cointop/cointop"
"github.com/cointop-sh/cointop/pkg/filecache"
"github.com/spf13/cobra"
)
// CleanCmd ...
// CleanCmd will wipe the cache only
func CleanCmd() *cobra.Command {
cacheDir := filecache.DefaultCacheDir
config := os.Getenv("COINTOP_CONFIG")
cacheDir := os.Getenv("COINTOP_CACHE_DIR")
cleanCmd := &cobra.Command{
Use: "clean",
Short: "Clear the cache",
Long: `The clean command clears the cache`,
RunE: func(cmd *cobra.Command, args []string) error {
// NOTE: if clean command, clean but don't run cointop
return cointop.Clean(&cointop.CleanConfig{
ct, err := cointop.NewCointop(&cointop.Config{
ConfigFilepath: config,
})
if err != nil {
return err
}
return ct.Clean(&cointop.CleanConfig{
Log: true,
CacheDir: cacheDir,
})
@ -24,6 +32,7 @@ func CleanCmd() *cobra.Command {
}
cleanCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, "Cache directory")
cleanCmd.Flags().StringVarP(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
return cleanCmd
}

@ -1,22 +1,30 @@
package cmd
import (
"fmt"
"os"
"github.com/cointop-sh/cointop/cointop"
"github.com/cointop-sh/cointop/pkg/filecache"
"github.com/spf13/cobra"
)
// ResetCmd ...
// ResetCmd will wipe cache and config file
func ResetCmd() *cobra.Command {
cacheDir := filecache.DefaultCacheDir
config := os.Getenv("COINTOP_CONFIG")
cacheDir := os.Getenv("COINTOP_CACHE_DIR")
resetCmd := &cobra.Command{
Use: "reset",
Short: "Resets the config and clear the cache",
Long: `The reset command resets the config and clears the cache`,
RunE: func(cmd *cobra.Command, args []string) error {
// NOTE: if reset command, reset but don't run cointop
return cointop.Reset(&cointop.ResetConfig{
ct, err := cointop.NewCointop(&cointop.Config{
ConfigFilepath: config,
})
if err != nil {
return err
}
return ct.Reset(&cointop.ResetConfig{
Log: true,
CacheDir: cacheDir,
})
@ -24,6 +32,7 @@ func ResetCmd() *cobra.Command {
}
resetCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, "Cache directory")
resetCmd.Flags().StringVarP(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
return resetCmd
}

@ -60,21 +60,27 @@ See git.io/cointop for more info.`,
return nil
}
// NOTE: if reset flag enabled, reset and run cointop
if reset {
if err := cointop.Reset(&cointop.ResetConfig{
Log: !silent,
}); err != nil {
// wipe before starting program
if reset || clean {
ct, err := cointop.NewCointop(&cointop.Config{
CacheDir: cacheDir,
ConfigFilepath: config,
})
if err != nil {
return err
}
}
// NOTE: if clean flag enabled, clean and run cointop
if clean {
if err := cointop.Clean(&cointop.CleanConfig{
Log: !silent,
}); err != nil {
return err
if reset {
if err := ct.Reset(&cointop.ResetConfig{
Log: !silent,
}); err != nil {
return err
}
} else if clean {
if err := ct.Clean(&cointop.CleanConfig{
Log: !silent,
}); err != nil {
return err
}
}
}

@ -523,18 +523,19 @@ type CleanConfig struct {
}
// Clean removes cache files
func Clean(config *CleanConfig) error {
func (ct *Cointop) Clean(config *CleanConfig) error {
if config == nil {
config = &CleanConfig{}
}
cacheCleaned := false
cacheDir := DefaultCacheDir
if config.CacheDir != "" {
cacheDir = pathutil.NormalizePath(config.CacheDir)
} else if ct.State.cacheDir != "" {
cacheDir = ct.State.cacheDir
}
cacheCleaned := false
if _, err := os.Stat(cacheDir); !os.IsNotExist(err) {
files, err := ioutil.ReadDir(cacheDir)
if err != nil {
@ -572,12 +573,12 @@ type ResetConfig struct {
}
// Reset removes configuration and cache files
func Reset(config *ResetConfig) error {
func (ct *Cointop) Reset(config *ResetConfig) error {
if config == nil {
config = &ResetConfig{}
}
if err := Clean(&CleanConfig{
if err := ct.Clean(&CleanConfig{
CacheDir: config.CacheDir,
Log: config.Log,
}); err != nil {

Loading…
Cancel
Save