Skip to content

chore: simplify cache Option type #187

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

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 3 additions & 8 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

// Option is an option to change some aspect of default CDI behavior.
type Option func(*Cache) error
type Option func(*Cache)

// Cache stores CDI Specs loaded from Spec directories.
type Cache struct {
Expand All @@ -54,9 +54,8 @@ type Cache struct {
// is detected. This option can be used to disable this behavior when a
// manually refreshed mode is preferable.
func WithAutoRefresh(autoRefresh bool) Option {
return func(c *Cache) error {
return func(c *Cache) {
c.autoRefresh = autoRefresh
return nil
}
}

Expand Down Expand Up @@ -92,12 +91,8 @@ func (c *Cache) Configure(options ...Option) error {
// Configure the Cache. Start/stop CDI Spec directory watch, refresh
// the Cache if necessary.
func (c *Cache) configure(options ...Option) error {
var err error

for _, o := range options {
if err = o(c); err != nil {
return fmt.Errorf("failed to apply cache options: %w", err)
}
o(c)
}

c.dirErrors = make(map[string]error)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cdi/spec-dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ var (

// WithSpecDirs returns an option to override the CDI Spec directories.
func WithSpecDirs(dirs ...string) Option {
return func(c *Cache) error {
return func(c *Cache) {
specDirs := make([]string, len(dirs))
for i, dir := range dirs {
specDirs[i] = filepath.Clean(dir)
}
c.specDirs = specDirs
return nil
}
}

Expand Down