about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-11 14:41:00 +0200
committerAlan Pearce2024-05-11 14:41:00 +0200
commitdbe952d1df63522e5c32b4ef3e59c43886ba9257 (patch)
tree36fecbc0a1d53e8f8437ace333dc848e65cbc80e
parent1aa991ae1f1a426424549c92060b204114c8b3c2 (diff)
downloadsearchix-dbe952d1df63522e5c32b4ef3e59c43886ba9257.tar.lz
searchix-dbe952d1df63522e5c32b4ef3e59c43886ba9257.tar.zst
searchix-dbe952d1df63522e5c32b4ef3e59c43886ba9257.zip
refactor: move sources input data to $data_root/sources
-rw-r--r--import/main.go6
-rw-r--r--internal/search/indexer.go24
2 files changed, 27 insertions, 3 deletions
diff --git a/import/main.go b/import/main.go
index 90f2b74..0bff7ab 100644
--- a/import/main.go
+++ b/import/main.go
@@ -64,11 +64,13 @@ func main() {
 		logger := slog.With("name", name, "importer", source.Type.String())
 		logger.Debug("starting importer")
 
+		importerDataPath := path.Join(cfg.DataPath, "sources", source.Channel)
+
 		switch source.Type {
 		case importer.ChannelNixpkgs:
-			imp = importer.NewNixpkgsChannelImporter(source, cfg.DataPath, logger)
+			imp = importer.NewNixpkgsChannelImporter(source, importerDataPath, logger)
 		case importer.Channel:
-			imp = importer.NewChannelImporter(source, cfg.DataPath, logger)
+			imp = importer.NewChannelImporter(source, importerDataPath, logger)
 		default:
 			log.Printf("unsupported importer type %s", source.Type.String())
 
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index 87cb12f..ea262ee 100644
--- a/internal/search/indexer.go
+++ b/internal/search/indexer.go
@@ -4,12 +4,14 @@ import (
 	"bytes"
 	"context"
 	"encoding/gob"
+	"io/fs"
 	"log"
 	"log/slog"
 	"os"
 	"path"
 	"searchix/internal/file"
 	"searchix/internal/options"
+	"slices"
 
 	"github.com/blevesearch/bleve/v2"
 	"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
@@ -125,6 +127,11 @@ func createIndex(indexPath string) (bleve.Index, error) {
 
 const indexBaseName = "index.bleve"
 
+var expectedDataFiles = []string{
+	indexBaseName,
+	"sources",
+}
+
 func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) {
 	var err error
 	bleve.SetLog(log.Default())
@@ -143,7 +150,22 @@ func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) {
 	var idx bleve.Index
 	if !exists || force {
 		if force {
-			err := os.RemoveAll(indexPath)
+			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)
 			if err != nil {
 				return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath)
 			}