Skip to content

Commit

Permalink
pkg/imagefilter/formatter: add short more readable output
Browse files Browse the repository at this point in the history
The 'short' output format should provide a better overview
when looking at all combinations.
  • Loading branch information
schuellerf committed Jan 27, 2025
1 parent dd55da4 commit aa79e76
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/imagefilter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"io"
"sort"
"strings"
)

// OutputFormat contains the valid output formats for formatting results
Expand All @@ -15,6 +17,7 @@ const (
OutputFormatText OutputFormat = "text"
OutputFormatJSON OutputFormat = "json"
OutputFormatTextShell OutputFormat = "shell"
OutputFormatTextShort OutputFormat = "short"
)

// ResultFormatter will format the given result list to the given io.Writer
Expand All @@ -31,6 +34,8 @@ func NewResultsFormatter(format OutputFormat) (ResultsFormatter, error) {
return &jsonResultsFormatter{}, nil
case OutputFormatTextShell:
return &shellResultsFormatter{}, nil
case OutputFormatTextShort:
return &textShortResultsFormatter{}, nil
default:
return nil, fmt.Errorf("unsupported formatter %q", format)
}
Expand Down Expand Up @@ -77,6 +82,51 @@ func (*shellResultsFormatter) Output(w io.Writer, all []Result) error {
return nil
}

type textShortResultsFormatter struct{}

func (*textShortResultsFormatter) Output(w io.Writer, all []Result) error {
var errs []error

outputMap := make(map[string]map[string][]string)
for _, res := range all {
if _, ok := outputMap[res.Distro.Name()]; !ok {
outputMap[res.Distro.Name()] = make(map[string][]string)
}
outputMap[res.Distro.Name()][res.ImgType.Name()] = append(outputMap[res.Distro.Name()][res.ImgType.Name()], res.Arch.Name())
}

// Sort and prepare output
var distros []string
for distro := range outputMap {
distros = append(distros, distro)
}
sort.Strings(distros)

for _, distro := range distros {
var types []string
for t := range outputMap[distro] {
types = append(types, t)
}
sort.Strings(types)

var typeArchPairs []string
for _, t := range types {
arches := outputMap[distro][t]
sort.Strings(arches)
typeArchPairs = append(typeArchPairs, fmt.Sprintf("%s: [ %s ]", t, strings.Join(arches, ", ")))
}

if _, err := fmt.Fprintf(w, "%s:\n %s\n", distro, strings.Join(typeArchPairs, "\n ")); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}

type jsonResultsFormatter struct{}

type distroResultJSON struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/imagefilter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ func TestResultsFormatter(t *testing.T) {
[]string{"test-distro-1:qcow2:test_arch3"},
"qcow2 --distro test-distro-1 --arch test_arch3\n",
},
{
"short",
[]string{"test-distro-1:qcow2:test_arch3"},
"test-distro-1:\n qcow2: [ test_arch3 ]\n",
},
{
"short",
[]string{
"test-distro-1:qcow2:test_arch3",
"test-distro-2:qcow2:test_arch3",
},
"test-distro-1:\n qcow2: [ test_arch3 ]\ntest-distro-2:\n qcow2: [ test_arch3 ]\n",
},
{
"short",
[]string{
"test-distro-1:test_type:test_arch",
"test-distro-1:test_type:test_arch2",
},
"test-distro-1:\n test_type: [ test_arch, test_arch2 ]\n",
},
} {
res := make([]imagefilter.Result, len(tc.fakeResults))
for i, resultSpec := range tc.fakeResults {
Expand Down

0 comments on commit aa79e76

Please sign in to comment.