Skip to content

Commit 8e13ecf

Browse files
committed
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.
1 parent 670a623 commit 8e13ecf

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

bib/cmd/bootc-image-builder/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,12 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
461461
osbuildEnv = append(osbuildEnv, envVars...)
462462
}
463463

464-
if err = progress.RunOSBuild(pbar, mf, osbuildStore, outputDir, exports, osbuildEnv); err != nil {
464+
osbuildOpts := progress.OSBuildOptions{
465+
StoreDir: osbuildStore,
466+
OutputDir: outputDir,
467+
ExtraEnv: osbuildEnv,
468+
}
469+
if err = progress.RunOSBuild(pbar, mf, exports, &osbuildOpts); err != nil {
465470
return fmt.Errorf("cannot run osbuild: %w", err)
466471
}
467472

bib/pkg/progress/progress.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,18 @@ func (b *debugProgressBar) SetProgress(subLevel int, msg string, done int, total
317317
return nil
318318
}
319319

320+
type OSBuildOptions struct {
321+
StoreDir string
322+
OutputDir string
323+
ExtraEnv []string
324+
}
325+
320326
// XXX: merge variant back into images/pkg/osbuild/osbuild-exec.go
321-
func RunOSBuild(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error {
327+
func RunOSBuild(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) error {
328+
if opts == nil {
329+
opts = &OSBuildOptions{}
330+
}
331+
322332
// To keep maximum compatibility keep the old behavior to run osbuild
323333
// directly and show all messages unless we have a "real" progress bar.
324334
//
@@ -328,20 +338,20 @@ func RunOSBuild(pb ProgressBar, manifest []byte, store, outputDirectory string,
328338
// just run with the new runOSBuildWithProgress() helper.
329339
switch pb.(type) {
330340
case *terminalProgressBar, *debugProgressBar:
331-
return runOSBuildWithProgress(pb, manifest, store, outputDirectory, exports, extraEnv)
341+
return runOSBuildWithProgress(pb, manifest, exports, opts)
332342
default:
333-
return runOSBuildNoProgress(pb, manifest, store, outputDirectory, exports, extraEnv)
343+
return runOSBuildNoProgress(pb, manifest, exports, opts)
334344
}
335345
}
336346

337-
func runOSBuildNoProgress(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error {
338-
_, err := osbuild.RunOSBuild(manifest, store, outputDirectory, exports, nil, extraEnv, false, os.Stderr)
347+
func runOSBuildNoProgress(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) error {
348+
_, err := osbuild.RunOSBuild(manifest, opts.StoreDir, opts.OutputDir, exports, nil, opts.ExtraEnv, false, os.Stderr)
339349
return err
340350
}
341351

342352
var osbuildCmd = "osbuild"
343353

344-
func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) (err error) {
354+
func runOSBuildWithProgress(pb ProgressBar, manifest []byte, exports []string, opts *OSBuildOptions) (err error) {
345355
rp, wp, err := os.Pipe()
346356
if err != nil {
347357
return fmt.Errorf("cannot create pipe for osbuild: %w", err)
@@ -351,8 +361,8 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
351361

352362
cmd := exec.Command(
353363
osbuildCmd,
354-
"--store", store,
355-
"--output-directory", outputDirectory,
364+
"--store", opts.StoreDir,
365+
"--output-directory", opts.OutputDir,
356366
"--monitor=JSONSeqMonitor",
357367
"--monitor-fd=3",
358368
"-",
@@ -362,7 +372,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
362372
}
363373

364374
var stdio bytes.Buffer
365-
cmd.Env = append(os.Environ(), extraEnv...)
375+
cmd.Env = append(os.Environ(), opts.ExtraEnv...)
366376
cmd.Stdin = bytes.NewBuffer(manifest)
367377
cmd.Stdout = &stdio
368378
cmd.Stderr = &stdio

bib/pkg/progress/progress_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ exit 112
157157

158158
pbar, err := progress.New("debug")
159159
assert.NoError(t, err)
160-
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil)
160+
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), nil, nil)
161161
assert.EqualError(t, err, `error running osbuild: exit status 112
162162
BuildLog:
163163
osbuild-stage-message
@@ -184,7 +184,7 @@ done
184184

185185
pbar, err := progress.New("debug")
186186
assert.NoError(t, err)
187-
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil)
187+
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), nil, nil)
188188
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`)
189189

190190
// ensure the SIGINT got delivered

0 commit comments

Comments
 (0)