diff --git a/cmd/commands/clean.go b/cmd/commands/clean.go index 1ee4588..6b2d575 100644 --- a/cmd/commands/clean.go +++ b/cmd/commands/clean.go @@ -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 } diff --git a/cmd/commands/reset.go b/cmd/commands/reset.go index 32fa32b..463ba28 100644 --- a/cmd/commands/reset.go +++ b/cmd/commands/reset.go @@ -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 } diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 055c4c2..53a7ee6 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -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 + } } } diff --git a/cointop/cointop.go b/cointop/cointop.go index 0efc314..8ba1b61 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -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 {