about summary refs log tree commit diff stats
path: root/internal/fetcher/main.go
blob: fcc04a90543b25a3aaa4477930205837504f8c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package fetcher

import (
	"context"
	"io"
	"log/slog"

	"go.alanpearce.eu/searchix/internal/config"
	"go.alanpearce.eu/searchix/internal/index"

	"github.com/pkg/errors"
)

type FetchedFiles struct {
	Revision io.ReadCloser
	Options  io.ReadCloser
	Packages io.ReadCloser
}

type Fetcher interface {
	FetchIfNeeded(context.Context, *index.SourceMeta) (FetchedFiles, error)
}

func New(
	source *config.Source,
	logger *slog.Logger,
) (fetcher Fetcher, err error) {
	switch source.Fetcher {
	case config.ChannelNixpkgs:
		fetcher, err = NewNixpkgsChannelFetcher(source, logger)
	case config.Channel:
		fetcher, err = NewChannelFetcher(source, logger)
	case config.Download:
		fetcher, err = NewDownloadFetcher(source, logger)
	default:
		err = errors.Errorf("unsupported fetcher type %s", source.Fetcher.String())
	}

	return
}