(#342) - remove strong distro check (#496)

* add Type conversion to remove strong distro type limit
Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>

* update signatures to be correct variable from os-release
Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>
This commit is contained in:
Christopher Angelo Phillips 2021-09-09 13:05:22 -04:00 committed by GitHub
parent 2f99a35f51
commit 3f305aa6cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 14 deletions

View File

@ -19,10 +19,12 @@ func NewDistro(t Type, ver, like string) (Distro, error) {
if ver == "" { if ver == "" {
return Distro{Type: t}, nil return Distro{Type: t}, nil
} }
verObj, err := hashiVer.NewVersion(ver) verObj, err := hashiVer.NewVersion(ver)
if err != nil { if err != nil {
return Distro{}, fmt.Errorf("could not create distro version: %w", err) return Distro{}, fmt.Errorf("could not create distro version: %w", err)
} }
return Distro{ return Distro{
Type: t, Type: t,
Version: verObj, Version: verObj,
@ -39,7 +41,7 @@ func (d Distro) Name() string {
// MajorVersion returns the major version value from the pseudo-semantically versioned distro version value. // MajorVersion returns the major version value from the pseudo-semantically versioned distro version value.
func (d Distro) MajorVersion() string { func (d Distro) MajorVersion() string {
if d.Version == nil { if d.Version == nil {
return fmt.Sprint("(version unknown)") return "(version unknown)"
} }
return fmt.Sprintf("%d", d.Version.Segments()[0]) return fmt.Sprintf("%d", d.Version.Segments()[0])
} }

View File

@ -97,11 +97,11 @@ identifyLoop:
return distro return distro
} }
func assemble(name, version, like string) *Distro { func assemble(id, version, like string) *Distro {
distroType, ok := IDMapping[name] distroType, ok := IDMapping[id]
// Both distro and version must be present // Both distro and version must be present
if len(name) == 0 && len(version) == 0 { if len(id) == 0 && len(version) == 0 {
return nil return nil
} }
@ -110,15 +110,17 @@ func assemble(name, version, like string) *Distro {
distroType, ok = IDMapping[like] distroType, ok = IDMapping[like]
} }
if ok { // If we still can't match allow name to be used in constructor
if !ok {
distroType = Type(id)
}
distro, err := NewDistro(distroType, version, like) distro, err := NewDistro(distroType, version, like)
if err != nil { if err != nil {
return nil return nil
} }
return &distro
}
return nil return &distro
} }
func parseOsRelease(contents string) *Distro { func parseOsRelease(contents string) *Distro {

View File

@ -2,16 +2,19 @@ package distro
import ( import (
"fmt" "fmt"
hashiVer "github.com/hashicorp/go-version"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
hashiVer "github.com/hashicorp/go-version"
"github.com/anchore/syft/internal" "github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/source" "github.com/anchore/syft/syft/source"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const CustomDistro Type = "scientific"
func TestIdentifyDistro(t *testing.T) { func TestIdentifyDistro(t *testing.T) {
tests := []struct { tests := []struct {
fixture string fixture string
@ -68,8 +71,9 @@ func TestIdentifyDistro(t *testing.T) {
Type: UnknownDistroType, Type: UnknownDistroType,
}, },
{ {
fixture: "test-fixtures/os/unmatchable", fixture: "test-fixtures/os/custom",
Type: UnknownDistroType, Type: CustomDistro,
Version: "8.0.0",
}, },
{ {
fixture: "test-fixtures/os/opensuse-leap", fixture: "test-fixtures/os/opensuse-leap",