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.
This commit is contained in:
Michael Vogt 2024-11-27 09:04:59 +01:00
parent 56f9c6969c
commit 2719f3f727
6 changed files with 54 additions and 14 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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)

View file

@ -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]`)
}

View file

@ -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)
}