diff --git a/cmd/osbuild-composer/composer.go b/cmd/osbuild-composer/composer.go index 721c73127..82187afcb 100644 --- a/cmd/osbuild-composer/composer.go +++ b/cmd/osbuild-composer/composer.go @@ -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") diff --git a/cmd/osbuild-pipeline/main.go b/cmd/osbuild-pipeline/main.go index a3fa4dfd3..8bd8697f1 100644 --- a/cmd/osbuild-pipeline/main.go +++ b/cmd/osbuild-pipeline/main.go @@ -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) } diff --git a/internal/distroregistry/distroregistry.go b/internal/distroregistry/distroregistry.go index c99e75f47..e21f14b8c 100644 --- a/internal/distroregistry/distroregistry.go +++ b/internal/distroregistry/distroregistry.go @@ -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 {