about summary refs log tree commit diff stats
path: root/internal/importer
diff options
context:
space:
mode:
authorAlan Pearce2025-01-15 22:25:33 +0100
committerAlan Pearce2025-01-15 22:25:33 +0100
commit7aea6aa210a8939ac208fb7540d1b46ba69a995f (patch)
tree80f8db2539289ca545eb356bf87e2b764d39c966 /internal/importer
parentb26ddba432f8bde78022d2fc8837f0ffb25448b1 (diff)
downloadsearchix-7aea6aa210a8939ac208fb7540d1b46ba69a995f.tar.lz
searchix-7aea6aa210a8939ac208fb7540d1b46ba69a995f.tar.zst
searchix-7aea6aa210a8939ac208fb7540d1b46ba69a995f.zip
feat: enable searching via program names for multi-program packages
implements: https://todo.sr.ht/~alanpearce/searchix/6
Diffstat (limited to 'internal/importer')
-rw-r--r--internal/importer/main.go13
-rw-r--r--internal/importer/package.go47
2 files changed, 49 insertions, 11 deletions
diff --git a/internal/importer/main.go b/internal/importer/main.go
index 4c66501..dfa2477 100644
--- a/internal/importer/main.go
+++ b/internal/importer/main.go
@@ -11,6 +11,7 @@ import (
 	"go.alanpearce.eu/searchix/internal/config"
 	"go.alanpearce.eu/searchix/internal/fetcher"
 	"go.alanpearce.eu/searchix/internal/index"
+	"go.alanpearce.eu/searchix/internal/programs"
 	"go.alanpearce.eu/x/log"
 
 	"github.com/pkg/errors"
@@ -78,6 +79,17 @@ func createSourceImporter(
 		)
 
 		if sourceMeta.Updated.After(previousUpdate) || forceUpdate {
+			var pdb *programs.DB
+
+			if source.Programs.Enable {
+				pdb, err = programs.Instantiate(ctx, source, log.Named("programs"))
+				if err != nil {
+					logger.Warn("programs database instantiation failed", "error", err)
+				}
+				if pdb.Path != sourceMeta.ProgramsPath {
+					sourceMeta.ProgramsPath = pdb.Path
+				}
+			}
 
 			err = setRepoRevision(files.Revision, source)
 			if err != nil {
@@ -105,6 +117,7 @@ func createSourceImporter(
 					files.Packages,
 					source,
 					logger.Named("processor"),
+					pdb,
 				)
 			}
 			if err != nil {
diff --git a/internal/importer/package.go b/internal/importer/package.go
index 80adc38..59bccd8 100644
--- a/internal/importer/package.go
+++ b/internal/importer/package.go
@@ -9,6 +9,7 @@ import (
 
 	"go.alanpearce.eu/searchix/internal/config"
 	"go.alanpearce.eu/searchix/internal/nix"
+	"go.alanpearce.eu/searchix/internal/programs"
 	"go.alanpearce.eu/x/log"
 
 	"github.com/bcicen/jstream"
@@ -39,12 +40,13 @@ type maintainerJSON struct {
 }
 
 type PackageIngester struct {
-	dec    *jstream.Decoder
-	ms     *mapstructure.Decoder
-	log    *log.Logger
-	pkg    packageJSON
-	infile io.ReadCloser
-	source *config.Source
+	dec      *jstream.Decoder
+	ms       *mapstructure.Decoder
+	log      *log.Logger
+	pkg      packageJSON
+	infile   io.ReadCloser
+	source   *config.Source
+	programs *programs.DB
 }
 
 func makeAdhocLicense(name string) nix.License {
@@ -66,13 +68,15 @@ func NewPackageProcessor(
 	infile io.ReadCloser,
 	source *config.Source,
 	log *log.Logger,
+	programsDB *programs.DB,
 ) (*PackageIngester, error) {
 	i := &PackageIngester{
-		dec:    jstream.NewDecoder(infile, 2).EmitKV(),
-		log:    log,
-		pkg:    packageJSON{},
-		infile: infile,
-		source: source,
+		dec:      jstream.NewDecoder(infile, 2).EmitKV(),
+		log:      log,
+		pkg:      packageJSON{},
+		infile:   infile,
+		source:   source,
+		programs: programsDB,
 	}
 
 	ms, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
@@ -116,6 +120,14 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan nix.Importable, <
 	results := make(chan nix.Importable)
 	errs := make(chan error)
 
+	if i.programs != nil {
+		err := i.programs.Open()
+		if err != nil {
+			errs <- errors.WithMessage(err, "could not open programs database")
+			i.programs = nil
+		}
+	}
+
 	go func() {
 		defer i.infile.Close()
 		defer close(results)
@@ -124,6 +136,7 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan nix.Importable, <
 	outer:
 		for mv := range i.dec.Stream() {
 			var err error
+			var programs []string
 			select {
 			case <-ctx.Done():
 				break outer
@@ -216,6 +229,13 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan nix.Importable, <
 				continue
 			}
 
+			if i.programs != nil {
+				programs, err = i.programs.GetPackagePrograms(ctx, kv.Key)
+				if err != nil {
+					errs <- errors.WithMessagef(err, "failed to get programs for package %s", i.pkg.Name)
+				}
+			}
+
 			maintainers := make([]nix.Maintainer, len(i.pkg.Meta.Maintainers))
 			for i, m := range i.pkg.Meta.Maintainers {
 				maintainers[i] = nix.Maintainer{
@@ -250,8 +270,13 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan nix.Importable, <
 				Licenses:        licenses,
 				Maintainers:     maintainers,
 				Definition:      url,
+				Programs:        programs,
 			}
 		}
+
+		if i.programs != nil {
+			i.programs.Close()
+		}
 	}()
 
 	return results, errs