about summary refs log tree commit diff stats
path: root/internal/fetcher/nixpkgs-channel.go
blob: e36728cdffec9ba274a42ed36ba5bcad3512a7a7 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package fetcher

import (
	"context"
	"log/slog"
	"net/url"
	"path"
	"searchix/internal/config"
	"searchix/internal/file"

	"github.com/pkg/errors"
)

type NixpkgsChannelFetcher struct {
	DataPath string
	Source   *config.Source
	Logger   *slog.Logger
}

func makeChannelURL(channel string, subPath string) (string, error) {
	url, err := url.JoinPath("https://channels.nixos.org/", channel, subPath)

	return url, errors.WithMessagef(err, "error creating URL")
}

var filesToFetch = map[string]string{
	"revision": "git-revision",
	"options":  "options.json.br",
	"packages": "packages.json.br",
}

func (i *NixpkgsChannelFetcher) FetchIfNeeded(
	parent context.Context,
) (f FetchedFiles, updated bool, err error) {
	ctx, cancel := context.WithTimeout(parent, i.Source.FetchTimeout.Duration)
	defer cancel()

	root := i.DataPath

	err = file.Mkdirp(root)
	if err != nil {
		err = errors.WithMessagef(err, "error creating directory for data: %s", root)

		return
	}

	var fetchURL string
	for _, filename := range filesToFetch {
		fetchURL, err = makeChannelURL(i.Source.Channel, filename)
		if err != nil {
			return
		}

		outPath := path.Join(root, filename)

		i.Logger.Debug("attempting to fetch file", "url", fetchURL, "outPath", outPath)
		updated, err = fetchFileIfNeeded(ctx, outPath, fetchURL)
		if err != nil {
			return
		}
		// don't bother to issue requests for the later files
		if !updated {
			break
		}
	}

	f = FetchedFiles{
		Revision: path.Join(root, "git-revision"),
		Options:  path.Join(root, "options.json.br"),
		Packages: path.Join(root, "packages.json.br"),
	}

	return
}