Skip to content

Commit

Permalink
imagefilter: implement SupportedOutputFormats()
Browse files Browse the repository at this point in the history
Implements function to get a list of all supported output formats.
  • Loading branch information
schuellerf committed Jan 31, 2025
1 parent 2621d49 commit b6de0d5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
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 SupportedFormatterMap = 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)
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
11 changes: 11 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,13 @@ func TestResultsFormatter(t *testing.T) {
assert.Equal(t, tc.expectsOutput, buf.String(), tc)
}
}

func TestSupportedOutputFormats(t *testing.T) {
formatters := imagefilter.SupportedOutputFormats()
assert.Len(t, formatters, len(imagefilter.SupportedFormatterMap))
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))
}

0 comments on commit b6de0d5

Please sign in to comment.