distro: don't use a global variable to store the distro register

Introduce a DistroRegister object. For now this does not introduce
any functional changes, as the object is always instantited to be
the same. However, in follow-up patches it will get options.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2019-12-19 13:47:07 +01:00
parent 6c49560479
commit 5fcdd4bea5
11 changed files with 59 additions and 40 deletions

View file

@ -40,17 +40,29 @@ type Distro interface {
Runner() string
}
var registered map[string]Distro
func init() {
registered = map[string]Distro{
fedora30.Name: fedora30.New(),
rhel82.Name: rhel82.New(),
}
type Registry struct {
distros map[string]Distro
}
func New(name string) Distro {
distro, ok := registered[name]
func NewRegistry() *Registry {
distros := &Registry{
distros: make(map[string]Distro),
}
distros.register(fedora30.New())
distros.register(rhel82.New())
return distros
}
func (r *Registry) register(distro Distro) {
name := distro.Name()
if _, exists := r.distros[name]; exists {
panic("a distro with this name already exists: " + name)
}
r.distros[name] = distro
}
func (r *Registry) GetDistro(name string) Distro {
distro, ok := r.distros[name]
if !ok {
return nil
}
@ -58,7 +70,7 @@ func New(name string) Distro {
return distro
}
func FromHost() (Distro, error) {
func (r *Registry) FromHost() (Distro, error) {
f, err := os.Open("/etc/os-release")
if err != nil {
return nil, err
@ -72,7 +84,7 @@ func FromHost() (Distro, error) {
name := osrelease["ID"] + "-" + osrelease["VERSION_ID"]
d := New(name)
d := r.GetDistro(name)
if d == nil {
return nil, errors.New("unknown distro: " + name)
}
@ -80,13 +92,6 @@ func FromHost() (Distro, error) {
return d, nil
}
func Register(name string, distro Distro) {
if _, exists := registered[name]; exists {
panic("a distro with this name already exists: " + name)
}
registered[name] = distro
}
func readOSRelease(r io.Reader) (map[string]string, error) {
osrelease := make(map[string]string)
scanner := bufio.NewScanner(r)

View file

@ -42,7 +42,8 @@ func TestDistro_Pipeline(t *testing.T) {
continue
}
t.Run(tt.Compose.OutputFormat, func(t *testing.T) {
d := distro.New(tt.Compose.Distro)
distros := distro.NewRegistry()
d := distros.GetDistro(tt.Compose.Distro)
if d == nil {
t.Errorf("unknown distro: %v", tt.Compose.Distro)
return

View file

@ -19,7 +19,8 @@ func TestListOutputFormats(t *testing.T) {
"vmdk",
}
f30 := distro.New("fedora-30")
distros := distro.NewRegistry()
f30 := distros.GetDistro("fedora-30")
if got := f30.ListOutputFormats(); !reflect.DeepEqual(got, want) {
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
}
@ -92,7 +93,8 @@ func TestFilenameFromType(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f30 := distro.New("fedora-30")
distros := distro.NewRegistry()
f30 := distros.GetDistro("fedora-30")
got, got1, err := f30.FilenameFromType(tt.args.outputFormat)
if (err != nil) != tt.wantErr {
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)

View file

@ -19,8 +19,9 @@ func TestListOutputFormats(t *testing.T) {
"vmdk",
}
f31 := distro.New("rhel-8.2")
if got := f31.ListOutputFormats(); !reflect.DeepEqual(got, want) {
distros := distro.NewRegistry()
rhel82 := distros.GetDistro("rhel-8.2")
if got := rhel82.ListOutputFormats(); !reflect.DeepEqual(got, want) {
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
}
}
@ -92,8 +93,9 @@ func TestFilenameFromType(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f31 := distro.New("rhel-8.2")
got, got1, err := f31.FilenameFromType(tt.args.outputFormat)
distros := distro.NewRegistry()
rhel82 := distros.GetDistro("rhel-8.2")
got, got1, err := rhel82.FilenameFromType(tt.args.outputFormat)
if (err != nil) != tt.wantErr {
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
return

View file

@ -4,7 +4,6 @@ import (
"errors"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/pipeline"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
@ -13,8 +12,8 @@ type TestDistro struct{}
const Name = "test"
func init() {
distro.Register(Name, &TestDistro{})
func New() *TestDistro {
return &TestDistro{}
}
func (d *TestDistro) Name() string {