Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions syft/presenter/table/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"sort"
"strings"

"github.com/olekukonko/tablewriter"

Expand Down Expand Up @@ -50,6 +51,7 @@ func (pres *Presenter) Present(output io.Writer) error {
}
return false
})
rows = removeDuplicateRows(rows)

table := tablewriter.NewWriter(output)

Expand All @@ -71,3 +73,21 @@ func (pres *Presenter) Present(output io.Writer) error {

return nil
}

func removeDuplicateRows(items [][]string) [][]string {
seen := map[string][]string{}
// nolint:prealloc
var result [][]string

for _, v := range items {
key := strings.Join(v, "|")
if seen[key] != nil {
// dup!
continue
}

seen[key] = v
result = append(result, v)
}
return result
}
30 changes: 30 additions & 0 deletions syft/presenter/table/presenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"bytes"
"flag"
"github.com/go-test/deep"
"testing"

"github.com/anchore/go-testutils"
Expand Down Expand Up @@ -63,3 +64,32 @@ func TestTablePresenter(t *testing.T) {
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
}
}

func TestRemoveDuplicateRows(t *testing.T) {
data := [][]string{
{"1", "2", "3"},
{"a", "b", "c"},
{"1", "2", "3"},
{"a", "b", "c"},
{"1", "2", "3"},
{"4", "5", "6"},
{"1", "2", "1"},
}

expected := [][]string{
{"1", "2", "3"},
{"a", "b", "c"},
{"4", "5", "6"},
{"1", "2", "1"},
}

actual := removeDuplicateRows(data)

if diffs := deep.Equal(expected, actual); len(diffs) > 0 {
t.Errorf("found diffs!")
for _, d := range diffs {
t.Errorf(" diff: %+v", d)
}
}

}