Skip to content

Commit

Permalink
fixup! rebase on distrofilter branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Nov 6, 2024
1 parent 6ed22e2 commit eca4753
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 201 deletions.
138 changes: 12 additions & 126 deletions cmd/image-builder/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@ package main

import (
"fmt"
"strings"

"github.com/sirupsen/logrus"

"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/distrosort"
"github.com/osbuild/images/pkg/imagefilter"
)

// XXX: move this file into distro factory?

type FilterResult struct {
Distro distro.Distro
Arch distro.Arch
ImgType distro.ImageType
}
func getOneImage(distroName, imgTypeStr, archStr string) (*imagefilter.Result, error) {
fac := distrofactory.NewDefault()
repos, err := newRepoRegistry()
if err != nil {
return nil, err
}
imageFilter, err := imagefilter.New(fac, repos)
if err != nil {
return nil, err
}

func getOneImage(distroName, imgTypeStr, archStr string) (*FilterResult, error) {
filterExprs := []string{
fmt.Sprintf("name:%s", distroName),
fmt.Sprintf("arch:%s", archStr),
fmt.Sprintf("type:%s", imgTypeStr),
}
filteredResults, err := getFilteredImages(filterExprs)
filteredResults, err := imageFilter.Filter(filterExprs...)
if err != nil {
return nil, err
}
Expand All @@ -38,115 +36,3 @@ func getOneImage(distroName, imgTypeStr, archStr string) (*FilterResult, error)
return nil, fmt.Errorf("internal error: found %v results for %s %s %s", len(filteredResults), distroName, imgTypeStr, archStr)
}
}

// XXX: rename FilterDistros to FilterImages(?)
func FilterDistros(fac *distrofactory.Factory, distroNames []string, filters Filters) ([]FilterResult, error) {
var res []FilterResult

if err := distrosort.Names(distroNames); err != nil {
return nil, err
}
for _, distroName := range distroNames {
distro := fac.GetDistro(distroName)
if distro == nil {
logrus.Debugf("skipping %v: has repositories but unsupported", distroName)
}
for _, archName := range distro.ListArches() {
a, err := distro.GetArch(archName)
if err != nil {
return nil, err
}
for _, imgTypeName := range a.ListImageTypes() {
imgType, err := a.GetImageType(imgTypeName)
if err != nil {
return nil, err
}
if filters.Matches(distro, a, imgType) {
res = append(res, FilterResult{distro, a, imgType})
}
}
}
}

return res, nil
}

// Filters is a way to filter a list of distros
type Filters []filter

// NewFilters creates a filtering for a list of distros
func NewFilters(sl []string) (Filters, error) {
var filters []filter
for _, s := range sl {
l := strings.SplitN(s, ":", 2)
switch l[0] {
case s:
filters = append(filters, &distroNameFilter{
filter: l[0],
exact: false,
})
case "name":
filters = append(filters, &distroNameFilter{l[1], true})
case "arch":
filters = append(filters, &archFilter{l[1]})
case "type":
filters = append(filters, &imgTypeFilter{l[1]})
// mostly here to show how powerful this is
case "bootmode":
filters = append(filters, &bootmodeFilter{l[1]})
default:
return nil, fmt.Errorf("unsupported filter prefix: %q", l[0])
}
}
return filters, nil
}

// Matches returns true if the given (distro,arch,imgType) tuple matches
// the filter expressions
func (fl Filters) Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool {
matches := true
for _, f := range fl {
matches = matches && f.Matches(distro, arch, imgType)
}
return matches
}

type filter interface {
Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool
}

type distroNameFilter struct {
filter string
exact bool
}

func (d *distroNameFilter) Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool {
if d.exact {
return distro.Name() == d.filter
}
return strings.Contains(distro.Name(), d.filter)
}

type archFilter struct {
filter string
}

func (d *archFilter) Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool {
return strings.Contains(arch.Name(), d.filter)
}

type imgTypeFilter struct {
filter string
}

func (d *imgTypeFilter) Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool {
return strings.Contains(imgType.Name(), d.filter)
}

type bootmodeFilter struct {
filter string
}

func (d *bootmodeFilter) Matches(distro distro.Distro, arch distro.Arch, imgType distro.ImageType) bool {
return imgType.BootMode().String() == d.filter
}
8 changes: 5 additions & 3 deletions cmd/image-builder/filters_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"errors"
"fmt"
"io"

"github.com/osbuild/images/pkg/imagefilter"
)

type FilteredResultFormatter interface {
Output(io.Writer, []FilterResult) error
Output(io.Writer, []imagefilter.Result) error
}

func NewFilteredResultFormatter(format string) (FilteredResultFormatter, error) {
Expand All @@ -24,7 +26,7 @@ func NewFilteredResultFormatter(format string) (FilteredResultFormatter, error)

type textFilteredResultFormatter struct{}

func (*textFilteredResultFormatter) Output(w io.Writer, all []FilterResult) error {
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 {
Expand Down Expand Up @@ -53,7 +55,7 @@ type filteredResultJSON struct {
} `json:"image_type"`
}

func (*jsonFilteredResultFormatter) Output(w io.Writer, all []FilterResult) error {
func (*jsonFilteredResultFormatter) Output(w io.Writer, all []imagefilter.Result) error {
var out []filteredResultJSON

for _, res := range all {
Expand Down
5 changes: 4 additions & 1 deletion cmd/image-builder/filters_fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/osbuild/images/cmd/image-builder"
"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/imagefilter"
)

func TestFitleredResultFormatter(t *testing.T) {
Expand All @@ -34,7 +35,9 @@ func TestFitleredResultFormatter(t *testing.T) {
require.NoError(t, err)

var buf bytes.Buffer
res := []main.FilterResult{{di, ar, im}}
res := []imagefilter.Result{
{Distro: di, Arch: ar, ImgType: im},
}
fmter, err := main.NewFilteredResultFormatter(tc.formatter)
require.NoError(t, err)
err = fmter.Output(&buf, res)
Expand Down
62 changes: 0 additions & 62 deletions cmd/image-builder/filters_test.go

This file was deleted.

16 changes: 7 additions & 9 deletions cmd/image-builder/list_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@ import (
"io"

"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/imagefilter"
)

func getFilteredImages(filterExprs []string) ([]FilterResult, error) {
func listImages(out io.Writer, format string, filterExprs []string) error {
fac := distrofactory.NewDefault()
repos, err := newRepoRegistry()
if err != nil {
return nil, err
return err
}
filters, err := NewFilters(filterExprs)
imageFilter, err := imagefilter.New(fac, repos)
if err != nil {
return nil, err
return err
}
fac := distrofactory.NewDefault()
return FilterDistros(fac, repos.ListDistros(), filters)
}

func listImages(out io.Writer, format string, filterExprs []string) error {
filteredResult, err := getFilteredImages(filterExprs)
filteredResult, err := imageFilter.Filter(filterExprs...)
if err != nil {
return err
}
Expand Down

0 comments on commit eca4753

Please sign in to comment.