Skip to content

Commit

Permalink
pkg/imagefilter/formatter: add shell output
Browse files Browse the repository at this point in the history
This should implement the initial idea of a
copy'n'paste ready list of possibilities.
  • Loading branch information
schuellerf committed Jan 24, 2025
1 parent dd54f7b commit 16cb0bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 26 additions & 3 deletions pkg/imagefilter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
type OutputFormat string

const (
OutputFormatDefault OutputFormat = ""
OutputFormatText OutputFormat = "text"
OutputFormatJSON OutputFormat = "json"
OutputFormatDefault OutputFormat = ""
OutputFormatText OutputFormat = "text"
OutputFormatJSON OutputFormat = "json"
OutputFormatTextShell OutputFormat = "shell"
)

// ResultFormatter will format the given result list to the given io.Writer
Expand All @@ -28,6 +29,8 @@ func NewResultsFormatter(format OutputFormat) (ResultsFormatter, error) {
return &textResultsFormatter{}, nil
case OutputFormatJSON:
return &jsonResultsFormatter{}, nil
case OutputFormatTextShell:
return &shellResultsFormatter{}, nil
default:
return nil, fmt.Errorf("unsupported formatter %q", format)
}
Expand All @@ -54,6 +57,26 @@ func (*textResultsFormatter) Output(w io.Writer, all []Result) error {
return nil
}

type shellResultsFormatter struct{}

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

for _, res := range all {
if _, err := fmt.Fprintf(w, "%s --distro %s --arch %s\n",
res.ImgType.Name(),
res.Distro.Name(),
res.Arch.Name()); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}

type jsonResultsFormatter struct{}

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

0 comments on commit 16cb0bd

Please sign in to comment.