mirror of
https://github.com/anchore/syft
synced 2024-11-10 06:14:16 +00:00
Merge pull request #64 from anchore/issue-36
Create a text presenter, default to it
This commit is contained in:
commit
d2e6229b2e
9 changed files with 167 additions and 1 deletions
|
@ -28,7 +28,7 @@ func setCliOptions() {
|
|||
// output & formatting options
|
||||
flag = "output"
|
||||
rootCmd.Flags().StringP(
|
||||
flag, "o", presenter.JSONPresenter.String(),
|
||||
flag, "o", presenter.TextPresenter.String(),
|
||||
fmt.Sprintf("report output formatter, options=%v", presenter.Options),
|
||||
)
|
||||
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
|
||||
|
|
|
@ -5,15 +5,18 @@ import "strings"
|
|||
const (
|
||||
UnknownPresenter Option = iota
|
||||
JSONPresenter
|
||||
TextPresenter
|
||||
)
|
||||
|
||||
var optionStr = []string{
|
||||
"UnknownPresenter",
|
||||
"json",
|
||||
"text",
|
||||
}
|
||||
|
||||
var Options = []Option{
|
||||
JSONPresenter,
|
||||
TextPresenter,
|
||||
}
|
||||
|
||||
type Option int
|
||||
|
@ -22,6 +25,8 @@ func ParseOption(userStr string) Option {
|
|||
switch strings.ToLower(userStr) {
|
||||
case strings.ToLower(JSONPresenter.String()):
|
||||
return JSONPresenter
|
||||
case strings.ToLower(TextPresenter.String()):
|
||||
return TextPresenter
|
||||
default:
|
||||
return UnknownPresenter
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/imgbom/imgbom/presenter/json"
|
||||
"github.com/anchore/imgbom/imgbom/presenter/text"
|
||||
"github.com/anchore/stereoscope/pkg/image"
|
||||
)
|
||||
|
||||
|
@ -16,6 +17,8 @@ func GetPresenter(option Option) Presenter {
|
|||
switch option {
|
||||
case JSONPresenter:
|
||||
return json.NewPresenter()
|
||||
case TextPresenter:
|
||||
return text.NewPresenter()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
|
56
imgbom/presenter/text/presenter.go
Normal file
56
imgbom/presenter/text/presenter.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
||||
)
|
||||
|
||||
// Presenter holds the Present method to produce output
|
||||
type Presenter struct{}
|
||||
|
||||
// NewPresenter is a constructor for a Presenter
|
||||
func NewPresenter() *Presenter {
|
||||
return &Presenter{}
|
||||
}
|
||||
|
||||
// Present is a method that is in charge of writing to an output buffer
|
||||
func (pres *Presenter) Present(output io.Writer, img *stereoscopeImg.Image, catalog *pkg.Catalog) error {
|
||||
tags := make([]string, len(img.Metadata.Tags))
|
||||
for idx, tag := range img.Metadata.Tags {
|
||||
tags[idx] = tag.String()
|
||||
}
|
||||
|
||||
// init the tabular writer
|
||||
w := new(tabwriter.Writer)
|
||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
||||
|
||||
fmt.Fprintln(w, "[Image]")
|
||||
|
||||
for idx, l := range img.Layers {
|
||||
fmt.Fprintln(w, " Layer:\t", idx)
|
||||
fmt.Fprintln(w, " Digest:\t", l.Metadata.Digest)
|
||||
fmt.Fprintln(w, " Size:\t", l.Metadata.Size)
|
||||
fmt.Fprintln(w, " MediaType:\t", l.Metadata.MediaType)
|
||||
fmt.Fprintln(w)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
// populate artifacts...
|
||||
for p := range catalog.Enumerate() {
|
||||
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
||||
fmt.Fprintln(w, " Type:\t", p.Type.String())
|
||||
if p.Metadata != nil {
|
||||
fmt.Fprintf(w, " Metadata:\t%+v\n", p.Metadata)
|
||||
}
|
||||
fmt.Fprintln(w, " Found by:\t", p.FoundBy)
|
||||
fmt.Fprintln(w)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
74
imgbom/presenter/text/presenter_test.go
Normal file
74
imgbom/presenter/text/presenter_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||
|
||||
type PackageInfo struct {
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
func TestTextPresenter(t *testing.T) {
|
||||
pres := NewPresenter()
|
||||
var buffer bytes.Buffer
|
||||
|
||||
catalog := pkg.NewCatalog()
|
||||
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-simple")
|
||||
defer cleanup()
|
||||
|
||||
// populate catalog with test data
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-1",
|
||||
Version: "1.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-1.txt"),
|
||||
},
|
||||
FoundBy: "dpkg",
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-2",
|
||||
Version: "2.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-2.txt"),
|
||||
},
|
||||
FoundBy: "dpkg",
|
||||
Metadata: PackageInfo{Name: "package-2", Version: "1.0.2"},
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
|
||||
// stub out all the digests so that they don't affect tests comparisons
|
||||
// TODO: update with go-testutils feature when issue #1 is resolved
|
||||
for _, l := range img.Layers {
|
||||
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
||||
}
|
||||
|
||||
// run presenter
|
||||
err := pres.Present(&buffer, img, catalog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual := buffer.Bytes()
|
||||
if *update {
|
||||
testutils.UpdateGoldenFileContents(t, actual)
|
||||
}
|
||||
|
||||
var expected = testutils.GetGoldenFileContents(t)
|
||||
|
||||
if !bytes.Equal(expected, actual) {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
||||
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one.
|
||||
FROM scratch
|
||||
ADD file-1.txt /somefile-1.txt
|
||||
ADD file-2.txt /somefile-2.txt
|
|
@ -0,0 +1 @@
|
|||
this file has contents
|
|
@ -0,0 +1 @@
|
|||
file-2 contents!
|
|
@ -0,0 +1,22 @@
|
|||
[Image]
|
||||
Layer: 0
|
||||
Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53
|
||||
Size: 22
|
||||
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
|
||||
|
||||
Layer: 1
|
||||
Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53
|
||||
Size: 16
|
||||
MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip
|
||||
|
||||
[package-1]
|
||||
Version: 1.0.1
|
||||
Type: deb
|
||||
Found by: dpkg
|
||||
|
||||
[package-2]
|
||||
Version: 2.0.1
|
||||
Type: deb
|
||||
Metadata: {Name:package-2 Version:1.0.2}
|
||||
Found by: dpkg
|
||||
|
Loading…
Reference in a new issue