github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/clean/config.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/locale" 9 "github.com/ActiveState/cli/internal/logging" 10 "github.com/ActiveState/cli/internal/output" 11 "github.com/ActiveState/cli/internal/rtutils/ptr" 12 "github.com/ActiveState/cli/internal/svcctl" 13 "github.com/ActiveState/cli/pkg/project" 14 ) 15 16 type configurable interface { 17 project.ConfigAble 18 ConfigPath() string 19 GetInt(string) int 20 Set(string, interface{}) error 21 GetStringMap(string) map[string]interface{} 22 GetBool(string) bool 23 GetString(string) string 24 } 25 26 type Config struct { 27 output output.Outputer 28 confirm promptable 29 cfg configurable 30 ipComm svcctl.IPCommunicator 31 } 32 33 type ConfigParams struct { 34 Force bool 35 } 36 37 func NewConfig(prime primeable) *Config { 38 return newConfig(prime.Output(), prime.Prompt(), prime.Config(), prime.IPComm()) 39 } 40 41 func newConfig(out output.Outputer, confirm promptable, cfg configurable, ipComm svcctl.IPCommunicator) *Config { 42 return &Config{ 43 output: out, 44 confirm: confirm, 45 cfg: cfg, 46 ipComm: ipComm, 47 } 48 } 49 50 func (c *Config) Run(params *ConfigParams) error { 51 if os.Getenv(constants.ActivatedStateEnvVarName) != "" { 52 return locale.NewError("err_clean_cache_activated") 53 } 54 55 if !params.Force { 56 ok, err := c.confirm.Confirm(locale.T("confirm"), locale.T("clean_config_confirm"), ptr.To(true)) 57 if err != nil { 58 return locale.WrapError(err, "err_clean_config_confirm", "Could not confirm clean config choice") 59 } 60 if !ok { 61 return locale.NewInputError("err_clean_config_aborted", "Cleaning of config aborted by user") 62 } 63 } 64 65 if err := stopServices(c.cfg, c.output, c.ipComm, params.Force); err != nil { 66 return errs.Wrap(err, "Failed to stop services.") 67 } 68 69 dir := c.cfg.ConfigPath() 70 c.cfg.Close() 71 72 logging.Debug("Removing config directory: %s", dir) 73 return removeConfig(dir, c.output) 74 }