From 6b40e0c0fa90f11be14a93f1d6275779fd645cac Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 13 May 2024 21:50:14 +0200 Subject: refactor: combine import and web server into one binary --- internal/importer/channel.go | 4 ++-- internal/importer/importer.go | 6 +++--- internal/importer/main.go | 16 +++------------- internal/importer/nixpkgs-channel.go | 4 ++-- 4 files changed, 10 insertions(+), 20 deletions(-) (limited to 'internal/importer') diff --git a/internal/importer/channel.go b/internal/importer/channel.go index fb6668c..1bce1b0 100644 --- a/internal/importer/channel.go +++ b/internal/importer/channel.go @@ -9,7 +9,7 @@ import ( "path" "searchix/internal/config" "searchix/internal/file" - "searchix/internal/search" + "searchix/internal/index" "strconv" "strings" @@ -81,7 +81,7 @@ func (i *ChannelImporter) FetchIfNeeded(parent context.Context) (bool, error) { return before != after, nil } -func (i *ChannelImporter) Import(parent context.Context, indexer *search.WriteIndex) (bool, error) { +func (i *ChannelImporter) Import(parent context.Context, indexer *index.WriteIndex) (bool, error) { if i.Source.OutputPath == "" { return false, errors.New("no output path specified") } diff --git a/internal/importer/importer.go b/internal/importer/importer.go index 5f251b0..a242481 100644 --- a/internal/importer/importer.go +++ b/internal/importer/importer.go @@ -5,13 +5,13 @@ import ( "log/slog" "path" "searchix/internal/config" - "searchix/internal/search" + "searchix/internal/index" "sync" ) type Importer interface { FetchIfNeeded(context.Context) (bool, error) - Import(context.Context, *search.WriteIndex) (bool, error) + Import(context.Context, *index.WriteIndex) (bool, error) } func NewNixpkgsChannelImporter( @@ -50,7 +50,7 @@ type importConfig struct { func processOptions( parent context.Context, - indexer *search.WriteIndex, + indexer *index.WriteIndex, conf *importConfig, ) (bool, error) { ctx, cancel := context.WithTimeout(parent, conf.Source.ImportTimeout) diff --git a/internal/importer/main.go b/internal/importer/main.go index a6f15e9..2d87e33 100644 --- a/internal/importer/main.go +++ b/internal/importer/main.go @@ -8,23 +8,18 @@ import ( "os/exec" "path" "searchix/internal/config" - "searchix/internal/search" + "searchix/internal/index" "strings" ) -func Start(cfg *config.Config, replace bool) error { +func Start(cfg *config.Config, indexer *index.WriteIndex, replace bool) error { if len(cfg.Importer.Sources) == 0 { slog.Info("No sources enabled") return nil } - indexer, err := search.NewIndexer(cfg.DataPath, replace) - if err != nil { - log.Fatalf("Failed to create indexer: %v", err) - } - - ctx, cancel := context.WithTimeout(context.Background(), cfg.Importer.Timeout) + ctx, cancel := context.WithTimeout(context.Background(), cfg.Importer.Timeout.Duration) defer cancel() var imp Importer @@ -81,10 +76,5 @@ func Start(cfg *config.Config, replace bool) error { } } - err = indexer.Close() - if err != nil { - slog.Error("error closing indexer", "error", err) - } - return nil } diff --git a/internal/importer/nixpkgs-channel.go b/internal/importer/nixpkgs-channel.go index 7aaa816..d302154 100644 --- a/internal/importer/nixpkgs-channel.go +++ b/internal/importer/nixpkgs-channel.go @@ -9,7 +9,7 @@ import ( "path" "searchix/internal/config" "searchix/internal/file" - "searchix/internal/search" + "searchix/internal/index" "github.com/pkg/errors" ) @@ -65,7 +65,7 @@ func (i *NixpkgsChannelImporter) FetchIfNeeded(parent context.Context) (bool, er func (i *NixpkgsChannelImporter) Import( parent context.Context, - indexer *search.WriteIndex, + indexer *index.WriteIndex, ) (bool, error) { filename := path.Join(i.DataPath, filesToFetch["options"]) revFilename := path.Join(i.DataPath, filesToFetch["revision"]) -- cgit 1.4.1