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:
parent
56f9c6969c
commit
2719f3f727
6 changed files with 54 additions and 14 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
@ -33,7 +34,12 @@ func MockOsStderr(new io.Writer) (restore func()) {
|
||||||
|
|
||||||
func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore func()) {
|
func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore func()) {
|
||||||
saved := newRepoRegistry
|
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() {
|
return func() {
|
||||||
newRepoRegistry = saved
|
newRepoRegistry = saved
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"github.com/osbuild/images/pkg/imagefilter"
|
"github.com/osbuild/images/pkg/imagefilter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newImageFilterDefault() (*imagefilter.ImageFilter, error) {
|
func newImageFilterDefault(dataDir string) (*imagefilter.ImageFilter, error) {
|
||||||
fac := distrofactory.NewDefault()
|
fac := distrofactory.NewDefault()
|
||||||
repos, err := newRepoRegistry()
|
repos, err := newRepoRegistry(dataDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/osbuild/images/pkg/imagefilter"
|
"github.com/osbuild/images/pkg/imagefilter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func listImages(out io.Writer, output string, filterExprs []string) error {
|
func listImages(output string, filterExprs []string, opts *cmdlineOpts) error {
|
||||||
imageFilter, err := newImageFilterDefault()
|
imageFilter, err := newImageFilterDefault(opts.dataDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -21,7 +19,7 @@ func listImages(out io.Writer, output string, filterExprs []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := fmter.Output(out, filteredResult); err != nil {
|
if err := fmter.Output(opts.out, filteredResult); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,11 @@ var (
|
||||||
osStderr io.Writer = os.Stderr
|
osStderr io.Writer = os.Stderr
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type cmdlineOpts struct {
|
||||||
|
dataDir string
|
||||||
|
out io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
func cmdListImages(cmd *cobra.Command, args []string) error {
|
func cmdListImages(cmd *cobra.Command, args []string) error {
|
||||||
filter, err := cmd.Flags().GetStringArray("filter")
|
filter, err := cmd.Flags().GetStringArray("filter")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -23,8 +28,16 @@ func cmdListImages(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
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.`,
|
operating sytsems like centos and RHEL with easy customizations support.`,
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
}
|
}
|
||||||
|
rootCmd.PersistentFlags().String("datadir", "", `Override the default data direcotry for e.g. custom repositories/*.json data`)
|
||||||
rootCmd.SetOut(osStdout)
|
rootCmd.SetOut(osStdout)
|
||||||
rootCmd.SetErr(osStderr)
|
rootCmd.SetErr(osStderr)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,3 +96,15 @@ func TestBadCmdErrorsNoExtraCobraNoise(t *testing.T) {
|
||||||
// no extra output from cobra
|
// no extra output from cobra
|
||||||
assert.Equal(t, "", fakeStderr.String())
|
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]`)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,24 @@ import (
|
||||||
// that we keep this in sync
|
// that we keep this in sync
|
||||||
// XXX2: means we need to depend on osbuild-composer-common or a new rpm
|
// 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)
|
// 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",
|
"/etc/osbuild-composer",
|
||||||
"/usr/share/osbuild-composer",
|
"/usr/share/osbuild-composer",
|
||||||
}
|
}
|
||||||
|
|
||||||
var newRepoRegistry = func() (*reporegistry.RepoRegistry, error) {
|
var newRepoRegistry = func(dataDir string) (*reporegistry.RepoRegistry, error) {
|
||||||
// TODO: add a extraReposPaths here so that users can do
|
var dataDirs []string
|
||||||
// "ibuilder --repositories ..." to add a custom path(s)
|
if dataDir != "" {
|
||||||
|
dataDirs = []string{dataDir}
|
||||||
|
} else {
|
||||||
|
dataDirs = defaultDataDirs
|
||||||
|
}
|
||||||
|
|
||||||
return reporegistry.New(repositoryConfigs)
|
return reporegistry.New(dataDirs)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue