about summary refs log tree commit diff stats
path: root/internal/search
diff options
context:
space:
mode:
authorAlan Pearce2024-05-12 14:37:59 +0200
committerAlan Pearce2024-05-12 14:37:59 +0200
commit4f5be8a89755dfdece7b2ec60c90d74bfde1b707 (patch)
treec4cd57dcf3ca30aeb2558161ae0d572d72c00f51 /internal/search
parentef50bbf934a3024fdb82c31f5dc38e017603a86f (diff)
downloadsearchix-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.go43
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)