diff options
author | Alan Pearce | 2024-05-12 14:37:59 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-12 14:37:59 +0200 |
commit | 4f5be8a89755dfdece7b2ec60c90d74bfde1b707 (patch) | |
tree | c4cd57dcf3ca30aeb2558161ae0d572d72c00f51 /internal/search | |
parent | ef50bbf934a3024fdb82c31f5dc38e017603a86f (diff) | |
download | searchix-4f5be8a89755dfdece7b2ec60c90d74bfde1b707.tar.lz searchix-4f5be8a89755dfdece7b2ec60c90d74bfde1b707.tar.zst searchix-4f5be8a89755dfdece7b2ec60c90d74bfde1b707.zip |
refactor: extract index deletion function
Diffstat (limited to 'internal/search')
-rw-r--r-- | internal/search/indexer.go | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/internal/search/indexer.go b/internal/search/indexer.go index e2e23c2..a74189e 100644 --- a/internal/search/indexer.go +++ b/internal/search/indexer.go @@ -137,6 +137,30 @@ var expectedDataFiles = []string{ "sources", } +func deleteIndex(dataRoot string) error { + dir, err := os.ReadDir(dataRoot) + if err != nil { + return errors.WithMessagef(err, "could not read data directory %s", dataRoot) + } + remainingFiles := slices.DeleteFunc(dir, func(e fs.DirEntry) bool { + return slices.Contains(expectedDataFiles, e.Name()) + }) + if len(remainingFiles) > 0 { + return errors.Errorf( + "cowardly refusing to remove data directory %s as it contains unknown files: %v", + dataRoot, + remainingFiles, + ) + } + + err = os.RemoveAll(dataRoot) + if err != nil { + return errors.WithMessagef(err, "could not remove data directory %s", dataRoot) + } + + return nil +} + func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) { var err error bleve.SetLog(log.Default()) @@ -157,24 +181,9 @@ func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) { var meta *IndexMeta if !exists || force { if force { - dir, err := os.ReadDir(dataRoot) - if err != nil { - return nil, errors.WithMessagef(err, "could not read data directory %s", dataRoot) - } - remainingFiles := slices.DeleteFunc(dir, func(e fs.DirEntry) bool { - return slices.Contains(expectedDataFiles, e.Name()) - }) - if len(remainingFiles) > 0 { - return nil, errors.Errorf( - "cowardly refusing to remove data directory %s as it contains unknown files: %v", - dataRoot, - remainingFiles, - ) - } - - err = os.RemoveAll(dataRoot) + err = deleteIndex(dataRoot) if err != nil { - return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath) + return nil, err } } idx, err = createIndex(indexPath) |