From a30708c025b605f4a88a005343e4277d719fbc2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 23 Sep 2024 19:15:33 +0200 Subject: [PATCH] reporegistry: add NoReposLoadedError and use it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `NoReposLoadedError` custom error type for the case when any function that is supposed to load repository configs, does not load any. Use it in all functions that are directly loading repositories from configuration files. I considered `NoReposError` name, but that could be later added for functions such as `RepoRegistry.ReposBy...()`, in case there are no repositories in the RepoRegistry for the given image type / arch. Signed-off-by: Tomáš Hozza --- pkg/reporegistry/error.go | 13 +++++++++++++ pkg/reporegistry/reporegistry.go | 3 --- pkg/reporegistry/repository.go | 7 +++++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 pkg/reporegistry/error.go diff --git a/pkg/reporegistry/error.go b/pkg/reporegistry/error.go new file mode 100644 index 0000000000..7e0938d7fe --- /dev/null +++ b/pkg/reporegistry/error.go @@ -0,0 +1,13 @@ +package reporegistry + +import "fmt" + +// NoReposLoadedError is an error type that is returned when no repositories +// are loaded from the given paths. +type NoReposLoadedError struct { + Paths []string +} + +func (e *NoReposLoadedError) Error() string { + return fmt.Sprintf("no repositories found in the given paths: %v", e.Paths) +} diff --git a/pkg/reporegistry/reporegistry.go b/pkg/reporegistry/reporegistry.go index 8934fde245..24209c9b51 100644 --- a/pkg/reporegistry/reporegistry.go +++ b/pkg/reporegistry/reporegistry.go @@ -23,9 +23,6 @@ func New(repoConfigPaths []string) (*RepoRegistry, error) { if err != nil { return nil, err } - if len(repositories) == 0 { - return nil, fmt.Errorf("no repositories found in the given paths: %v", repoConfigPaths) - } return &RepoRegistry{repositories}, nil } diff --git a/pkg/reporegistry/repository.go b/pkg/reporegistry/repository.go index a65e194681..8b9b7ab87a 100644 --- a/pkg/reporegistry/repository.go +++ b/pkg/reporegistry/repository.go @@ -1,7 +1,6 @@ package reporegistry import ( - "fmt" "log" "os" "path/filepath" @@ -66,6 +65,10 @@ func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) { } } + if len(distrosRepoConfigs) == 0 { + return nil, &NoReposLoadedError{confPaths} + } + return distrosRepoConfigs, nil } @@ -93,7 +96,7 @@ func LoadRepositories(confPaths []string, distro string) (map[string][]rpmmd.Rep } if repoConfigs == nil { - return nil, fmt.Errorf("LoadRepositories failed: none of the provided paths contain distro configuration") + return nil, &NoReposLoadedError{confPaths} } return repoConfigs, nil