github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/clean/cache.go (about) 1 package clean 2 3 import ( 4 "os" 5 6 "github.com/ActiveState/cli/internal/constants" 7 "github.com/ActiveState/cli/internal/errs" 8 "github.com/ActiveState/cli/internal/installation/storage" 9 "github.com/ActiveState/cli/internal/locale" 10 "github.com/ActiveState/cli/internal/logging" 11 "github.com/ActiveState/cli/internal/output" 12 "github.com/ActiveState/cli/internal/svcctl" 13 "github.com/ActiveState/cli/pkg/platform/runtime/target" 14 "github.com/ActiveState/cli/pkg/projectfile" 15 ) 16 17 type Cache struct { 18 output output.Outputer 19 config configurable 20 confirm promptable 21 path string 22 ipComm svcctl.IPCommunicator 23 } 24 25 type CacheParams struct { 26 Force bool 27 Project string 28 } 29 30 func NewCache(prime primeable) *Cache { 31 return newCache(prime.Output(), prime.Config(), prime.Prompt(), prime.IPComm()) 32 } 33 34 func newCache(output output.Outputer, cfg configurable, confirm promptable, ipComm svcctl.IPCommunicator) *Cache { 35 return &Cache{ 36 output: output, 37 config: cfg, 38 confirm: confirm, 39 path: storage.CachePath(), 40 ipComm: ipComm, 41 } 42 } 43 44 func (c *Cache) Run(params *CacheParams) error { 45 if os.Getenv(constants.ActivatedStateEnvVarName) != "" { 46 return locale.NewError("err_clean_cache_activated") 47 } 48 49 if params.Project != "" { 50 paths := projectfile.GetProjectPaths(c.config, params.Project) 51 52 if len(paths) == 0 { 53 return locale.NewInputError("err_cache_no_project", "Could not determine path to project {{.V0}}", params.Project) 54 } 55 56 for _, projectPath := range paths { 57 err := c.removeProjectCache(projectPath, params.Project, params.Force) 58 if err != nil { 59 return err 60 } 61 } 62 return nil 63 } 64 65 return c.removeCache(c.path, params.Force) 66 } 67 68 func (c *Cache) removeCache(path string, force bool) error { 69 if !force { 70 ok, err := c.confirm.Confirm(locale.T("confirm"), locale.T("clean_cache_confirm"), new(bool)) 71 if err != nil { 72 return err 73 } 74 if !ok { 75 return locale.NewInputError("err_clean_cache_not_confirmed", "Cleaning of cache aborted by user") 76 } 77 } 78 79 logging.Debug("Removing cache path: %s", path) 80 err := removeCache(c.path) 81 if err != nil { 82 return errs.Wrap(err, "Failed to remove cache") 83 } 84 85 c.output.Notice(locale.Tl("clean_cache_success_message", "Successfully cleaned cache.")) 86 return nil 87 } 88 89 func (c *Cache) removeProjectCache(projectDir, namespace string, force bool) error { 90 if !force { 91 ok, err := c.confirm.Confirm(locale.T("confirm"), locale.Tr("clean_cache_artifact_confirm", namespace), new(bool)) 92 if err != nil { 93 return err 94 } 95 if !ok { 96 return locale.NewInputError("err_clean_cache_artifact_not_confirmed", "Cleaning of cached runtime aborted by user") 97 } 98 } 99 100 projectInstallPath := target.ProjectDirToTargetDir(projectDir, storage.CachePath()) 101 logging.Debug("Remove project path: %s", projectInstallPath) 102 err := os.RemoveAll(projectInstallPath) 103 if err != nil { 104 return locale.WrapError(err, "err_clean_remove_artifact", "Could not remove cached runtime environment for project: {{.V0}}", namespace) 105 } 106 107 return nil 108 }