about summary refs log tree commit diff stats
path: root/internal/fetcher/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/fetcher/main.go')
-rw-r--r--internal/fetcher/main.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/fetcher/main.go b/internal/fetcher/main.go
new file mode 100644
index 0000000..d8bc25e
--- /dev/null
+++ b/internal/fetcher/main.go
@@ -0,0 +1,74 @@
+package fetcher
+
+import (
+	"context"
+	"log/slog"
+	"searchix/internal/config"
+
+	"github.com/pkg/errors"
+)
+
+type FetchedFiles struct {
+	Revision string
+	Options  string
+	Packages string
+}
+
+type Fetcher interface {
+	FetchIfNeeded(context.Context) (FetchedFiles, bool, error)
+}
+
+func NewNixpkgsChannelFetcher(
+	source *config.Source,
+	dataPath string,
+	logger *slog.Logger,
+) *NixpkgsChannelFetcher {
+	return &NixpkgsChannelFetcher{
+		DataPath: dataPath,
+		Source:   source,
+		Logger:   logger,
+	}
+}
+
+func NewChannelFetcher(
+	source *config.Source,
+	dataPath string,
+	logger *slog.Logger,
+) *ChannelFetcher {
+	return &ChannelFetcher{
+		DataPath: dataPath,
+		Source:   source,
+		Logger:   logger,
+	}
+}
+
+func NewDownloadFetcher(
+	source *config.Source,
+	dataPath string,
+	logger *slog.Logger,
+) *DownloadFetcher {
+	return &DownloadFetcher{
+		DataPath: dataPath,
+		Source:   source,
+		Logger:   logger,
+	}
+}
+
+func New(
+	source *config.Source,
+	fetcherDataPath string,
+	logger *slog.Logger,
+) (fetcher Fetcher, err error) {
+	switch source.Fetcher {
+	case config.ChannelNixpkgs:
+		fetcher = NewNixpkgsChannelFetcher(source, fetcherDataPath, logger)
+	case config.Channel:
+		fetcher = NewChannelFetcher(source, fetcherDataPath, logger)
+	case config.Download:
+		fetcher = NewDownloadFetcher(source, fetcherDataPath, logger)
+	default:
+		err = errors.Errorf("unsupported fetcher type %s", source.Fetcher.String())
+	}
+
+	return
+}