From 29af8e7178058a2542feea97a5486c348db41ea5 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Thu, 16 Sep 2021 20:22:16 +0200 Subject: [PATCH] GODT-1349: Change cache-related settings when enabling/disabling/moving cache --- internal/bridge/bridge.go | 33 +++++++++++++++++++++++++++++++++ internal/frontend/cli/system.go | 6 ------ internal/users/users.go | 24 ++++++++++++------------ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go index 398ae830..d294f62b 100644 --- a/internal/bridge/bridge.go +++ b/internal/bridge/bridge.go @@ -204,3 +204,36 @@ func (b *Bridge) GetKeychainApp() string { func (b *Bridge) SetKeychainApp(helper string) { b.settings.Set(settings.PreferredKeychainKey, helper) } + +func (b *Bridge) EnableCache() error { + // Set this back to the default location before enabling. + b.settings.Set(settings.CacheLocationKey, "") + + if err := b.Users.EnableCache(); err != nil { + return err + } + + b.settings.SetBool(settings.CacheEnabledKey, true) + + return nil +} + +func (b *Bridge) DisableCache() error { + if err := b.Users.DisableCache(); err != nil { + return err + } + + b.settings.SetBool(settings.CacheEnabledKey, false) + + return nil +} + +func (b *Bridge) MigrateCache(from, to string) error { + if err := b.Users.MigrateCache(from, to); err != nil { + return err + } + + b.settings.Set(settings.CacheLocationKey, to) + + return nil +} diff --git a/internal/frontend/cli/system.go b/internal/frontend/cli/system.go index c2643c72..092ccfea 100644 --- a/internal/frontend/cli/system.go +++ b/internal/frontend/cli/system.go @@ -163,15 +163,11 @@ func (f *frontendCLI) enableCacheOnDisk(c *ishell.Context) { } if f.yesNoQuestion("Are you sure you want to enable the local cache") { - // Set this back to the default location before enabling. - f.settings.Set(settings.CacheLocationKey, "") - if err := f.bridge.EnableCache(); err != nil { f.Println("The local cache could not be enabled.") return } - f.settings.SetBool(settings.CacheEnabledKey, true) f.restarter.SetToRestart() f.Stop() } @@ -189,7 +185,6 @@ func (f *frontendCLI) disableCacheOnDisk(c *ishell.Context) { return } - f.settings.SetBool(settings.CacheEnabledKey, false) f.restarter.SetToRestart() f.Stop() } @@ -211,7 +206,6 @@ func (f *frontendCLI) setCacheOnDiskLocation(c *ishell.Context) { return } - f.settings.Set(settings.CacheLocationKey, location) f.restarter.SetToRestart() f.Stop() } diff --git a/internal/users/users.go b/internal/users/users.go index b6b21c63..5dfb5c87 100644 --- a/internal/users/users.go +++ b/internal/users/users.go @@ -434,6 +434,18 @@ func (u *Users) EnableCache() error { return nil } +func (u *Users) DisableCache() error { + // NOTE(GODT-1158): Is it an error if we can't remove a user's cache? + + for _, user := range u.users { + if err := user.store.RemoveCache(); err != nil { + logrus.WithError(err).Error("Failed to remove user's message cache") + } + } + + return nil +} + func (u *Users) MigrateCache(from, to string) error { // NOTE(GODT-1158): Is it enough to just close the store? Do we need to force-close the cacher too? @@ -451,18 +463,6 @@ func (u *Users) MigrateCache(from, to string) error { return os.Rename(from, to) } -func (u *Users) DisableCache() error { - // NOTE(GODT-1158): Is it an error if we can't remove a user's cache? - - for _, user := range u.users { - if err := user.store.RemoveCache(); err != nil { - logrus.WithError(err).Error("Failed to remove user's message cache") - } - } - - return nil -} - // hasUser returns whether the struct currently has a user with ID `id`. func (u *Users) hasUser(id string) (user *User, ok bool) { for _, u := range u.users {