about summary refs log tree commit diff stats
path: root/import
diff options
context:
space:
mode:
authorAlan Pearce2024-05-09 16:47:41 +0200
committerAlan Pearce2024-05-09 19:27:55 +0200
commite062ca72b222b890e345548bd8422d5df98e9fef (patch)
tree89f52ebfdb1fb8069e6323d9dde42f5491dad5d1 /import
parent967f6fdf5c1693d3aa27079b3ae28768fb7356c6 (diff)
downloadsearchix-e062ca72b222b890e345548bd8422d5df98e9fef.tar.lz
searchix-e062ca72b222b890e345548bd8422d5df98e9fef.tar.zst
searchix-e062ca72b222b890e345548bd8422d5df98e9fef.zip
feat: import sources from configuration in go code and index options
Diffstat (limited to 'import')
-rw-r--r--import/main.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/import/main.go b/import/main.go
new file mode 100644
index 0000000..de652c5
--- /dev/null
+++ b/import/main.go
@@ -0,0 +1,111 @@
+package main
+
+import (
+	"context"
+	"errors"
+	"log"
+	"log/slog"
+	"os"
+	"os/exec"
+	"path"
+	"searchix/internal/config"
+	"searchix/internal/importer"
+	"searchix/internal/search"
+	"slices"
+	"strings"
+	"time"
+)
+
+const timeout = 30 * time.Minute
+
+func main() {
+	if _, found := os.LookupEnv("DEBUG"); found {
+		slog.SetLogLoggerLevel(slog.LevelDebug)
+	}
+	cfg, err := config.GetConfig()
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	enabledSources := slices.DeleteFunc(cfg.Sources, func(s importer.Source) bool {
+		return !s.Enable
+	})
+
+	if len(enabledSources) == 0 {
+		slog.Info("No sources enabled")
+
+		return
+	}
+
+	indexer, err := search.NewIndexer(cfg.DataPath)
+	if err != nil {
+		log.Fatalf("Failed to create indexer: %v", err)
+	}
+
+	ctx, cancel := context.WithTimeout(context.Background(), timeout)
+	defer cancel()
+
+	var imp importer.Importer
+	var hadErrors bool
+	for _, source := range enabledSources {
+		logger := slog.With("name", source.Name, "importer", source.Type.String())
+		logger.Debug("starting importer")
+
+		switch source.Type {
+		case importer.ChannelNixpkgs:
+			imp = importer.NewNixpkgsChannelImporter(source, cfg.DataPath, logger)
+		case importer.Channel:
+			imp = importer.NewChannelImporter(source, cfg.DataPath, 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)
+			}
+			hadErrors = true
+
+			continue
+		}
+		logger.Info("importer fetch succeeded", "updated", updated)
+
+		if updated {
+			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)
+	}
+
+	if hadErrors {
+		os.RemoveAll(path.Join(cfg.DataPath, "index.bleve"))
+		defer os.Exit(1)
+	}
+}