distroregistry: add a default distroregistry

This commit adds NewDefault() method to distroregistry that returns a slice
with all distributions supported by osbuild-composer. This way, there's only
one place where a distribution needs to be defined while its support
is being added to composer.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
This commit is contained in:
Ondřej Budai 2021-03-11 12:33:22 +01:00 committed by Ondřej Budai
parent dd4db353e2
commit 3c715c7cf8
3 changed files with 33 additions and 14 deletions

View file

@ -21,11 +21,6 @@ import (
"github.com/osbuild/osbuild-composer/internal/store"
"github.com/osbuild/osbuild-composer/internal/weldr"
"github.com/osbuild/osbuild-composer/internal/worker"
"github.com/osbuild/osbuild-composer/internal/distro/fedora32"
"github.com/osbuild/osbuild-composer/internal/distro/fedora33"
"github.com/osbuild/osbuild-composer/internal/distro/rhel8"
"github.com/osbuild/osbuild-composer/internal/distro/rhel84"
)
type Composer struct {
@ -63,10 +58,7 @@ func NewComposer(config *ComposerConfigFile, stateDir, cacheDir string, logger *
return nil, err
}
c.distros, err = distroregistry.New(fedora32.New(), fedora33.New(), rhel8.New(), rhel84.New(), rhel84.NewCentos())
if err != nil {
return nil, fmt.Errorf("Error loading distros: %v", err)
}
c.distros = distroregistry.NewDefault()
c.rpm = rpmmd.NewRPMMD(path.Join(c.cacheDir, "rpmmd"), "/usr/libexec/osbuild-composer/dnf-json")

View file

@ -9,10 +9,6 @@ import (
"os"
"path"
"github.com/osbuild/osbuild-composer/internal/distro/fedora32"
"github.com/osbuild/osbuild-composer/internal/distro/fedora33"
"github.com/osbuild/osbuild-composer/internal/distro/rhel8"
"github.com/osbuild/osbuild-composer/internal/distro/rhel84"
"github.com/osbuild/osbuild-composer/internal/distroregistry"
"github.com/osbuild/osbuild-composer/internal/blueprint"
@ -68,7 +64,7 @@ func main() {
}
}
distros, err := distroregistry.New(fedora32.New(), fedora33.New(), rhel8.New(), rhel84.New(), rhel84.NewCentos())
distros := distroregistry.NewDefault()
if err != nil {
panic(err)
}

View file

@ -6,8 +6,22 @@ import (
"sort"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/distro/fedora32"
"github.com/osbuild/osbuild-composer/internal/distro/fedora33"
"github.com/osbuild/osbuild-composer/internal/distro/rhel8"
"github.com/osbuild/osbuild-composer/internal/distro/rhel84"
)
// When adding support for a new distribution, add it here.
// Note that this is a constant, do not write to this array.
var supportedDistros = []func() distro.Distro{
fedora32.New,
fedora33.New,
rhel8.New,
rhel84.New,
rhel84.NewCentos,
}
type Registry struct {
distros map[string]distro.Distro
}
@ -26,6 +40,23 @@ func New(distros ...distro.Distro) (*Registry, error) {
return reg, nil
}
// NewDefault creates a Registry with all distributions supported by
// osbuild-composer. If you need to add a distribution here, see the
// supportedDistros variable.
func NewDefault() *Registry {
var distros []distro.Distro
for _, distroInitializer := range supportedDistros {
distros = append(distros, distroInitializer())
}
registry, err := New(distros...)
if err != nil {
panic(fmt.Sprintf("two supported distros have the same name, this is a programming error: %v", err))
}
return registry
}
func (r *Registry) GetDistro(name string) distro.Distro {
d, ok := r.distros[name]
if !ok {