diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 7ea1f3fd5..938dc9cd6 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -51,7 +51,8 @@ type backend interface { } type CacheOptions struct { - Log *logrus.Entry + Log *logrus.Entry + Format string } func WithLog(log *logrus.Entry) CacheOption { @@ -60,6 +61,12 @@ func WithLog(log *logrus.Entry) CacheOption { } } +func WithFormat(format string) CacheOption { + return func(o *CacheOptions) { + o.Format = format + } +} + type CacheOption func(*CacheOptions) // New creates a new Cache. It chooses a cache implementation based @@ -73,7 +80,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) { for _, opt := range cacheOpts { opt(opts) } - cacheBackend, err := getDefaultBackend(cacheDir, opts.Log) + cacheBackend, err := getBackend(cacheDir, opts.Format, opts.Log) if err != nil { return nil, err } @@ -84,7 +91,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) { return &cache{backend: cacheBackend, log: opts.Log}, nil } -func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) { +func getBackend(cacheDir string, backendName string, log *logrus.Entry) (backend, error) { entries, err := os.ReadDir(cacheDir) if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("detect cache format: read cache directory: %v", err) @@ -96,12 +103,21 @@ func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) { } if len(entries) == 0 { - log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend") - return backends[0], nil + if backendName == "" { + log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend") + return backends[0], nil + } + for _, b := range backends { + if b.Name() == backendName { + log.WithField("backend", backendName).Info("using preferred backend") + return b, nil + } + } + return nil, fmt.Errorf("preferred backend %q not found", backendName) } for _, backend := range backends { - if backend.IsCachePresent() { + if (backendName == "" || backend.Name() == backendName) && backend.IsCachePresent() { log.WithField("backend", backend.Name()).Info("found existing cache contents") return backend, nil } diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index 7a779719d..47a156fed 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -222,9 +222,11 @@ func TestCache_ListPackages(t *testing.T) { func genTestCaches(t *testing.T, fbcFS fs.FS) map[string]Cache { t.Helper() - caches := map[string]Cache{ - "json": &cache{backend: newJSONBackend(t.TempDir()), log: log.Null()}, - "pogreb.v1": &cache{backend: newPogrebV1Backend(t.TempDir()), log: log.Null()}, + caches := make(map[string]Cache) + for _, format := range []string{FormatJSON, FormatPogrebV1} { + c, err := New(t.TempDir(), WithFormat(format), WithLog(log.Null())) + require.NoError(t, err) + caches[format] = c } for _, c := range caches { diff --git a/pkg/cache/json.go b/pkg/cache/json.go index 7ea599218..fc73431e9 100644 --- a/pkg/cache/json.go +++ b/pkg/cache/json.go @@ -40,8 +40,10 @@ type jsonBackend struct { bundles bundleKeys } +const FormatJSON = "json" + func (q *jsonBackend) Name() string { - return "json" + return FormatJSON } func (q *jsonBackend) IsCachePresent() bool { diff --git a/pkg/cache/pogrebv1.go b/pkg/cache/pogrebv1.go index af5b35ddf..a340b4458 100644 --- a/pkg/cache/pogrebv1.go +++ b/pkg/cache/pogrebv1.go @@ -31,10 +31,12 @@ func newPogrebV1Backend(baseDir string) *pogrebV1Backend { } const ( + FormatPogrebV1 = "pogreb.v1" + pogrebV1CacheModeDir = 0770 pogrebV1CacheModeFile = 0660 - pograbV1CacheDir = "pogreb.v1" + pograbV1CacheDir = FormatPogrebV1 pogrebDigestFile = pograbV1CacheDir + "/digest" pogrebDbDir = pograbV1CacheDir + "/db" ) @@ -46,7 +48,7 @@ type pogrebV1Backend struct { } func (q *pogrebV1Backend) Name() string { - return pograbV1CacheDir + return FormatPogrebV1 } func (q *pogrebV1Backend) IsCachePresent() bool {