From a875a1630902d3d94d7e979ffaee52838aae47a3 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 27 Jan 2025 11:52:08 +0100 Subject: [PATCH] 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. --- bib/pkg/progress/progress.go | 28 +++++++++++++++++++--------- bib/pkg/progress/progress_test.go | 4 ++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/bib/pkg/progress/progress.go b/bib/pkg/progress/progress.go index 2326153..691faf2 100644 --- a/bib/pkg/progress/progress.go +++ b/bib/pkg/progress/progress.go @@ -317,8 +317,18 @@ func (b *debugProgressBar) SetProgress(subLevel int, msg string, done int, total return nil } +type OSBuildOptions struct { + StoreDir string + OutputDir string + ExtraEnv []string +} + // 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 // 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. switch pb.(type) { case *terminalProgressBar, *debugProgressBar: - return runOSBuildWithProgress(pb, manifest, store, outputDirectory, exports, extraEnv) + return runOSBuildWithProgress(pb, manifest, exports, opts) 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 { - _, err := osbuild.RunOSBuild(manifest, store, outputDirectory, exports, nil, extraEnv, false, os.Stderr) +func runOSBuildNoProgress(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) error { + _, err := osbuild.RunOSBuild(manifest, opts.StoreDir, opts.OutputDir, exports, nil, opts.ExtraEnv, false, os.Stderr) return err } 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() if err != nil { 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( osbuildCmd, - "--store", store, - "--output-directory", outputDirectory, + "--store", opts.StoreDir, + "--output-directory", opts.OutputDir, "--monitor=JSONSeqMonitor", "--monitor-fd=3", "-", @@ -362,7 +372,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect } var stdio bytes.Buffer - cmd.Env = append(os.Environ(), extraEnv...) + cmd.Env = append(os.Environ(), opts.ExtraEnv...) cmd.Stdin = bytes.NewBuffer(manifest) cmd.Stdout = &stdio cmd.Stderr = &stdio diff --git a/bib/pkg/progress/progress_test.go b/bib/pkg/progress/progress_test.go index 1daa21c..3e935af 100644 --- a/bib/pkg/progress/progress_test.go +++ b/bib/pkg/progress/progress_test.go @@ -157,7 +157,7 @@ exit 112 pbar, err := progress.New("debug") 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 BuildLog: osbuild-stage-message @@ -184,7 +184,7 @@ done pbar, err := progress.New("debug") 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`) // ensure the SIGINT got delivered