about summary refs log tree commit diff stats
path: root/internal/importer/main.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-13 19:34:12 +0200
committerAlan Pearce2024-05-13 19:34:12 +0200
commit55efc5bec9703a299de5aac89006ed85600445fc (patch)
tree2e73e77ead7b6ad3820b3622be2b779b369e66ad /internal/importer/main.go
parent37deedc9b1da92571548c920721984d545269eb4 (diff)
downloadsearchix-55efc5bec9703a299de5aac89006ed85600445fc.tar.lz
searchix-55efc5bec9703a299de5aac89006ed85600445fc.tar.zst
searchix-55efc5bec9703a299de5aac89006ed85600445fc.zip
refactor(config): simplify configuration
commit 8225dbdb692c99b39dcafe2e5ec6ddc4daf08fb5
Author: Alan Pearce <alan@alanpearce.eu>
Date:   Mon May 13 19:18:26 2024 +0200

    refactor: consolidate configuration to reduce command-line options

commit 5616d4c5a9bc6c0c14f744f812fa6609f859dc34
Author: Alan Pearce <alan@alanpearce.eu>
Date:   Mon May 13 17:41:58 2024 +0200

    refactor: move config file parsing to program entry points
Diffstat (limited to 'internal/importer/main.go')
-rw-r--r--internal/importer/main.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/internal/importer/main.go b/internal/importer/main.go
new file mode 100644
index 0000000..a6f15e9
--- /dev/null
+++ b/internal/importer/main.go
@@ -0,0 +1,90 @@
+package importer
+
+import (
+	"context"
+	"errors"
+	"log"
+	"log/slog"
+	"os/exec"
+	"path"
+	"searchix/internal/config"
+	"searchix/internal/search"
+	"strings"
+)
+
+func Start(cfg *config.Config, 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)
+	defer cancel()
+
+	var imp Importer
+	for name, source := range cfg.Importer.Sources {
+		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 config.ChannelNixpkgs:
+			imp = NewNixpkgsChannelImporter(source, importerDataPath, logger)
+		case config.Channel:
+			imp = NewChannelImporter(source, importerDataPath, logger)
+		default:
+			log.Printf("unsupported importer type %s", source.Type.String())
+
+			continue
+		}
+
+		updated, err := imp.FetchIfNeeded(ctx)
+
+		if err != nil {
+			var exerr *exec.ExitError
+			if errors.As(err, &exerr) {
+				lines := strings.Split(strings.TrimSpace(string(exerr.Stderr)), "\n")
+				for _, line := range lines {
+					logger.Warn("importer fetch failed", "stderr", line, "status", exerr.ExitCode())
+				}
+			} else {
+				logger.Warn("importer fetch failed", "error", err)
+			}
+
+			continue
+		}
+		logger.Info("importer fetch succeeded", "updated", updated)
+
+		if updated || replace {
+			hadWarnings, err := imp.Import(ctx, indexer)
+
+			if err != nil {
+				msg := err.Error()
+				for _, line := range strings.Split(strings.TrimSpace(msg), "\n") {
+					logger.Error("importer init failed", "error", line)
+				}
+
+				continue
+			}
+			if hadWarnings {
+				logger.Warn("importer succeeded, but with warnings/errors")
+			} else {
+				logger.Info("importer succeeded")
+			}
+		}
+	}
+
+	err = indexer.Close()
+	if err != nil {
+		slog.Error("error closing indexer", "error", err)
+	}
+
+	return nil
+}