feat(importer): add --replace flag to overwrite existing index
1 file changed, 17 insertions(+), 1 deletion(-)
changed files
M internal/search/indexer.go → internal/search/indexer.go
@@ -6,7 +6,9 @@ "context" "encoding/gob" "log" "log/slog" + "os" "path" + "searchix/internal/file" "searchix/internal/options" "github.com/blevesearch/bleve/v2"@@ -27,11 +29,25 @@ index bleve.Index 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