progress: add progress.OSBuildOptions struct

This commit adds a `OSBuildOptions` struct that can be used to
pass (optional) options to the `progress.RunOSBuild()` helper.

This make it easier to expand with more options.
This commit is contained in:
Michael Vogt 2025-01-27 11:52:08 +01:00 committed by Simon de Vlieger
parent e51a56b084
commit a875a16309
2 changed files with 21 additions and 11 deletions

View file

@ -317,8 +317,18 @@ func (b *debugProgressBar) SetProgress(subLevel int, msg string, done int, total
return nil return nil
} }
type OSBuildOptions struct {
StoreDir string
OutputDir string
ExtraEnv []string
}
// XXX: merge variant back into images/pkg/osbuild/osbuild-exec.go // XXX: merge variant back into images/pkg/osbuild/osbuild-exec.go
func RunOSBuild(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error { func RunOSBuild(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) error {
if opts == nil {
opts = &OSBuildOptions{}
}
// To keep maximum compatibility keep the old behavior to run osbuild // To keep maximum compatibility keep the old behavior to run osbuild
// directly and show all messages unless we have a "real" progress bar. // directly and show all messages unless we have a "real" progress bar.
// //
@ -328,20 +338,20 @@ func RunOSBuild(pb ProgressBar, manifest []byte, store, outputDirectory string,
// just run with the new runOSBuildWithProgress() helper. // just run with the new runOSBuildWithProgress() helper.
switch pb.(type) { switch pb.(type) {
case *terminalProgressBar, *debugProgressBar: case *terminalProgressBar, *debugProgressBar:
return runOSBuildWithProgress(pb, manifest, store, outputDirectory, exports, extraEnv) return runOSBuildWithProgress(pb, manifest, exports, opts)
default: default:
return runOSBuildNoProgress(pb, manifest, store, outputDirectory, exports, extraEnv) return runOSBuildNoProgress(pb, manifest, exports, opts)
} }
} }
func runOSBuildNoProgress(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error { func runOSBuildNoProgress(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) error {
_, err := osbuild.RunOSBuild(manifest, store, outputDirectory, exports, nil, extraEnv, false, os.Stderr) _, err := osbuild.RunOSBuild(manifest, opts.StoreDir, opts.OutputDir, exports, nil, opts.ExtraEnv, false, os.Stderr)
return err return err
} }
var osbuildCmd = "osbuild" var osbuildCmd = "osbuild"
func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) (err error) { func runOSBuildWithProgress(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) (err error) {
rp, wp, err := os.Pipe() rp, wp, err := os.Pipe()
if err != nil { if err != nil {
return fmt.Errorf("cannot create pipe for osbuild: %w", err) return fmt.Errorf("cannot create pipe for osbuild: %w", err)
@ -351,8 +361,8 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
cmd := exec.Command( cmd := exec.Command(
osbuildCmd, osbuildCmd,
"--store", store, "--store", opts.StoreDir,
"--output-directory", outputDirectory, "--output-directory", opts.OutputDir,
"--monitor=JSONSeqMonitor", "--monitor=JSONSeqMonitor",
"--monitor-fd=3", "--monitor-fd=3",
"-", "-",
@ -362,7 +372,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
} }
var stdio bytes.Buffer var stdio bytes.Buffer
cmd.Env = append(os.Environ(), extraEnv...) cmd.Env = append(os.Environ(), opts.ExtraEnv...)
cmd.Stdin = bytes.NewBuffer(manifest) cmd.Stdin = bytes.NewBuffer(manifest)
cmd.Stdout = &stdio cmd.Stdout = &stdio
cmd.Stderr = &stdio cmd.Stderr = &stdio

View file

@ -157,7 +157,7 @@ exit 112
pbar, err := progress.New("debug") pbar, err := progress.New("debug")
assert.NoError(t, err) assert.NoError(t, err)
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil) err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), nil, nil)
assert.EqualError(t, err, `error running osbuild: exit status 112 assert.EqualError(t, err, `error running osbuild: exit status 112
BuildLog: BuildLog:
osbuild-stage-message osbuild-stage-message
@ -184,7 +184,7 @@ done
pbar, err := progress.New("debug") pbar, err := progress.New("debug")
assert.NoError(t, err) assert.NoError(t, err)
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil) err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), nil, nil)
assert.EqualError(t, err, `error parsing osbuild status, please report a bug and try with "--progress=verbose": cannot scan line "invalid-json": invalid character 'i' looking for beginning of value`) assert.EqualError(t, err, `error parsing osbuild status, please report a bug and try with "--progress=verbose": cannot scan line "invalid-json": invalid character 'i' looking for beginning of value`)
// ensure the SIGINT got delivered // ensure the SIGINT got delivered