Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/image-builder/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"
"os"

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/image-builder/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/image-builder/list.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}

Expand Down
16 changes: 15 additions & 1 deletion cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions cmd/image-builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]`)
}
20 changes: 15 additions & 5 deletions cmd/image-builder/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}