Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion syft/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func NewUnknownDistro() Distro {
}

func NewDistro(t Type, ver string) (Distro, error) {
if ver == "" {
return Distro{Type: t}, nil
}
verObj, err := hashiVer.NewVersion(ver)
if err != nil {
return Distro{}, fmt.Errorf("could not create distro version: %w", err)
Expand All @@ -32,6 +35,9 @@ func NewDistro(t Type, ver string) (Distro, error) {
}

func (d Distro) MajorVersion() string {
if d.Version == nil {
return fmt.Sprint("(version unknown)")
}
return fmt.Sprintf("%d", d.Version.Segments()[0])
}

Expand All @@ -40,7 +46,11 @@ func (d Distro) FullVersion() string {
}

func (d Distro) String() string {
return fmt.Sprintf("%s %s", d.Type, d.RawVersion)
versionStr := "(version unknown)"
if d.RawVersion != "" {
versionStr = d.RawVersion
}
return fmt.Sprintf("%s %s", d.Type, versionStr)
}

// Name provides a string repr of the distro
Expand Down
3 changes: 2 additions & 1 deletion syft/distro/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ identifyLoop:
}

if len(refs) == 0 {
log.Debugf("No Refs found from path: %s", entry.path)
continue
}

Expand Down Expand Up @@ -84,7 +85,7 @@ func assemble(name, version string) *Distro {
distroType, ok := IDMapping[name]

// Both distro and version must be present
if len(name) == 0 || len(version) == 0 {
if len(name) == 0 {
return nil
}

Expand Down
18 changes: 14 additions & 4 deletions syft/distro/identify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func TestIdentifyDistro(t *testing.T) {
fixture: "test-fixtures/os/unmatchable",
Type: UnknownDistroType,
},
{
fixture: "test-fixtures/os/opensuse-leap",
Type: OpenSuseLeap,
Version: "15.2.0",
},
{
fixture: "test-fixtures/os/arch",
Type: ArchLinux,
},
}

observedDistros := internal.NewStringSet()
Expand Down Expand Up @@ -94,6 +103,11 @@ func TestIdentifyDistro(t *testing.T) {
return
}

if d.Version == nil {
t.Log("Distro doesn't have a Version")
return
}

if d.Version.String() != test.Version {
t.Errorf("expected distro version doesn't match: %v != %v", d.Version.String(), test.Version)
}
Expand Down Expand Up @@ -175,10 +189,6 @@ func TestParseOsReleaseFailures(t *testing.T) {
fixture string
name string
}{
{
fixture: "test-fixtures/bad-version",
name: "No version",
},
{
fixture: "test-fixtures/bad-id",
name: "No name ID",
Expand Down
15 changes: 0 additions & 15 deletions syft/distro/test-fixtures/bad-version

This file was deleted.

10 changes: 10 additions & 0 deletions syft/distro/test-fixtures/os/arch/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://www.archlinux.org/"
DOCUMENTATION_URL="https://wiki.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://bugs.archlinux.org/"
LOGO=archlinux
10 changes: 10 additions & 0 deletions syft/distro/test-fixtures/os/opensuse-leap/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.2"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.2"
PRETTY_NAME="openSUSE Leap 15.2"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.2"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
30 changes: 17 additions & 13 deletions syft/distro/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const (
Busybox
AmazonLinux
OracleLinux
//ArchLinux
ArchLinux
OpenSuseLeap
)

type Type int
Expand All @@ -27,7 +28,8 @@ var distroStr = []string{
"busybox",
"amazn",
"oraclelinux",
//"archlinux",
"archlinux",
"opensuse-leap",
}

var All = []Type{
Expand All @@ -40,7 +42,8 @@ var All = []Type{
Busybox,
AmazonLinux,
OracleLinux,
//ArchLinux,
ArchLinux,
OpenSuseLeap,
}

func (t Type) String() string {
Expand All @@ -53,14 +56,15 @@ func (t Type) String() string {

// IDMapping connects a distro ID like "ubuntu" to a Distro type
var IDMapping = map[string]Type{
"debian": Debian,
"ubuntu": Ubuntu,
"rhel": RedHat,
"centos": CentOS,
"fedora": Fedora,
"alpine": Alpine,
"busybox": Busybox,
"amzn": AmazonLinux,
"ol": OracleLinux,
//"arch": ArchLinux,
"debian": Debian,
"ubuntu": Ubuntu,
"rhel": RedHat,
"centos": CentOS,
"fedora": Fedora,
"alpine": Alpine,
"busybox": Busybox,
"amzn": AmazonLinux,
"ol": OracleLinux,
"arch": ArchLinux,
"opensuse-leap": OpenSuseLeap,
}