about summary refs log tree commit diff stats
path: root/internal/search
diff options
context:
space:
mode:
Diffstat (limited to 'internal/search')
-rw-r--r--internal/search/indexer.go29
-rw-r--r--internal/search/search.go6
2 files changed, 24 insertions, 11 deletions
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index fc0214b..ad891ff 100644
--- a/internal/search/indexer.go
+++ b/internal/search/indexer.go
@@ -29,19 +29,33 @@ type WriteIndex struct {
 	indexMapping *mapping.IndexMappingImpl
 }
 
-func NewIndexer(dir string, force bool) (*WriteIndex, error) {
+const ExpectedIndexExtension = ".bleve"
+
+func NewIndexer(indexPath 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)
+		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)
+			return nil, errors.Errorf(
+				"index file %s already exists (use --force to replace)",
+				indexPath,
+			)
+		}
+		if path.Ext(indexPath) != ExpectedIndexExtension {
+			return nil, errors.Errorf(
+				"cowardly refusing to delete path %s (it doesn't end in '%s')",
+				indexPath,
+				ExpectedIndexExtension,
+			)
 		}
 		err := os.RemoveAll(indexPath)
 		if err != nil {
@@ -130,7 +144,10 @@ func NewIndexer(dir string, force bool) (*WriteIndex, error) {
 	}, nil
 }
 
-func (i *WriteIndex) ImportOptions(ctx context.Context, objects <-chan *options.NixOption) <-chan error {
+func (i *WriteIndex) ImportOptions(
+	ctx context.Context,
+	objects <-chan *options.NixOption,
+) <-chan error {
 	var err error
 	errs := make(chan error)
 
diff --git a/internal/search/search.go b/internal/search/search.go
index 92afdfb..9766294 100644
--- a/internal/search/search.go
+++ b/internal/search/search.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"context"
 	"encoding/gob"
-	"path"
 	"searchix/internal/options"
 
 	"github.com/blevesearch/bleve/v2"
@@ -13,7 +12,6 @@ import (
 )
 
 const ResultsPerPage = 20
-const indexFilename = "index.bleve"
 
 type DocumentMatch struct {
 	search.DocumentMatch
@@ -29,9 +27,7 @@ type ReadIndex struct {
 	index bleve.Index
 }
 
-func Open(dir string) (*ReadIndex, error) {
-	indexPath := path.Join(dir, indexFilename)
-
+func Open(indexPath string) (*ReadIndex, error) {
 	idx, err := bleve.Open(indexPath)
 	if err != nil {
 		return nil, errors.WithMessagef(err, "unable to open index at path %s", indexPath)