refactor: unify cache implementations (#6977)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Teppei Fukuda
2024-06-21 10:35:33 +04:00
committed by GitHub
parent 9dc8a2ba6b
commit 6dff4223ed
65 changed files with 481 additions and 962 deletions

View File

@@ -6,12 +6,12 @@ import (
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy/pkg/cache"
"github.com/aquasecurity/trivy/pkg/commands/operation"
"github.com/aquasecurity/trivy/pkg/flag"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/module"
rpcServer "github.com/aquasecurity/trivy/pkg/rpc/server"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)
// Run runs the scan
@@ -19,16 +19,16 @@ func Run(ctx context.Context, opts flag.Options) (err error) {
log.InitLogger(opts.Debug, opts.Quiet)
// configure cache dir
fsutils.SetCacheDir(opts.CacheDir)
cache, err := operation.NewCache(opts.CacheOptions)
cache.SetDir(opts.CacheDir)
cacheClient, err := cache.NewClient(opts.CacheOptions.CacheBackendOptions)
if err != nil {
return xerrors.Errorf("server cache error: %w", err)
}
defer cache.Close()
log.Debug("Cache", log.String("dir", fsutils.CacheDir()))
defer cacheClient.Close()
log.Debug("Cache", log.String("dir", cache.Dir()))
if opts.Reset {
return cache.ClearDB()
return cacheClient.ClearDB()
}
// download the database file
@@ -57,5 +57,5 @@ func Run(ctx context.Context, opts flag.Options) (err error) {
server := rpcServer.NewServer(opts.AppVersion, opts.Listen, opts.CacheDir, opts.Token, opts.TokenHeader,
opts.DBRepository, opts.RegistryOpts())
return server.ListenAndServe(ctx, cache, opts.SkipDBUpdate)
return server.ListenAndServe(ctx, cacheClient, opts.SkipDBUpdate)
}