Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ func run() error {
}
overrideRepos = repoConfig[archName]
} else {
// HACK: reporegistry hardcodes adding the "repositories"
// dir to the "repoConfigPaths" but the interface of
// "images" does not expect this.
// XXX: should we fix this in reporegistry?
repositories = filepath.Join(repositories, "..")
reporeg, err = reporegistry.New([]string{repositories})
reporeg, err = reporegistry.New([]string{repositories}, nil)
if err != nil {
return fmt.Errorf("failed to load repositories from %q: %w", repositories, err)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/reporegistry/error.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 9 additions & 4 deletions pkg/reporegistry/reporegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reporegistry

import (
"fmt"
"io/fs"

"github.com/osbuild/images/pkg/distroidparser"
"github.com/osbuild/images/pkg/rpmmd"
Expand All @@ -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
}
Expand Down
24 changes: 14 additions & 10 deletions pkg/reporegistry/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ 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))
}
mergedFSes = append(mergedFSes, confFSes...)

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 {
Expand Down Expand Up @@ -89,13 +90,16 @@ 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
repoConfigs, err = rpmmd.LoadRepositoriesFromFile(confPath + path)
repoConfigs, err = rpmmd.LoadRepositoriesFromFile(filepath.Join(confPath, path))
if os.IsNotExist(err) {
continue
} else if err != nil {
Expand All @@ -109,7 +113,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
Expand Down
8 changes: 4 additions & 4 deletions pkg/reporegistry/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions test/data/repositories/testrepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ import (
var FS embed.FS

func New() (*reporegistry.RepoRegistry, error) {
repositories, err := reporegistry.LoadAllRepositoriesFromFS([]fs.FS{FS})
if err != nil {
return nil, err
}
return reporegistry.NewFromDistrosRepoConfigs(repositories), nil
return reporegistry.New(nil, []fs.FS{FS})
}
Loading