distro: panic less often
Return errors from all distro's New() functions instead of logging and returning nil. Also, return errors instead of panicking from NewRegistry() and NewDefaultRegistry().
This commit is contained in:
parent
d1965d6268
commit
87e9c39532
19 changed files with 128 additions and 62 deletions
|
|
@ -3,6 +3,7 @@ package distro
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
|
@ -70,41 +71,41 @@ type Registry struct {
|
|||
distros map[common.Distribution]Distro
|
||||
}
|
||||
|
||||
func NewRegistry(distros ...Distro) *Registry {
|
||||
func NewRegistry(distros ...Distro) (*Registry, error) {
|
||||
reg := &Registry{
|
||||
distros: make(map[common.Distribution]Distro),
|
||||
}
|
||||
for _, distro := range distros {
|
||||
distroTag := distro.Distribution()
|
||||
if _, exists := reg.distros[distroTag]; exists {
|
||||
panic("a distro with this name already exists: " + distro.Name())
|
||||
return nil, fmt.Errorf("NewRegistry: passed two distros with the same name: %s", distro.Name())
|
||||
}
|
||||
reg.distros[distroTag] = distro
|
||||
}
|
||||
return reg
|
||||
return reg, nil
|
||||
}
|
||||
|
||||
// Create a new Registry containing all known distros.
|
||||
func NewDefaultRegistry(confPaths []string) *Registry {
|
||||
f30 := fedora30.New(confPaths)
|
||||
if f30 == nil {
|
||||
panic("Attempt to register Fedora 30 failed")
|
||||
func NewDefaultRegistry(confPaths []string) (*Registry, error) {
|
||||
f30, err := fedora30.New(confPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading fedora30: %v", err)
|
||||
}
|
||||
f31 := fedora31.New(confPaths)
|
||||
if f31 == nil {
|
||||
panic("Attempt to register Fedora 31 failed")
|
||||
f31, err := fedora31.New(confPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading fedora31: %v", err)
|
||||
}
|
||||
f32 := fedora32.New(confPaths)
|
||||
if f32 == nil {
|
||||
panic("Attempt to register Fedora 32 failed")
|
||||
f32, err := fedora32.New(confPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading fedora32: %v", err)
|
||||
}
|
||||
el81 := rhel81.New(confPaths)
|
||||
if el81 == nil {
|
||||
panic("Attempt to register RHEL 8.1 failed")
|
||||
el81, err := rhel81.New(confPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading rhel81: %v", err)
|
||||
}
|
||||
el82 := rhel82.New(confPaths)
|
||||
if el82 == nil {
|
||||
panic("Attempt to register RHEL 8.2 failed")
|
||||
el82, err := rhel82.New(confPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading rhel82: %v", err)
|
||||
}
|
||||
return NewRegistry(f30, f31, f32, el81, el82)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,10 @@ func TestDistro_Pipeline(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
t.Run(tt.Compose.OutputFormat, func(t *testing.T) {
|
||||
distros := distro.NewDefaultRegistry([]string{"../.."})
|
||||
distros, err := distro.NewDefaultRegistry([]string{"../.."})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := distros.GetDistro(tt.Compose.Distro)
|
||||
if d == nil {
|
||||
t.Errorf("unknown distro: %v", tt.Compose.Distro)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package fedora30
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -46,7 +47,7 @@ type output struct {
|
|||
const Distro = common.Fedora30
|
||||
const ModulePlatformID = "platform:f30"
|
||||
|
||||
func New(confPaths []string) *Fedora30 {
|
||||
func New(confPaths []string) (*Fedora30, error) {
|
||||
const GigaByte = 1024 * 1024 * 1024
|
||||
|
||||
r := Fedora30{
|
||||
|
|
@ -65,8 +66,7 @@ func New(confPaths []string) *Fedora30 {
|
|||
|
||||
repoMap, err := rpmmd.LoadRepositories(confPaths, r.Name())
|
||||
if err != nil {
|
||||
log.Printf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
return nil
|
||||
return nil, fmt.Errorf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
}
|
||||
|
||||
repos, exists := repoMap["x86_64"]
|
||||
|
|
@ -307,7 +307,7 @@ func New(confPaths []string) *Fedora30 {
|
|||
},
|
||||
}
|
||||
|
||||
return &r
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r *Fedora30) Name() string {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
f30 := fedora30.New([]string{"../../../"})
|
||||
f30, err := fedora30.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := f30.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +96,10 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f30 := fedora30.New([]string{"../../../"})
|
||||
f30, err := fedora30.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, got1, err := f30.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package fedora31
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -46,7 +47,7 @@ type output struct {
|
|||
const Distro = common.Fedora31
|
||||
const ModulePlatformID = "platform:f31"
|
||||
|
||||
func New(confPaths []string) *Fedora31 {
|
||||
func New(confPaths []string) (*Fedora31, error) {
|
||||
const GigaByte = 1024 * 1024 * 1024
|
||||
|
||||
r := Fedora31{
|
||||
|
|
@ -65,8 +66,7 @@ func New(confPaths []string) *Fedora31 {
|
|||
|
||||
repoMap, err := rpmmd.LoadRepositories(confPaths, r.Name())
|
||||
if err != nil {
|
||||
log.Printf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
return nil
|
||||
return nil, fmt.Errorf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
}
|
||||
|
||||
repos, exists := repoMap["x86_64"]
|
||||
|
|
@ -307,7 +307,7 @@ func New(confPaths []string) *Fedora31 {
|
|||
},
|
||||
}
|
||||
|
||||
return &r
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r *Fedora31) Name() string {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
f31 := fedora31.New([]string{"../../../"})
|
||||
f31, err := fedora31.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := f31.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +96,10 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f31 := fedora31.New([]string{"../../../"})
|
||||
f31, err := fedora31.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, got1, err := f31.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package fedora32
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -46,7 +47,7 @@ type output struct {
|
|||
const Distro = common.Fedora32
|
||||
const ModulePlatformID = "platform:f32"
|
||||
|
||||
func New(confPaths []string) *Fedora32 {
|
||||
func New(confPaths []string) (*Fedora32, error) {
|
||||
const GigaByte = 1024 * 1024 * 1024
|
||||
|
||||
r := Fedora32{
|
||||
|
|
@ -65,8 +66,7 @@ func New(confPaths []string) *Fedora32 {
|
|||
|
||||
repoMap, err := rpmmd.LoadRepositories(confPaths, r.Name())
|
||||
if err != nil {
|
||||
log.Printf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
return nil
|
||||
return nil, fmt.Errorf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
}
|
||||
|
||||
repos, exists := repoMap["x86_64"]
|
||||
|
|
@ -307,7 +307,7 @@ func New(confPaths []string) *Fedora32 {
|
|||
},
|
||||
}
|
||||
|
||||
return &r
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r *Fedora32) Name() string {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
f32 := fedora32.New([]string{"../../../"})
|
||||
f32, err := fedora32.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := f32.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +96,10 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f32 := fedora32.New([]string{"../../../"})
|
||||
f32, err := fedora32.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, got1, err := f32.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package rhel81
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -47,7 +48,7 @@ type output struct {
|
|||
const Distro = common.RHEL81
|
||||
const ModulePlatformID = "platform:el8"
|
||||
|
||||
func New(confPaths []string) *RHEL81 {
|
||||
func New(confPaths []string) (*RHEL81, error) {
|
||||
const GigaByte = 1024 * 1024 * 1024
|
||||
|
||||
r := RHEL81{
|
||||
|
|
@ -70,8 +71,7 @@ func New(confPaths []string) *RHEL81 {
|
|||
|
||||
repoMap, err := rpmmd.LoadRepositories(confPaths, r.Name())
|
||||
if err != nil {
|
||||
log.Printf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
return nil
|
||||
return nil, fmt.Errorf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
}
|
||||
|
||||
repos, exists := repoMap["x86_64"]
|
||||
|
|
@ -446,7 +446,7 @@ func New(confPaths []string) *RHEL81 {
|
|||
},
|
||||
}
|
||||
|
||||
return &r
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r *RHEL81) Name() string {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
el81 := rhel81.New([]string{"../../../"})
|
||||
el81, err := rhel81.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := el81.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +96,10 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
el81 := rhel81.New([]string{"../../../"})
|
||||
el81, err := rhel81.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, got1, err := el81.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package rhel82
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -47,7 +48,7 @@ type output struct {
|
|||
const Distro = common.RHEL82
|
||||
const ModulePlatformID = "platform:el8"
|
||||
|
||||
func New(confPaths []string) *RHEL82 {
|
||||
func New(confPaths []string) (*RHEL82, error) {
|
||||
const GigaByte = 1024 * 1024 * 1024
|
||||
|
||||
r := RHEL82{
|
||||
|
|
@ -70,8 +71,7 @@ func New(confPaths []string) *RHEL82 {
|
|||
|
||||
repoMap, err := rpmmd.LoadRepositories(confPaths, r.Name())
|
||||
if err != nil {
|
||||
log.Printf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
return nil
|
||||
return nil, fmt.Errorf("Could not load repository data for %s: %s", r.Name(), err.Error())
|
||||
}
|
||||
|
||||
repos, exists := repoMap["x86_64"]
|
||||
|
|
@ -446,7 +446,7 @@ func New(confPaths []string) *RHEL82 {
|
|||
},
|
||||
}
|
||||
|
||||
return &r
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r *RHEL82) Name() string {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
el82 := rhel82.New([]string{"../../../"})
|
||||
el82, err := rhel82.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := el82.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +96,10 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
el82 := rhel82.New([]string{"../../../"})
|
||||
el82, err := rhel82.New([]string{"../../../"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, got1, err := el82.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue