all repos — searchix @ 21e9e778cc6be3c8717cd4286bedd17f2b22282a

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

feat: add offline flag to reindex (mostly for dev)

Alan Pearce
commit

21e9e778cc6be3c8717cd4286bedd17f2b22282a

parent

11a0ae77d2e66bcdd0a65c71a160dcc5d9d64e56

1 file changed, 47 insertions(+), 0 deletions(-)

changed files
M internal/fetcher/main.gointernal/fetcher/main.go
@@ -3,6 +3,7 @@
import ( "context" "io" + "os" "alin.ovh/searchix/internal/config" "alin.ovh/searchix/internal/file"
@@ -57,3 +58,49 @@ }
return } + +func Open( + source *config.Source, + opts *Options, +) (*FetchedFiles, error) { + root := opts.Root + + f := &FetchedFiles{} + + rev, err := openIfExists(root, source.JoinPath("revision")) + if err != nil { + opts.Logger.Warn("failed to open revision file", "error", err) + } + if rev != nil { + f.Revision = rev + } + + switch source.Importer { + case config.Options: + f.Options, err = root.Open(source.JoinPath("options.json")) + case config.Packages: + f.Packages, err = root.Open(source.JoinPath("packages.json")) + default: + err = fault.Newf("unsupported importer type %s", source.Importer.String()) + } + if err != nil { + return nil, err + } + + return f, nil +} + +func openIfExists(root *file.Root, filename string) (*os.File, error) { + if exists, err := root.Exists(filename); err != nil { + return nil, fault.Wrap(err, fmsg.With("failed to check if file exists")) + } else if !exists { + return nil, nil + } + + f, err := root.Open(filename) + if err != nil { + return nil, fault.Wrap(err, fmsg.Withf("failed to open file %s", filename)) + } + + return f, nil +}