diff --git a/cmd/image-builder/filters_fmt.go b/cmd/image-builder/filters_fmt.go deleted file mode 100644 index 2de69a8e79..0000000000 --- a/cmd/image-builder/filters_fmt.go +++ /dev/null @@ -1,83 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "fmt" - "io" - - "github.com/osbuild/images/pkg/imagefilter" -) - -type FilteredResultFormatter interface { - Output(io.Writer, []imagefilter.Result) error -} - -func NewFilteredResultFormatter(format string) (FilteredResultFormatter, error) { - switch format { - case "", "text": - return &textFilteredResultFormatter{}, nil - case "json": - return &jsonFilteredResultFormatter{}, nil - default: - return nil, fmt.Errorf("unsupported formatter %q", format) - } -} - -type textFilteredResultFormatter struct{} - -func (*textFilteredResultFormatter) Output(w io.Writer, all []imagefilter.Result) error { - var errs []error - for _, res := range all { - if _, err := fmt.Fprintf(w, "%s arch:%s type:%s\n", res.Distro.Name(), res.Arch.Name(), res.ImgType.Name()); err != nil { - errs = append(errs, err) - } - } - if len(errs) > 0 { - return errors.Join(errs...) - } - - return nil -} - -type jsonFilteredResultFormatter struct{} - -// XXX: consider adding more details to the output -type filteredResultJSON struct { - Distro struct { - Name string `json:"name"` - } `json:"distro"` - Arch struct { - Name string `json:"name"` - } `json:"arch"` - ImgType struct { - Name string `json:"name"` - } `json:"image_type"` -} - -func (*jsonFilteredResultFormatter) Output(w io.Writer, all []imagefilter.Result) error { - var out []filteredResultJSON - - for _, res := range all { - out = append(out, filteredResultJSON{ - Distro: struct { - Name string `json:"name"` - }{ - Name: res.Distro.Name(), - }, - Arch: struct { - Name string `json:"name"` - }{ - Name: res.Arch.Name(), - }, - ImgType: struct { - Name string `json:"name"` - }{ - Name: res.ImgType.Name(), - }, - }) - } - - enc := json.NewEncoder(w) - return enc.Encode(out) -} diff --git a/cmd/image-builder/filters_fmt_test.go b/cmd/image-builder/filters_fmt_test.go deleted file mode 100644 index 99750733aa..0000000000 --- a/cmd/image-builder/filters_fmt_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package main_test - -import ( - "bytes" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/osbuild/images/cmd/image-builder" - "github.com/osbuild/images/pkg/distrofactory" - "github.com/osbuild/images/pkg/imagefilter" -) - -func TestFitleredResultFormatter(t *testing.T) { - fac := distrofactory.NewTestDefault() - - for _, tc := range []struct { - formatter string - distro, arch, imgType string - expectsOutput string - }{ - {"", "test-distro-1", "test_arch3", "qcow2", `test-distro-1 arch:test_arch3 type:qcow2` + "\n"}, - {"text", "test-distro-1", "test_arch3", "qcow2", `test-distro-1 arch:test_arch3 type:qcow2` + "\n"}, - {"json", "test-distro-1", "test_arch3", "qcow2", `[{"distro":{"name":"test-distro-1"},"arch":{"name":"test_arch3"},"image_type":{"name":"qcow2"}}]` + "\n"}, - } { - // XXX: it would be nice if TestDistro would support constructing - // like GetDistro("rhel-8.1:i386,amd64:ami,qcow2") instead of - // the current very static setup - di := fac.GetDistro(tc.distro) - require.NotNil(t, di) - ar, err := di.GetArch(tc.arch) - require.NoError(t, err) - im, err := ar.GetImageType(tc.imgType) - require.NoError(t, err) - - var buf bytes.Buffer - res := []imagefilter.Result{ - {Distro: di, Arch: ar, ImgType: im}, - } - fmter, err := main.NewFilteredResultFormatter(tc.formatter) - require.NoError(t, err) - err = fmter.Output(&buf, res) - assert.NoError(t, err) - assert.Equal(t, tc.expectsOutput, buf.String(), tc) - } -} diff --git a/cmd/image-builder/list_images.go b/cmd/image-builder/list_images.go index faebd47b5f..9622b007ba 100644 --- a/cmd/image-builder/list_images.go +++ b/cmd/image-builder/list_images.go @@ -2,6 +2,8 @@ package main import ( "io" + + "github.com/osbuild/images/pkg/imagefilter" ) func listImages(out io.Writer, format string, filterExprs []string) error { @@ -15,7 +17,7 @@ func listImages(out io.Writer, format string, filterExprs []string) error { return err } - fmter, err := NewFilteredResultFormatter(format) + fmter, err := imagefilter.NewResultsFormatter(imagefilter.OutputFormat(format)) if err != nil { return err }