internal/importer/download-options.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 | package importer
import (
"bytes"
"context"
"log/slog"
"net/url"
"os"
"path"
"searchix/internal/config"
"searchix/internal/file"
"searchix/internal/index"
"github.com/pkg/errors"
)
type DownloadOptionsImporter struct {
DataPath string
Source *config.Source
SourceFile string
Logger *slog.Logger
}
var optionsFiles = map[string]string{
"revision": "revision",
"options": "options.json",
}
func (i *DownloadOptionsImporter) FetchIfNeeded(parent context.Context) (bool, error) {
ctx, cancel := context.WithTimeout(parent, i.Source.FetchTimeout)
defer cancel()
root := i.DataPath
err := file.Mkdirp(root)
if err != nil {
return false, errors.WithMessagef(err, "error creating directory for data: %s", root)
}
var updated bool
for _, filename := range optionsFiles {
url, err := url.JoinPath(i.Source.URL, filename)
if err != nil {
return false, errors.WithMessagef(
err,
"could not build URL with elements %s and %s",
i.Source.URL,
filename,
)
}
path := path.Join(root, filename)
i.Logger.Debug("preparing to fetch URL", "url", url, "path", path)
updated, err = fetchFileIfNeeded(ctx, path, url)
if err != nil {
return false, err
}
// don't bother to issue requests for the later files
if !updated {
return false, err
}
}
return updated, nil
}
func (i *DownloadOptionsImporter) Import(
parent context.Context,
indexer *index.WriteIndex,
) (bool, error) {
filename := path.Join(i.DataPath, optionsFiles["options"])
revFilename := path.Join(i.DataPath, optionsFiles["revision"])
bits, err := os.ReadFile(revFilename)
if err != nil {
return false, errors.WithMessagef(err, "unable to read revision file at %s", revFilename)
}
i.Source.Repo.Revision = string(bytes.TrimSpace(bits))
i.Logger.Debug("preparing import run", "revision", i.Source.Repo.Revision, "filename", filename)
return processOptions(parent, indexer, &importConfig{
Source: i.Source,
Filename: filename,
Logger: i.Logger,
})
}
|