command[clean]: Delete cache_dir from config

pull/192/head
Miguel Mota 3 years ago
parent f375eec1eb
commit a07bed9dab
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

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

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

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

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

Loading…
Cancel
Save