all repos — searchix @ bdb5a54c661407c39668096074a1f4a57898eb77

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

feat: store fetched files in data directory

Alan Pearce
commit

bdb5a54c661407c39668096074a1f4a57898eb77

parent

3e928369c3af69d0ef24a2f5d20c938689b15aa1

1 file changed, 25 insertions(+), 11 deletions(-)

changed files
M internal/fetcher/channel.gointernal/fetcher/channel.go
@@ -3,16 +3,14 @@
import ( "context" "fmt" - "os" "os/exec" - "path" + "path/filepath" "strconv" "strings" "time" "alin.ovh/searchix/internal/config" "alin.ovh/searchix/internal/index" - "alin.ovh/x/log" "github.com/Southclaws/fault" "github.com/Southclaws/fault/fmsg" )
@@ -20,18 +18,18 @@
type ChannelFetcher struct { Source *config.Source SourceFile string - Logger *log.Logger + *Options } func NewChannelFetcher( source *config.Source, - logger *log.Logger, + options *Options, ) (*ChannelFetcher, error) { switch source.Importer { case config.Options: return &ChannelFetcher{ - Source: source, - Logger: logger, + Source: source, + Options: options, }, nil default: return nil, fault.Newf("unsupported importer type %s", source.Importer)
@@ -42,6 +40,7 @@ func (i *ChannelFetcher) FetchIfNeeded(
ctx context.Context, sourceMeta *index.SourceMeta, ) (*FetchedFiles, error) { + target := i.Source.JoinPath("options.json") args := []string{ "--no-build-output", "--timeout",
@@ -64,20 +63,35 @@ return nil, fault.Wrap(err, fmsg.With("failed to run nix-build (--dry-run)"))
} //nolint:forbidigo // nix-build only gives the top-level path - outPath := path.Join(strings.TrimSpace(string(out)), i.Source.OutputPath, "options.json") + outPath := filepath.Join(strings.TrimSpace(string(out)), i.Source.OutputPath, "options.json") + i.Logger.Debug( "checking output path", "outputPath", outPath, + "updated", + outPath != sourceMeta.Path, ) - if outPath != sourceMeta.Path { sourceMeta.Path = outPath sourceMeta.Updated = time.Now().Truncate(time.Second) + + if exists, err := i.Root.Exists(target); err != nil { + return nil, fault.Wrap(err, fmsg.With("failed to check if target path exists")) + } else if exists { + err := i.Root.Remove(target) + if err != nil { + return nil, fault.Wrap(err, fmsg.Withf("failed to remove previous file %s", target)) + } + } + + err := i.Root.CopyTo(outPath, target) + if err != nil { + return nil, fault.Wrap(err, fmsg.With("failed to copy options.json")) + } } - //nolint:forbidigo // nix builds the file in the nix store - file, err := os.Open(outPath) + file, err := i.Root.Open(target) if err != nil { return nil, fault.Wrap(err, fmsg.With("failed to open options.json")) }