mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
allow root command to catalog a directory
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
466169e8d7
commit
4d31655908
50
cmd/root.go
50
cmd/root.go
@ -17,13 +17,14 @@ import (
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf("%s [IMAGE]", internal.ApplicationName),
|
||||
Short: "A container image BOM tool", // TODO: add copy
|
||||
Use: fmt.Sprintf("%s [SOURCE]", internal.ApplicationName),
|
||||
Short: "A tool that generates a Software Build Of Materials (SBOM)",
|
||||
Long: internal.Tprintf(`\
|
||||
Supports the following image sources:
|
||||
{{.appName}} yourrepo/yourimage:tag defaults to using images from a docker daemon
|
||||
{{.appName}} docker://yourrepo/yourimage:tag explicitly use the docker daemon
|
||||
{{.appName}} tar://path/to/yourimage.tar use a tarball from disk
|
||||
{{.appName}} dir://path/to/yourproject read directly from a path in disk
|
||||
`, map[string]interface{}{
|
||||
"appName": internal.ApplicationName,
|
||||
}),
|
||||
@ -44,38 +45,38 @@ func init() {
|
||||
)
|
||||
}
|
||||
|
||||
func startWorker(userImage string) <-chan error {
|
||||
func startWorker(userInput string) <-chan error {
|
||||
errs := make(chan error)
|
||||
go func() {
|
||||
defer close(errs)
|
||||
|
||||
log.Infof("Fetching image '%s'", userImage)
|
||||
img, err := stereoscope.GetImage(userImage)
|
||||
if err != nil {
|
||||
errs <- fmt.Errorf("could not fetch image '%s': %w", userImage, err)
|
||||
return
|
||||
}
|
||||
defer stereoscope.Cleanup()
|
||||
|
||||
log.Info("Identifying Distro")
|
||||
distro := imgbom.IdentifyDistro(img)
|
||||
if distro == nil {
|
||||
log.Errorf("error identifying distro")
|
||||
} else {
|
||||
log.Infof(" Distro: %s", distro)
|
||||
}
|
||||
|
||||
log.Info("Cataloging image")
|
||||
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
|
||||
protocol := imgbom.NewProtocol(userInput)
|
||||
fmt.Printf("protocol: %+v", protocol)
|
||||
catalog, err := imgbom.Catalog(protocol, appConfig.ScopeOpt)
|
||||
if err != nil {
|
||||
errs <- fmt.Errorf("could not catalog image: %w", err)
|
||||
}
|
||||
|
||||
log.Info("Complete!")
|
||||
switch protocol.Type {
|
||||
case imgbom.DirProtocol:
|
||||
log.Info("Cataloging directory")
|
||||
bus.Publish(partybus.Event{
|
||||
Type: event.CatalogerFinished,
|
||||
Value: presenter.GetPresenter(appConfig.PresenterOpt, img, catalog),
|
||||
Value: presenter.GetDirPresenter(appConfig.PresenterOpt, catalog),
|
||||
})
|
||||
default:
|
||||
log.Info("Cataloging image")
|
||||
log.Infof("Fetching image '%s'", userInput)
|
||||
img, err := stereoscope.GetImage(userInput)
|
||||
if err != nil {
|
||||
errs <- fmt.Errorf("could not fetch image '%s': %w", userInput, err)
|
||||
}
|
||||
defer stereoscope.Cleanup()
|
||||
|
||||
bus.Publish(partybus.Event{
|
||||
Type: event.CatalogerFinished,
|
||||
Value: presenter.GetImgPresenter(appConfig.PresenterOpt, img, catalog),
|
||||
})
|
||||
}
|
||||
}()
|
||||
return errs
|
||||
}
|
||||
@ -86,4 +87,5 @@ func doRunCmd(_ *cobra.Command, args []string) int {
|
||||
ux := ui.Select(appConfig.CliOptions.Verbosity > 0, appConfig.Quiet)
|
||||
|
||||
return ux(errs, eventSubscription)
|
||||
|
||||
}
|
||||
|
||||
@ -1,119 +0,0 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/imgbom/internal/log"
|
||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
||||
)
|
||||
|
||||
type Presenter struct {
|
||||
img *stereoscopeImg.Image
|
||||
catalog *pkg.Catalog
|
||||
}
|
||||
|
||||
func NewPresenter(img *stereoscopeImg.Image, catalog *pkg.Catalog) *Presenter {
|
||||
return &Presenter{
|
||||
img: img,
|
||||
catalog: catalog,
|
||||
}
|
||||
}
|
||||
|
||||
type document struct {
|
||||
Image image `json:"image"`
|
||||
Artifacts []artifact `json:"artifacts"`
|
||||
}
|
||||
|
||||
type image struct {
|
||||
Layers []layer `json:"layers"`
|
||||
Size int64 `json:"size"`
|
||||
Digest string `json:"digest"`
|
||||
MediaType string `json:"mediaType"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
type layer struct {
|
||||
MediaType string `json:"mediaType"`
|
||||
Digest string `json:"digest"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type source struct {
|
||||
FoundBy string `json:"foundBy"`
|
||||
Layer int `json:"layer"`
|
||||
Effects []string `json:"effects"`
|
||||
}
|
||||
|
||||
type artifact struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Type string `json:"type"`
|
||||
Cataloger string `json:"cataloger"`
|
||||
Sources []source `json:"sources"`
|
||||
Metadata interface{} `json:"metadata"`
|
||||
}
|
||||
|
||||
func (pres *Presenter) Present(output io.Writer) error {
|
||||
tags := make([]string, len(pres.img.Metadata.Tags))
|
||||
for idx, tag := range pres.img.Metadata.Tags {
|
||||
tags[idx] = tag.String()
|
||||
}
|
||||
|
||||
doc := document{
|
||||
Image: image{
|
||||
Digest: pres.img.Metadata.Digest,
|
||||
Size: pres.img.Metadata.Size,
|
||||
MediaType: string(pres.img.Metadata.MediaType),
|
||||
Tags: tags,
|
||||
Layers: make([]layer, len(pres.img.Layers)),
|
||||
},
|
||||
Artifacts: make([]artifact, 0),
|
||||
}
|
||||
|
||||
// populate image...
|
||||
for idx, l := range pres.img.Layers {
|
||||
doc.Image.Layers[idx] = layer{
|
||||
MediaType: string(l.Metadata.MediaType),
|
||||
Digest: l.Metadata.Digest,
|
||||
Size: l.Metadata.Size,
|
||||
}
|
||||
}
|
||||
|
||||
// populate artifacts...
|
||||
for p := range pres.catalog.Enumerate() {
|
||||
art := artifact{
|
||||
Name: p.Name,
|
||||
Version: p.Version,
|
||||
Type: p.Type.String(),
|
||||
Sources: make([]source, len(p.Source)),
|
||||
Metadata: p.Metadata,
|
||||
}
|
||||
|
||||
for idx, src := range p.Source {
|
||||
fileMetadata, err := pres.img.FileCatalog.Get(src)
|
||||
if err != nil {
|
||||
// TODO: test case
|
||||
log.Errorf("could not get metadata from catalog (presenter=json): %+v", src)
|
||||
}
|
||||
|
||||
srcObj := source{
|
||||
FoundBy: p.FoundBy,
|
||||
Layer: int(fileMetadata.Source.Metadata.Index),
|
||||
Effects: []string{}, // TODO
|
||||
}
|
||||
art.Sources[idx] = srcObj
|
||||
}
|
||||
|
||||
doc.Artifacts = append(doc.Artifacts, art)
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(&doc)
|
||||
if err != nil {
|
||||
log.Errorf("failed to marshal json (presenter=json): %w", err)
|
||||
}
|
||||
|
||||
_, err = output.Write(bytes)
|
||||
return err
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user