all repos — searchix @ 001c5b4f4e6dcf0110504211d3f1298d5e6abc48

Search engine for NixOS, nix-darwin, home-manager and NUR users

internal/fetcher/nixpkgs-channel.go (view raw)

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package fetcher

import (
	"context"
	"net/url"

	"alin.ovh/searchix/internal/config"
	"alin.ovh/searchix/internal/fetcher/http"
	"alin.ovh/searchix/internal/index"

	"github.com/Southclaws/fault"
	"github.com/Southclaws/fault/fmsg"
)

type NixpkgsChannelFetcher struct {
	Source *config.Source
	*Options
}

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

	return url, fault.Wrap(err, fmsg.Withf("error creating URL"))
}

func NewNixpkgsChannelFetcher(
	source *config.Source,
	options *Options,
) (*NixpkgsChannelFetcher, error) {
	switch source.Importer {
	case config.Options, config.Packages:
		return &NixpkgsChannelFetcher{
			Source:  source,
			Options: options,
		}, nil
	default:
		return nil, fault.Newf("unsupported importer type %s", source.Importer)
	}
}

const (
	revisionFilename = "git-revision"
	optionsFilename  = "options.json.br"
	packagesFileName = "packages.json.br"
)

func (i *NixpkgsChannelFetcher) FetchIfNeeded(
	ctx context.Context,
	sourceMeta *index.SourceMeta,
) (f *FetchedFiles, err error) {
	f = &FetchedFiles{}

	filesToFetch := make(map[string]string, 2)
	filesToFetch[revisionFilename] = "revision"
	switch i.Source.Importer {
	case config.Packages:
		filesToFetch[packagesFileName] = "packages.json"
	case config.Options:
		filesToFetch[optionsFilename] = "options.json"
	}

	fetcher := http.NewFetcher(&http.Options{
		Logger: i.Logger.Named("http"),
		Root:   i.Root,
	})

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

		i.Logger.Debug("attempting to fetch file", "url", fetchURL)
		body, err := fetcher.FetchFileIfNeeded(ctx, target, fetchURL)
		if err != nil {
			return f, fault.Wrap(err, fmsg.Withf("failed to fetch file with url %s", fetchURL))
		}

		stat, err := i.Root.Stat(target)
		if err != nil {
			return nil, fault.Wrap(err, fmsg.Withf("could not stat file %s", target))
		}
		sourceMeta.Updated = stat.ModTime()

		switch urlname {
		case revisionFilename:
			f.Revision = body
		case optionsFilename:
			f.Options = body
		case packagesFileName:
			f.Packages = body
		default:
			return f, fault.Newf("unknown file kind %s", urlname)
		}
	}

	return
}