mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
remove duplicate rows from the summary table (#179)
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
parent
3d91a66536
commit
c46d004a3b
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
|
||||
@ -50,6 +51,7 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
}
|
||||
return false
|
||||
})
|
||||
rows = removeDuplicateRows(rows)
|
||||
|
||||
table := tablewriter.NewWriter(output)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package table
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"github.com/go-test/deep"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
@ -64,3 +65,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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user