about summary refs log tree commit diff stats
path: root/internal/search/indexer.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-09 18:39:45 +0200
committerAlan Pearce2024-05-09 19:27:57 +0200
commit778e1d774f574f45c179d0fecf0c1da9cc359b80 (patch)
tree51d7a2c1fa6c29dc3782c10c7df40545ef694710 /internal/search/indexer.go
parent99d5507b223baa6c780707d32c7bbbb8611034f0 (diff)
downloadsearchix-778e1d774f574f45c179d0fecf0c1da9cc359b80.tar.lz
searchix-778e1d774f574f45c179d0fecf0c1da9cc359b80.tar.zst
searchix-778e1d774f574f45c179d0fecf0c1da9cc359b80.zip
feat(importer): add --replace flag to overwrite existing index
Diffstat (limited to 'internal/search/indexer.go')
-rw-r--r--internal/search/indexer.go18
1 files changed, 17 insertions, 1 deletions
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index b0e57d4..fc0214b 100644
--- a/internal/search/indexer.go
+++ b/internal/search/indexer.go
@@ -6,7 +6,9 @@ import (
 	"encoding/gob"
 	"log"
 	"log/slog"
+	"os"
 	"path"
+	"searchix/internal/file"
 	"searchix/internal/options"
 
 	"github.com/blevesearch/bleve/v2"
@@ -27,12 +29,26 @@ type WriteIndex struct {
 	indexMapping *mapping.IndexMappingImpl
 }
 
-func NewIndexer(dir string) (*WriteIndex, error) {
+func NewIndexer(dir string, force bool) (*WriteIndex, error) {
 	var err error
 	bleve.SetLog(log.Default())
 
 	indexPath := path.Join(dir, indexFilename)
 
+	exists, err := file.Exists(indexPath)
+	if err != nil {
+		return nil, errors.WithMessagef(err, "could not check if index exists at path %s", indexPath)
+	}
+	if exists {
+		if !force {
+			return nil, errors.Errorf("index file %s already exists (use --force to replace)", indexPath)
+		}
+		err := os.RemoveAll(indexPath)
+		if err != nil {
+			return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath)
+		}
+	}
+
 	indexMapping := bleve.NewIndexMapping()
 	indexMapping.StoreDynamic = false
 	indexMapping.IndexDynamic = false