diff --git a/internal/app/bridge.go b/internal/app/bridge.go index 37b437b6..6ce1f0d3 100644 --- a/internal/app/bridge.go +++ b/internal/app/bridge.go @@ -41,6 +41,9 @@ import ( const vaultSecretName = "bridge-vault-key" +// deleteOldGoIMAPFiles Set with `-ldflags -X app.deleteOldGoIMAPFiles=true` to enable cleanup of old imap cache data. +var deleteOldGoIMAPFiles bool //nolint:gochecknoglobals + // withBridge creates creates and tears down the bridge. func withBridge( //nolint:funlen c *cli.Context, @@ -61,6 +64,13 @@ func withBridge( //nolint:funlen dialer.NewTLSPinChecker(dialer.TrustedAPIPins), ) + // Delete old go-imap cache files + if deleteOldGoIMAPFiles { + if err := locations.CleanGoIMAPCache(); err != nil { + logrus.WithError(err).Error("Failed to remove old go-imap cache") + } + } + // Create a proxy dialer which switches to a proxy if the request fails. proxyDialer := dialer.NewProxyTLSDialer(pinningDialer, constants.APIHost) diff --git a/internal/locations/locations.go b/internal/locations/locations.go index f71bbe56..b2c7f315 100644 --- a/internal/locations/locations.go +++ b/internal/locations/locations.go @@ -193,6 +193,10 @@ func (l *Locations) getLogsPath() string { return filepath.Join(l.userCache, "logs") } +func (l *Locations) getGoIMAPCachePath() string { + return filepath.Join(l.userConfig, "cache") +} + func (l *Locations) getUpdatesPath() string { // In order to properly update Bridge 1.6.X and higher we need to // change the launcher first. Since this is not part of automatic @@ -235,3 +239,8 @@ func (l *Locations) Clean() error { l.getGluonPath(), ).Do() } + +// CleanGoIMAPCache removes all cache data from the go-imap implementation. +func (l *Locations) CleanGoIMAPCache() error { + return files.Remove(l.getGoIMAPCachePath()).Do() +} diff --git a/internal/locations/locations_test.go b/internal/locations/locations_test.go index ca147beb..096513dd 100644 --- a/internal/locations/locations_test.go +++ b/internal/locations/locations_test.go @@ -108,6 +108,29 @@ func TestCleanRemovesUnexpectedFilesAndFolders(t *testing.T) { assert.NoFileExists(t, filepath.Join(l.userCache, "dir3", "dir4", "unexpected5.txt")) } +func TestRemoveOldGoIMAPCacheFolders(t *testing.T) { + l := newTestLocations(t) + + createFilesInDir(t, + l.getGoIMAPCachePath(), + "foo", + "bar", + ) + + require.FileExists(t, filepath.Join(l.getGoIMAPCachePath(), "foo")) + require.FileExists(t, filepath.Join(l.getGoIMAPCachePath(), "bar")) + + assert.NoError(t, l.CleanGoIMAPCache()) + + assert.DirExists(t, l.getSettingsPath()) + assert.DirExists(t, l.getLogsPath()) + assert.DirExists(t, l.getUpdatesPath()) + + assert.NoFileExists(t, filepath.Join(l.getGoIMAPCachePath(), "foo")) + assert.NoFileExists(t, filepath.Join(l.getGoIMAPCachePath(), "bar")) + assert.NoDirExists(t, l.getGoIMAPCachePath()) +} + func newFakeAppDirs(t *testing.T) *fakeAppDirs { return &fakeAppDirs{ configDir: t.TempDir(),