refactor: extract index deletion function
Alan Pearce alan@alanpearce.eu
Sun, 12 May 2024 14:37:59 +0200
1 files changed, 26 insertions(+), 17 deletions(-)
jump to
M internal/search/indexer.go → internal/search/indexer.go
@@ -137,6 +137,30 @@ indexBaseName, "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 @@ var idx bleve.Index var meta *IndexMeta if !exists || force { if force { - dir, err := os.ReadDir(dataRoot) + err = deleteIndex(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) - if err != nil { - return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath) + return nil, err } } idx, err = createIndex(indexPath)