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

imagefilter: add SupportedOutputFormats #1168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/stretchr/testify v1.10.0
github.com/ubccr/kerby v0.0.0-20230802201021-412be7bfaee5
github.com/vmware/govmomi v0.48.0
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
golang.org/x/oauth2 v0.25.0
golang.org/x/sys v0.29.0
golang.org/x/tools v0.26.0
Expand Down Expand Up @@ -168,7 +169,6 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sync v0.10.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/imagefilter/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package imagefilter

var SupportedFormatters = supportedFormatters
32 changes: 21 additions & 11 deletions pkg/imagefilter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

"github.com/osbuild/images/pkg/distrosort"
// we cannot use "maps" yet, as it needs go1.23
"golang.org/x/exp/maps"
)

// OutputFormat contains the valid output formats for formatting results
Expand All @@ -27,20 +29,28 @@ type ResultsFormatter interface {
Output(io.Writer, []Result) error
}

// NewResultFormatter will create a formatter based on the given format.
var supportedFormatters = map[string]ResultsFormatter{
string(OutputFormatDefault): &textResultsFormatter{},
string(OutputFormatText): &textResultsFormatter{},
string(OutputFormatJSON): &jsonResultsFormatter{},
string(OutputFormatTextShell): &shellResultsFormatter{},
string(OutputFormatTextShort): &textShortResultsFormatter{},
}

// SupportedOutputFormats returns a list of supported output formats
func SupportedOutputFormats() []string {
keys := maps.Keys(supportedFormatters)
sort.Strings(keys)
supakeen marked this conversation as resolved.
Show resolved Hide resolved
return keys
}

// NewResultsFormatter will create a formatter based on the given format.
func NewResultsFormatter(format OutputFormat) (ResultsFormatter, error) {
switch format {
case OutputFormatDefault, OutputFormatText:
return &textResultsFormatter{}, nil
case OutputFormatJSON:
return &jsonResultsFormatter{}, nil
case OutputFormatTextShell:
return &shellResultsFormatter{}, nil
case OutputFormatTextShort:
return &textShortResultsFormatter{}, nil
default:
rs, ok := supportedFormatters[string(format)]
if !ok {
return nil, fmt.Errorf("unsupported formatter %q", format)
}
return rs, nil
}

type textResultsFormatter struct{}
Expand Down
17 changes: 17 additions & 0 deletions pkg/imagefilter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package imagefilter_test

import (
"bytes"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -105,3 +106,19 @@ func TestResultsFormatter(t *testing.T) {
assert.Equal(t, tc.expectsOutput, buf.String(), tc)
}
}

func TestSupportedOutputFormats(t *testing.T) {
supakeen marked this conversation as resolved.
Show resolved Hide resolved
formatters := imagefilter.SupportedOutputFormats()
assert.Len(t, formatters, len(imagefilter.SupportedFormatters))
assert.Contains(t, formatters, "text")
assert.Contains(t, formatters, "json")
assert.Contains(t, formatters, "shell")
assert.Contains(t, formatters, "short")
assert.True(t, sort.StringsAreSorted(formatters))
}

func TestResultsFormatterError(t *testing.T) {
fmter, err := imagefilter.NewResultsFormatter(imagefilter.OutputFormat("unsupported"))
assert.Error(t, err)
assert.Nil(t, fmter)
}
Loading