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.
This commit is contained in:
Michael Vogt 2024-12-04 16:49:23 +01:00 committed by Simon de Vlieger
parent 0d06eedd26
commit 4b8bff8404
5 changed files with 150 additions and 19 deletions

View file

@ -0,0 +1,48 @@
package main_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/osbuild/image-builder-cli/cmd/image-builder"
)
func TestFindDistro(t *testing.T) {
for _, tc := range []struct {
argDistro string
bpDistro string
expectedDistro string
expectedErr string
}{
{"arg", "", "arg", ""},
{"", "bp", "bp", ""},
{"arg", "bp", "", `error selecting distro name, cmdline argument "arg" is different from blueprint "bp"`},
// the argDistro,bpDistro == "" case is tested below
} {
distro, err := main.FindDistro(tc.argDistro, tc.bpDistro)
if tc.expectedErr != "" {
assert.Equal(t, tc.expectedErr, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedDistro, distro)
}
}
}
func TestFindDistroAutoDetect(t *testing.T) {
var buf bytes.Buffer
restore := main.MockOsStderr(&buf)
defer restore()
restore = main.MockDistroGetHostDistroName(func() (string, error) {
return "mocked-host-distro", nil
})
defer restore()
distro, err := main.FindDistro("", "")
assert.NoError(t, err)
assert.Equal(t, "mocked-host-distro", distro)
assert.Equal(t, `No distro name specified, selecting "mocked-host-distro" based on host, use --distro to override`, buf.String())
}