From 601564862a81ea5b1de0ab287c56a2735f8788f1 Mon Sep 17 00:00:00 2001 From: Florent Revest Date: Tue, 15 Oct 2024 09:32:22 +0200 Subject: pkg/build: share parameters sanitization between builds and cleans When refactoring the Clean() function to share more code with Image(), there is logic I forgot to replicate, like setting a default number of CPUs to build/clean with. I imagine that not setting the default tracer could end up being a subtle issue in the future too. To avoid missing these cases in the future, refactor the parameters sanitization into a helper function called by both. --- pkg/build/build.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'pkg') diff --git a/pkg/build/build.go b/pkg/build/build.go index b84adb20b..c9a5b038f 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -49,6 +49,15 @@ type ImageDetails struct { CompilerID string } +func sanitize(params *Params) { + if params.Tracer == nil { + params.Tracer = &debugtracer.NullTracer{} + } + if params.BuildCPUs == 0 { + params.BuildCPUs = runtime.NumCPU() + } +} + // Image creates a disk image for the specified OS/ARCH/VM. // Kernel is taken from KernelDir, userspace system is taken from UserspaceDir. // If CmdlineFile is not empty, contents of the file are appended to the kernel command line. @@ -71,12 +80,7 @@ type ImageDetails struct { // the version of the compiler/toolchain that was used to build the kernel. // The CompilerID field is not guaranteed to be non-empty. func Image(params Params) (details ImageDetails, err error) { - if params.Tracer == nil { - params.Tracer = &debugtracer.NullTracer{} - } - if params.BuildCPUs == 0 { - params.BuildCPUs = runtime.NumCPU() - } + sanitize(¶ms) var builder builder builder, err = getBuilder(params.TargetOS, params.TargetArch, params.VMType) if err != nil { @@ -114,6 +118,7 @@ func Image(params Params) (details ImageDetails, err error) { } func Clean(params Params) error { + sanitize(¶ms) builder, err := getBuilder(params.TargetOS, params.TargetArch, params.VMType) if err != nil { return err -- cgit mrf-deployment