Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short output #1167

Merged
merged 2 commits into from
Jan 27, 2025
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
16 changes: 8 additions & 8 deletions docs/image-types/rhel8/google-gce.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ The high-level description of the workflow used to build RHEL Guest Images is as
4. Delete all created resources.

RHEL Guest Images are imported with the following Guest OS features set:
* UEFI_COMPATIBLE
* VIRTIO_SCSI_MULTIQUEUE
* SEV_CAPABLE
* SEV_SNP_CAPABLE
* SEV_LIVE_MIGRATABLE
* SEV_LIVE_MIGRATABLE_V2
* GVNIC
* IDPF
* `UEFI_COMPATIBLE`
* `VIRTIO_SCSI_MULTIQUEUE`
* `SEV_CAPABLE`
* `SEV_SNP_CAPABLE`
* `SEV_LIVE_MIGRATABLE`
* `SEV_LIVE_MIGRATABLE_V2`
* `GVNIC`
* `IDPF`


[daisy-tool]: https://github.com/GoogleCloudPlatform/compute-image-tools/tree/master/daisy
Expand Down
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
Loading