diff --git a/cmd/cli.go b/cmd/cli.go index 1035dc785..a337dfaea 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -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 { diff --git a/imgbom/presenter/option.go b/imgbom/presenter/option.go index 354e84754..91f628fc1 100644 --- a/imgbom/presenter/option.go +++ b/imgbom/presenter/option.go @@ -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 } diff --git a/imgbom/presenter/presenter.go b/imgbom/presenter/presenter.go index ed2eab414..421618985 100644 --- a/imgbom/presenter/presenter.go +++ b/imgbom/presenter/presenter.go @@ -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 } diff --git a/imgbom/presenter/text/presenter.go b/imgbom/presenter/text/presenter.go new file mode 100644 index 000000000..016354526 --- /dev/null +++ b/imgbom/presenter/text/presenter.go @@ -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 +} diff --git a/imgbom/presenter/text/presenter_test.go b/imgbom/presenter/text/presenter_test.go new file mode 100644 index 000000000..797424b51 --- /dev/null +++ b/imgbom/presenter/text/presenter_test.go @@ -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)) + } + +} diff --git a/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile b/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile new file mode 100644 index 000000000..79cfa759e --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile @@ -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 diff --git a/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt b/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt new file mode 100644 index 000000000..985d3408e --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt @@ -0,0 +1 @@ +this file has contents \ No newline at end of file diff --git a/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt b/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt new file mode 100644 index 000000000..396d08bbc --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt @@ -0,0 +1 @@ +file-2 contents! \ No newline at end of file diff --git a/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden new file mode 100644 index 000000000..7e3610bde --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden @@ -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 +