From 2719f3f727056644a9112e2d677b5126dfbc34c4 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 27 Nov 2024 09:04:59 +0100 Subject: [PATCH] list-images: add --datadir override for e.g. custom repositories This commit adds a `--datadir` override that allows overriding the default search paths for e.g. the custom repositories. In practise only repository overrides are supported because that is the only data stored there. --- cmd/image-builder/export_test.go | 8 +++++++- cmd/image-builder/filters.go | 4 ++-- cmd/image-builder/list.go | 8 +++----- cmd/image-builder/main.go | 16 +++++++++++++++- cmd/image-builder/main_test.go | 12 ++++++++++++ cmd/image-builder/repos.go | 20 +++++++++++++++----- 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/cmd/image-builder/export_test.go b/cmd/image-builder/export_test.go index 833e554..0cdb7b3 100644 --- a/cmd/image-builder/export_test.go +++ b/cmd/image-builder/export_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "os" @@ -33,7 +34,12 @@ func MockOsStderr(new io.Writer) (restore func()) { func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore func()) { saved := newRepoRegistry - newRepoRegistry = f + newRepoRegistry = func(dataDir string) (*reporegistry.RepoRegistry, error) { + if dataDir != "" { + panic(fmt.Sprintf("cannot use custom dataDir %v in mock", dataDir)) + } + return f() + } return func() { newRepoRegistry = saved } diff --git a/cmd/image-builder/filters.go b/cmd/image-builder/filters.go index 633a8ca..480be1a 100644 --- a/cmd/image-builder/filters.go +++ b/cmd/image-builder/filters.go @@ -5,9 +5,9 @@ import ( "github.com/osbuild/images/pkg/imagefilter" ) -func newImageFilterDefault() (*imagefilter.ImageFilter, error) { +func newImageFilterDefault(dataDir string) (*imagefilter.ImageFilter, error) { fac := distrofactory.NewDefault() - repos, err := newRepoRegistry() + repos, err := newRepoRegistry(dataDir) if err != nil { return nil, err } diff --git a/cmd/image-builder/list.go b/cmd/image-builder/list.go index d17de9a..81e357e 100644 --- a/cmd/image-builder/list.go +++ b/cmd/image-builder/list.go @@ -1,13 +1,11 @@ package main import ( - "io" - "github.com/osbuild/images/pkg/imagefilter" ) -func listImages(out io.Writer, output string, filterExprs []string) error { - imageFilter, err := newImageFilterDefault() +func listImages(output string, filterExprs []string, opts *cmdlineOpts) error { + imageFilter, err := newImageFilterDefault(opts.dataDir) if err != nil { return err } @@ -21,7 +19,7 @@ func listImages(out io.Writer, output string, filterExprs []string) error { if err != nil { return err } - if err := fmter.Output(out, filteredResult); err != nil { + if err := fmter.Output(opts.out, filteredResult); err != nil { return err } diff --git a/cmd/image-builder/main.go b/cmd/image-builder/main.go index 490f30b..96fe0f9 100644 --- a/cmd/image-builder/main.go +++ b/cmd/image-builder/main.go @@ -14,6 +14,11 @@ var ( osStderr io.Writer = os.Stderr ) +type cmdlineOpts struct { + dataDir string + out io.Writer +} + func cmdListImages(cmd *cobra.Command, args []string) error { filter, err := cmd.Flags().GetStringArray("filter") if err != nil { @@ -23,8 +28,16 @@ func cmdListImages(cmd *cobra.Command, args []string) error { if err != nil { return err } + dataDir, err := cmd.Flags().GetString("datadir") + if err != nil { + return err + } - return listImages(osStdout, output, filter) + opts := &cmdlineOpts{ + out: osStdout, + dataDir: dataDir, + } + return listImages(output, filter, opts) } func run() error { @@ -42,6 +55,7 @@ Image-builder builds operating system images for a range of predefined operating sytsems like centos and RHEL with easy customizations support.`, SilenceErrors: true, } + rootCmd.PersistentFlags().String("datadir", "", `Override the default data direcotry for e.g. custom repositories/*.json data`) rootCmd.SetOut(osStdout) rootCmd.SetErr(osStderr) diff --git a/cmd/image-builder/main_test.go b/cmd/image-builder/main_test.go index f646a0b..2d949e4 100644 --- a/cmd/image-builder/main_test.go +++ b/cmd/image-builder/main_test.go @@ -96,3 +96,15 @@ func TestBadCmdErrorsNoExtraCobraNoise(t *testing.T) { // no extra output from cobra assert.Equal(t, "", fakeStderr.String()) } + +func TestListImagesOverrideDatadir(t *testing.T) { + restore := main.MockOsArgs([]string{"--datadir=/this/path/does/not/exist", "list-images"}) + defer restore() + + var fakeStdout bytes.Buffer + restore = main.MockOsStdout(&fakeStdout) + defer restore() + + err := main.Run() + assert.EqualError(t, err, `no repositories found in the given paths: [/this/path/does/not/exist]`) +} diff --git a/cmd/image-builder/repos.go b/cmd/image-builder/repos.go index 2408f4c..8964753 100644 --- a/cmd/image-builder/repos.go +++ b/cmd/image-builder/repos.go @@ -8,14 +8,24 @@ import ( // that we keep this in sync // XXX2: means we need to depend on osbuild-composer-common or a new rpm // that provides the relevant packages *or* we use go:embed (cf images#1038) -var repositoryConfigs = []string{ +// +// defaultDataDirs contains the default search paths to look for repository +// data. Note that the repositories are under a repositories/ sub-directory +// and contain a bunch of json files of the form "$distro_$version".json +// (but that is an implementation detail that the "images" library takes +// care of). +var defaultDataDirs = []string{ "/etc/osbuild-composer", "/usr/share/osbuild-composer", } -var newRepoRegistry = func() (*reporegistry.RepoRegistry, error) { - // TODO: add a extraReposPaths here so that users can do - // "ibuilder --repositories ..." to add a custom path(s) +var newRepoRegistry = func(dataDir string) (*reporegistry.RepoRegistry, error) { + var dataDirs []string + if dataDir != "" { + dataDirs = []string{dataDir} + } else { + dataDirs = defaultDataDirs + } - return reporegistry.New(repositoryConfigs) + return reporegistry.New(dataDirs) }