diff --git a/pkg/reporegistry/error.go b/pkg/reporegistry/error.go index 7e0938d7fe..34e7bb080b 100644 --- a/pkg/reporegistry/error.go +++ b/pkg/reporegistry/error.go @@ -1,13 +1,17 @@ package reporegistry -import "fmt" +import ( + "fmt" + "io/fs" +) // NoReposLoadedError is an error type that is returned when no repositories // are loaded from the given paths. type NoReposLoadedError struct { Paths []string + FSes []fs.FS } func (e *NoReposLoadedError) Error() string { - return fmt.Sprintf("no repositories found in the given paths: %v", e.Paths) + return fmt.Sprintf("no repositories found in the given paths: %v/%v", e.Paths, e.FSes) } diff --git a/pkg/reporegistry/reporegistry.go b/pkg/reporegistry/reporegistry.go index 7a15e042ad..7e796fd028 100644 --- a/pkg/reporegistry/reporegistry.go +++ b/pkg/reporegistry/reporegistry.go @@ -2,6 +2,7 @@ package reporegistry import ( "fmt" + "io/fs" "github.com/osbuild/images/pkg/distroidparser" "github.com/osbuild/images/pkg/rpmmd" @@ -14,10 +15,14 @@ type RepoRegistry struct { repos rpmmd.DistrosRepoConfigs } -// New returns a new RepoRegistry instance with the data -// loaded from the given repoConfigPaths -func New(repoConfigPaths []string) (*RepoRegistry, error) { - repositories, err := LoadAllRepositories(repoConfigPaths) +// New returns a new RepoRegistry instance with the data loaded from +// the given repoConfigPaths and repoConfigFS instance. The order is +// important here, first the paths are tried, then the FSes. +// +// Note that the confPaths must point directly to the directory with +// the json repo files. +func New(repoConfigPaths []string, repoConfigFS []fs.FS) (*RepoRegistry, error) { + repositories, err := loadAllRepositories(repoConfigPaths, repoConfigFS) if err != nil { return nil, err } diff --git a/pkg/reporegistry/repository.go b/pkg/reporegistry/repository.go index 4706724bef..47f239de8c 100644 --- a/pkg/reporegistry/repository.go +++ b/pkg/reporegistry/repository.go @@ -3,7 +3,6 @@ package reporegistry import ( "io/fs" "os" - "path/filepath" "strings" "github.com/sirupsen/logrus" @@ -12,23 +11,26 @@ import ( "github.com/osbuild/images/pkg/rpmmd" ) -// LoadAllRepositories loads all repositories for given distros from the given list of paths. +// loadAllRepositories loads all repositories for given distros from the given list of paths. // Behavior is the same as with the LoadRepositories() method. -func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) { - var confFSes []fs.FS +func loadAllRepositories(confPaths []string, confFSes []fs.FS) (rpmmd.DistrosRepoConfigs, error) { + var mergedFSes []fs.FS for _, confPath := range confPaths { - confFSes = append(confFSes, os.DirFS(filepath.Join(confPath, "repositories"))) + mergedFSes = append(mergedFSes, os.DirFS(confPath)) + } + for _, confFS := range confFSes { + mergedFSes = append(mergedFSes, confFS) } - distrosRepoConfigs, err := LoadAllRepositoriesFromFS(confFSes) + distrosRepoConfigs, err := loadAllRepositoriesFromFS(mergedFSes) if len(distrosRepoConfigs) == 0 { - return nil, &NoReposLoadedError{confPaths} + return nil, &NoReposLoadedError{confPaths, confFSes} } return distrosRepoConfigs, err } -func LoadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, error) { +func loadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, error) { distrosRepoConfigs := rpmmd.DistrosRepoConfigs{} for _, confPath := range confPaths { @@ -89,9 +91,12 @@ func LoadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, err // If there are duplicate distro repositories definitions found in multiple paths, the first // encounter is preferred. For this reason, the order of paths in the passed list should // reflect the desired preference. +// +// Note that the confPaths must point directly to the directory with +// the json repo files. func LoadRepositories(confPaths []string, distro string) (map[string][]rpmmd.RepoConfig, error) { var repoConfigs map[string][]rpmmd.RepoConfig - path := "/repositories/" + distro + ".json" + path := distro + ".json" for _, confPath := range confPaths { var err error @@ -109,7 +114,7 @@ func LoadRepositories(confPaths []string, distro string) (map[string][]rpmmd.Rep } if repoConfigs == nil { - return nil, &NoReposLoadedError{confPaths} + return nil, &NoReposLoadedError{confPaths, nil} } return repoConfigs, nil diff --git a/pkg/reporegistry/repository_test.go b/pkg/reporegistry/repository_test.go index 0bcf75f63f..30a29b3951 100644 --- a/pkg/reporegistry/repository_test.go +++ b/pkg/reporegistry/repository_test.go @@ -19,8 +19,8 @@ import ( func getConfPaths(t *testing.T) []string { confPaths := []string{ - "./test/confpaths/priority1", - "./test/confpaths/priority2", + "./test/confpaths/priority1/repositories", + "./test/confpaths/priority2/repositories", } var absConfPaths []string @@ -280,7 +280,7 @@ func Test_LoadAllRepositories(t *testing.T) { confPaths := getConfPaths(t) - distroReposMap, err := LoadAllRepositories(confPaths) + distroReposMap, err := loadAllRepositories(confPaths, nil) assert.NotNil(t, distroReposMap) assert.Nil(t, err) assert.Equal(t, len(distroReposMap), len(expectedReposMap)) @@ -307,7 +307,7 @@ func TestLoadRepositoriesLogging(t *testing.T) { logrus.AddHook(logHook) confPaths := getConfPaths(t) - _, err := LoadAllRepositories(confPaths) + _, err := loadAllRepositories(confPaths, nil) require.NoError(t, err) needle := "Loaded repository configuration file: rhel-8.10.json" assert.True(t, slices.ContainsFunc(logHook.AllEntries(), func(entry *logrus.Entry) bool {