all repos — searchix @ 778e1d774f574f45c179d0fecf0c1da9cc359b80

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat(importer): add --replace flag to overwrite existing index

Alan Pearce
commit

778e1d774f574f45c179d0fecf0c1da9cc359b80

parent

99d5507b223baa6c780707d32c7bbbb8611034f0

2 files changed, 20 insertions(+), 3 deletions(-)

jump to
M import/main.goimport/main.go
@@ -22,6 +22,7 @@
type Config struct { ConfigFile string `conf:"short:c"` LogLevel slog.Level `conf:"default:INFO"` + Replace bool `conf:"default:false,help:whether to remove existing database, if exists"` } func main() {
@@ -50,7 +51,7 @@
return } - indexer, err := search.NewIndexer(cfg.DataPath) + indexer, err := search.NewIndexer(cfg.DataPath, runtimeConfig.Replace) if err != nil { log.Fatalf("Failed to create indexer: %v", err) }
@@ -93,7 +94,7 @@ continue
} logger.Info("importer fetch succeeded", "updated", updated) - if updated { + if updated || runtimeConfig.Replace { hadWarnings, err := imp.Import(ctx, indexer) if err != nil {
M internal/search/indexer.gointernal/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