diff --git a/cmd/root.go b/cmd/root.go index c86f8e82b..1ece97dec 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,6 +55,14 @@ func doRunCmd(cmd *cobra.Command, args []string) int { } defer stereoscope.Cleanup() + log.Info("Identifying Distro") + distro, err := imgbom.IdentifyDistro(img) + if err != nil { + log.Errorf("error identifying Distro: %w", err) + } else { + log.Info(" Distro: %s", distro) + } + log.Info("Cataloging image") catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt) if err != nil { diff --git a/go.mod b/go.mod index 0cdce0b6d..261b4de52 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/google/go-containerregistry v0.0.0-20200521151920-a873a21aff23 // indirect github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99 // indirect github.com/hashicorp/go-multierror v1.1.0 + github.com/hashicorp/go-version v1.2.0 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.1 github.com/sergi/go-diff v1.1.0 diff --git a/go.sum b/go.sum index ed50c7c70..0c6d2ae02 100644 --- a/go.sum +++ b/go.sum @@ -238,6 +238,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= +github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/imgbom/constants.go b/imgbom/constants.go deleted file mode 100644 index 1e64632ee..000000000 --- a/imgbom/constants.go +++ /dev/null @@ -1,4 +0,0 @@ -package imgbom - -// note: must be a single word, all lowercase -const LibraryName = "imgbom" diff --git a/imgbom/distro/distro.go b/imgbom/distro/distro.go new file mode 100644 index 000000000..e9e23364d --- /dev/null +++ b/imgbom/distro/distro.go @@ -0,0 +1,31 @@ +package distro + +import ( + "fmt" + + hashiVer "github.com/hashicorp/go-version" +) + +type Distro struct { + Type Type + Version *hashiVer.Version +} + +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, + }, nil +} + +func (d Distro) MajorVersion() int { + return d.Version.Segments()[0] +} + +func (d Distro) String() string { + return fmt.Sprintf("%s %s", d.Type, d.Version) +} diff --git a/imgbom/distro/identify.go b/imgbom/distro/identify.go new file mode 100644 index 000000000..d5468b4ca --- /dev/null +++ b/imgbom/distro/identify.go @@ -0,0 +1,11 @@ +package distro + +import ( + "github.com/anchore/stereoscope/pkg/image" +) + +func Identify(img *image.Image) (Distro, error) { + // TODO: implement me based off of https://github.com/anchore/anchore-engine/blob/78b23d7e8f007005c070673405b5e23730a660e0/anchore_engine/analyzers/utils.py#L131 + + return NewDistro(UnknownDistro, "0.0.0") +} diff --git a/imgbom/distro/type.go b/imgbom/distro/type.go new file mode 100644 index 000000000..49f1742b3 --- /dev/null +++ b/imgbom/distro/type.go @@ -0,0 +1,52 @@ +package distro + +const ( + UnknownDistro Type = iota + Debian + // Ubuntu + // RedHat + // CentOS + // Fedora + // Alpine + // Busybox + // AmazonLinux + // OracleLinux + // ArchLinux +) + +type Type int + +var distroStr = []string{ + "UnknownDistro", + "Debian", + // "Ubuntu", + // "RedHat", + // "CentOS", + // "Fedora", + // "Alpine", + // "Busybox", + // "AmazonLinux", + // "OracleLinux", + // "ArchLinux", +} + +var All = []Type{ + Debian, + // Ubuntu, + // RedHat, + // CentOS, + // Fedora, + // Alpine, + // Busybox, + // AmazonLinux, + // OracleLinux, + // ArchLinux, +} + +func (t Type) String() string { + if int(t) >= len(distroStr) || t < 0 { + return distroStr[0] + } + + return distroStr[t] +} diff --git a/imgbom/catalog_image.go b/imgbom/lib.go similarity index 57% rename from imgbom/catalog_image.go rename to imgbom/lib.go index 458b3594a..036149103 100644 --- a/imgbom/catalog_image.go +++ b/imgbom/lib.go @@ -2,19 +2,27 @@ package imgbom import ( "github.com/anchore/imgbom/imgbom/analyzer" + "github.com/anchore/imgbom/imgbom/distro" + "github.com/anchore/imgbom/imgbom/logger" "github.com/anchore/imgbom/imgbom/pkg" "github.com/anchore/imgbom/imgbom/scope" + "github.com/anchore/imgbom/internal/log" "github.com/anchore/stereoscope/pkg/image" ) -// TODO: add os detection results as return value +func IdentifyDistro(img *image.Image) (distro.Distro, error) { + return distro.Identify(img) +} + func CatalogImage(img *image.Image, o scope.Option) (*pkg.Catalog, error) { s, err := scope.NewScope(img, o) if err != nil { return nil, err } - // TODO: add OS detection here... - return analyzer.Analyze(s) } + +func SetLogger(logger logger.Logger) { + log.Log = logger +} diff --git a/imgbom/log.go b/imgbom/log.go deleted file mode 100644 index ac2d5d4d1..000000000 --- a/imgbom/log.go +++ /dev/null @@ -1,10 +0,0 @@ -package imgbom - -import ( - "github.com/anchore/imgbom/imgbom/logger" - "github.com/anchore/imgbom/internal/log" -) - -func SetLogger(logger logger.Logger) { - log.Log = logger -}