about summary refs log tree commit diff stats
path: root/internal/fetcher/nixpkgs-channel.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-16 23:41:57 +0200
committerAlan Pearce2024-05-16 23:41:57 +0200
commita5e758d41c151c17ed03b39454470ba8dd0c3b99 (patch)
tree386333b5020477eabcf490773113b029e47a21ef /internal/fetcher/nixpkgs-channel.go
parentd558039919b6198a246a6a3fd007276191cb4b2f (diff)
downloadsearchix-a5e758d41c151c17ed03b39454470ba8dd0c3b99.tar.lz
searchix-a5e758d41c151c17ed03b39454470ba8dd0c3b99.tar.zst
searchix-a5e758d41c151c17ed03b39454470ba8dd0c3b99.zip
refactor: separate fetch and import logic
Diffstat (limited to 'internal/fetcher/nixpkgs-channel.go')
-rw-r--r--internal/fetcher/nixpkgs-channel.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/fetcher/nixpkgs-channel.go b/internal/fetcher/nixpkgs-channel.go
new file mode 100644
index 0000000..aa1a09d
--- /dev/null
+++ b/internal/fetcher/nixpkgs-channel.go
@@ -0,0 +1,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)
+	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 {
+			return
+		}
+	}
+
+	f = FetchedFiles{
+		Revision: path.Join(root, "git-revision"),
+		Options:  path.Join(root, "options.json.br"),
+		Packages: path.Join(root, "packages.json.br"),
+	}
+
+	return
+}