Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clear cache command #702

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func initializeHandler(srv *jrpc2.Server) handler.Func {
types.CodeSubmitFixFeedback,
types.CodeFixDiffsCommand,
types.ExecuteCLICommand,
types.ClearCacheCommand,
},
},
},
Expand Down
90 changes: 90 additions & 0 deletions domain/ide/command/clear_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* © 2023 Snyk Limited
ShawkyZ marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package command

import (
"context"
"github.com/rs/zerolog"
"github.com/snyk/snyk-ls/application/config"
"github.com/snyk/snyk-ls/domain/ide/workspace"
"github.com/snyk/snyk-ls/internal/types"
"github.com/sourcegraph/go-lsp"
"net/url"
)

type clearCache struct {
command types.CommandData
}

func (cmd *clearCache) Command() types.CommandData {
return cmd.command
}

// Execute Deletes persisted, inMemory Cache or both.
// Parameters: folderUri either folder Uri or nil for all folders
// cacheType: either inMemory or persisted or empty for both.
func (cmd *clearCache) Execute(_ context.Context) (any, error) {
logger := config.CurrentConfig().Logger().With().Str("method", "clearCache.Execute").Logger()
args := cmd.command.Arguments
var parsedFolderUri *lsp.DocumentURI
folderURI := args[0].(string)

if folderURI != "" {
decodedPath, err := url.PathUnescape(folderURI)
if err == nil {
uri := lsp.DocumentURI(decodedPath)
ShawkyZ marked this conversation as resolved.
Show resolved Hide resolved
parsedFolderUri = &uri
}
}

cacheType := args[1].(string)

if cacheType == "" {
cmd.purgeInMemoryCache(&logger, parsedFolderUri)
cmd.purgePersistedCache(&logger, parsedFolderUri)
} else if cacheType == "inMemory" {
cmd.purgeInMemoryCache(&logger, parsedFolderUri)
} else if cacheType == "persisted" {
cmd.purgePersistedCache(&logger, parsedFolderUri)
}

return nil, nil
}

func (cmd *clearCache) purgeInMemoryCache(logger *zerolog.Logger, folderUri *lsp.DocumentURI) {
ws := workspace.Get()
for _, folder := range ws.Folders() {
if folderUri != nil && *folderUri != folder.Uri() {
continue
}
logger.Info().Msgf("deleting in-memory cache for folder %s", folder.Path())
folder.Clear()
}
}

func (cmd *clearCache) purgePersistedCache(logger *zerolog.Logger, folderUri *lsp.DocumentURI) {
var folderList []string
ws := workspace.Get()
for _, folder := range ws.Folders() {
if folderUri != nil && *folderUri != folder.Uri() {
continue
}
folderList = append(folderList, folder.Path())
}
logger.Info().Msgf("deleting perrsisted cache for folders %v", folderList)
ws.ScanPersister().Clear(folderList, false)
}
3 changes: 2 additions & 1 deletion domain/ide/command/command_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package command

import (
"fmt"

"github.com/snyk/snyk-ls/application/config"
"github.com/snyk/snyk-ls/domain/snyk"
"github.com/snyk/snyk-ls/infrastructure/authentication"
Expand Down Expand Up @@ -90,6 +89,8 @@ func CreateFromCommandData(
}, nil
case types.ExecuteCLICommand:
return &executeCLICommand{command: commandData, authService: authService, notifier: notifier, logger: c.Logger(), cli: cli}, nil
case types.ClearCacheCommand:
return &clearCache{command: commandData}, nil
}

return nil, fmt.Errorf("unknown command %v", commandData)
Expand Down
3 changes: 3 additions & 0 deletions domain/ide/workspace/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/sourcegraph/go-lsp"
"strings"
"sync"

Expand Down Expand Up @@ -646,6 +647,8 @@ func (f *Folder) sendHoversForFile(path string, issues []snyk.Issue) {

func (f *Folder) Path() string { return f.path }

func (f *Folder) Uri() lsp.DocumentURI { return uri.PathToUri(f.path) }

func (f *Folder) Name() string { return f.name }

func (f *Folder) Status() FolderStatus { return f.status }
Expand Down
4 changes: 4 additions & 0 deletions domain/ide/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func Set(w *Workspace) {
instance = w
}

func (w *Workspace) ScanPersister() persistence.ScanSnapshotPersister {
return w.scanPersister
}

func (w *Workspace) RemoveFolder(folderPath string) {
w.mutex.Lock()
defer w.mutex.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion domain/snyk/persistence/git_persistence_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type hashedFolderPath string

type ScanSnapshotPersister interface {
Init(folderPath []string) error
Clear(folderPath []string, deleteIfExpired bool)
Clear(folderPath []string, deleteOnlyExpired bool)
Add(folderPath, commitHash string, issueList []snyk.Issue, p product.Product) error
GetPersistedIssueList(folderPath string, p product.Product) ([]snyk.Issue, error)
Exists(folderPath, commitHash string, p product.Product) bool
Expand Down
1 change: 1 addition & 0 deletions internal/types/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
GetActiveUserCommand = "snyk.getActiveUser"
ReportAnalyticsCommand = "snyk.reportAnalytics"
ExecuteCLICommand = "snyk.executeCLI"
ClearCacheCommand = "snyk.clearCache"

// Snyk Code specific commands
CodeFixCommand = "snyk.code.fix"
Expand Down