syft/imgbom/distro/distro.go
Alfredo Deza f51cae2043 distro: add new constructor for getting UnkownDistros
Signed-off-by: Alfredo Deza <adeza@anchore.com>
2020-07-17 13:32:53 -04:00

50 lines
948 B
Go

package distro
import (
"fmt"
hashiVer "github.com/hashicorp/go-version"
)
type Distro struct {
Type Type
Version *hashiVer.Version
RawVersion string
}
// NewUnknownDistro creates a standardized UnkownDistro with a "0.0.0" version
func NewUnknownDistro() Distro {
return Distro{
Type: UnknownDistro,
}
}
func NewDistro(t Type, ver string) (Distro, error) {
verObj, err := hashiVer.NewVersion(ver)
if err != nil {
return Distro{}, fmt.Errorf("could not create distro version: %w", err)
}
return Distro{
Type: t,
Version: verObj,
RawVersion: ver,
}, nil
}
func (d Distro) MajorVersion() string {
return fmt.Sprintf("%d", d.Version.Segments()[0])
}
func (d Distro) FullVersion() string {
return d.RawVersion
}
func (d Distro) String() string {
return fmt.Sprintf("%s %s", d.Type, d.RawVersion)
}
// Name provides a string repr of the distro
func (d Distro) Name() string {
return d.Type.String()
}