diff options
Diffstat (limited to 'cache/cache.go')
| -rw-r--r-- | cache/cache.go | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/cache/cache.go b/cache/cache.go index 9f25af0..7121f65 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -54,12 +54,12 @@ func (e Entry) String() string { // LoadFile creates a Cache from a file or an empty one if the file does not exist // this handles opening a reader for Unmarshal -func LoadFile(root fs.FS, path string) (*Cache, error) { +func LoadFile(root fs.FS, path string) (Cache, error) { fd, err := root.Open(path) if errors.Is(err, fs.ErrNotExist) { - return nil, nil + return Cache{}, nil } else if err != nil { - return nil, err + return Cache{}, err } if fd != nil { defer fd.Close() @@ -68,7 +68,7 @@ func LoadFile(root fs.FS, path string) (*Cache, error) { } // Unmarshal attempts to create a Cache from reader content -func Unmarshal(r io.Reader) (*Cache, error) { +func Unmarshal(r io.Reader) (Cache, error) { c := &Cache{} scanner := bufio.NewScanner(r) for scanner.Scan() { @@ -88,7 +88,7 @@ func Unmarshal(r io.Reader) (*Cache, error) { } c.Entries = append(c.Entries, *entry) } - return c, nil + return *c, nil } // Marshal returns the file representation of the Cache @@ -119,16 +119,13 @@ func New(st *model.State) *Cache { // MergeAndSave creates a cache from a state, combines it with // a potential saved context file, and writes it to disk func MergeAndSave(in *model.State) error { - root := "/" - f := os.DirFS(root) - // remove leading slash from the dirfs - loaded, err := LoadFile(f, strings.TrimPrefix(paths.ContextsFile.String(), "/")) + loaded, err := Get() if err != nil { return err } insert := New(in) result := loaded.Merge(insert) - if loaded == nil { + if loaded.Entries == nil { return SaveFile(paths.ContextsFile.String(), result) } if slices.Equal(loaded.Entries, result.Entries) { @@ -157,12 +154,12 @@ func Save(w io.Writer, loaded *Cache) error { } // LoadState creates a state with model.NewState based on cache content -func LoadState(f fs.FS, cache *Cache, indexers []model.Indexer, runners []model.Runner) (*model.State, []error) { +func LoadState(f fs.FS, data map[string]*model.SourceData, cache Cache, indexers []model.Indexer, runners []model.Runner) (*model.State, []error) { var locs []string for _, e := range cache.Entries { locs = append(locs, e.Path) } - return model.NewState(f, locs, indexers, runners) + return model.NewState(f, locs, data, indexers, runners) } // Strip removes the needle's entries from the receiver's entries when they have matching paths. @@ -182,3 +179,9 @@ outer: Entries: result, } } + +func Get() (Cache, error) { + f := os.DirFS(paths.CacheDir.String()) + loaded, err := LoadFile(f, strings.TrimPrefix(paths.ContextsFileName, "/")) + return loaded, err +} |
