remove duplicate rows from the summary table (#179)

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-09-25 10:32:37 -04:00 committed by GitHub
parent 3d91a66536
commit c46d004a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"sort" "sort"
"strings"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
@ -50,6 +51,7 @@ func (pres *Presenter) Present(output io.Writer) error {
} }
return false return false
}) })
rows = removeDuplicateRows(rows)
table := tablewriter.NewWriter(output) table := tablewriter.NewWriter(output)
@ -71,3 +73,21 @@ func (pres *Presenter) Present(output io.Writer) error {
return nil 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
}

View File

@ -3,6 +3,7 @@ package table
import ( import (
"bytes" "bytes"
"flag" "flag"
"github.com/go-test/deep"
"testing" "testing"
"github.com/anchore/go-testutils" "github.com/anchore/go-testutils"
@ -64,3 +65,32 @@ func TestTablePresenter(t *testing.T) {
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs)) 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)
}
}
}