debian-forge-cli/cmd/image-builder/export_test.go
Michael Vogt 4b8bff8404 cmd: rework argument handling
This commit tweaks the argument handling based on the suggestion
by Achilleas and Ondrej (thanks!). Now it takes
```console
$ image-builder manifest qcow2 ./path/to/blueprint
...
$ image-builder manifest --arch s390x --distro centos-9 qcow2
...
```
If no arch is specified the hostname arch is used. If no distro
is specified in either the blueprint or the commandline it is
auto-detected based on the host.

Note that if the distro from the comandline and the blueprint
diverge an error is raised. We can relax this rule and just
add precedence, e.g. commandline always overrides the blueprint
but ideally we would have a clear use-case here so we start
conservative and can always relax this rule later (the inverse
is much harder).

This means it is no longer copy/paste friendly from `list-images`
by default but instead we can provide a new
`list-images --output=shell` option that outputs in exactly the
format that `image-builder [manifest|build]` need.
2024-12-16 07:54:45 +00:00

60 lines
1.1 KiB
Go

package main
import (
"fmt"
"io"
"os"
"github.com/osbuild/images/pkg/reporegistry"
)
var (
GetOneImage = getOneImage
Run = run
FindDistro = findDistro
)
func MockOsArgs(new []string) (restore func()) {
saved := os.Args
os.Args = append([]string{"argv0"}, new...)
return func() {
os.Args = saved
}
}
func MockOsStdout(new io.Writer) (restore func()) {
saved := osStdout
osStdout = new
return func() {
osStdout = saved
}
}
func MockOsStderr(new io.Writer) (restore func()) {
saved := osStderr
osStderr = new
return func() {
osStderr = saved
}
}
func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore func()) {
saved := newRepoRegistry
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
}
}
func MockDistroGetHostDistroName(f func() (string, error)) (restore func()) {
saved := distroGetHostDistroName
distroGetHostDistroName = f
return func() {
distroGetHostDistroName = saved
}
}