diff options
author | Alan Pearce | 2024-05-13 19:34:12 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-13 19:34:12 +0200 |
commit | 55efc5bec9703a299de5aac89006ed85600445fc (patch) | |
tree | 2e73e77ead7b6ad3820b3622be2b779b369e66ad /import | |
parent | 37deedc9b1da92571548c920721984d545269eb4 (diff) | |
download | searchix-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 'import')
-rw-r--r-- | import/main.go | 116 |
1 files changed, 10 insertions, 106 deletions
diff --git a/import/main.go b/import/main.go index d9c3bf5..76ebdcf 100644 --- a/import/main.go +++ b/import/main.go @@ -1,125 +1,29 @@ package main import ( - "context" - "errors" + "flag" "log" "log/slog" - "os" - "os/exec" - "path" "searchix/internal/config" "searchix/internal/importer" - "searchix/internal/search" - "strings" - "time" - - "github.com/ardanlabs/conf/v3" ) -type Config struct { - ConfigFile string `conf:"short:c"` - LogLevel slog.Level `conf:"default:INFO"` - Timeout time.Duration `conf:"default:30m,help:maximum time to wait for all fetchers and importers combined"` - Replace bool `conf:"default:false,help:whether to remove existing database, if exists"` -} +var ( + replace = flag.Bool("replace", false, "whether to replace existing database, if it exists") + configFile = flag.String("config", "config.toml", "config file to use") +) func main() { - if _, found := os.LookupEnv("DEBUG"); found { - slog.SetLogLoggerLevel(slog.LevelDebug) - } - var runtimeConfig Config - help, err := conf.Parse("", &runtimeConfig) - if err != nil { - if errors.Is(err, conf.ErrHelpWanted) { - log.Println(help) - os.Exit(1) - } - log.Panicf("parsing runtime configuration: %v", err) - } - slog.SetLogLoggerLevel(runtimeConfig.LogLevel) + flag.Parse() - cfg, err := config.GetConfig(runtimeConfig.ConfigFile) + cfg, err := config.GetConfig(*configFile) if err != nil { log.Fatal(err) } + slog.SetLogLoggerLevel(cfg.LogLevel) - if len(cfg.Sources) == 0 { - slog.Info("No sources enabled") - - return - } - - indexer, err := search.NewIndexer(cfg.DataPath, runtimeConfig.Replace) + err = importer.Start(cfg, *replace) if err != nil { - log.Fatalf("Failed to create indexer: %v", err) - } - - ctx, cancel := context.WithTimeout(context.Background(), runtimeConfig.Timeout) - defer cancel() - - var imp importer.Importer - var hadErrors bool - for name, source := range cfg.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 importer.ChannelNixpkgs: - imp = importer.NewNixpkgsChannelImporter(source, importerDataPath, logger) - case importer.Channel: - imp = importer.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) - } - hadErrors = true - - continue - } - logger.Info("importer fetch succeeded", "updated", updated) - - if updated || runtimeConfig.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) - } - - if hadErrors { - defer os.Exit(1) + log.Fatal(err) } } |