From 18f1e2b7f2430c82435926f4e8f38123010589fb Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 16 Feb 2022 16:31:05 +0100 Subject: [PATCH] clarify SelectedPackageTypes meaning and improve its usage (#30142) - document SelectedPackageTypes meaning - simplify isPackageTypeSelected --- dev-tools/mage/crossbuild.go | 5 +++-- dev-tools/mage/pkg.go | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/dev-tools/mage/crossbuild.go b/dev-tools/mage/crossbuild.go index 7a08a035bc4..a6bdd932447 100644 --- a/dev-tools/mage/crossbuild.go +++ b/dev-tools/mage/crossbuild.go @@ -43,7 +43,8 @@ const defaultCrossBuildTarget = "golangCrossBuild" // See NewPlatformList for details about platform filtering expressions. var Platforms = BuildPlatforms.Defaults() -// SelectedPackageTypes is the list of package types +// SelectedPackageTypes is the list of package types. If empty, all packages types +// are considered to be selected (see isPackageTypeSelected). var SelectedPackageTypes []PackageType func init() { @@ -65,7 +66,7 @@ func init() { } } -// CrossBuildOption defines a option to the CrossBuild target. +// CrossBuildOption defines an option to the CrossBuild target. type CrossBuildOption func(params *crossBuildParams) // ImageSelectorFunc returns the name of the builder image. diff --git a/dev-tools/mage/pkg.go b/dev-tools/mage/pkg.go index 77f964a39b9..379042ae104 100644 --- a/dev-tools/mage/pkg.go +++ b/dev-tools/mage/pkg.go @@ -112,17 +112,19 @@ func Package() error { return nil } +// isPackageTypeSelected returns true if SelectedPackageTypes is empty or if +// pkgType is present on SelectedPackageTypes. It returns false otherwise. func isPackageTypeSelected(pkgType PackageType) bool { - if SelectedPackageTypes != nil { - selected := false - for _, t := range SelectedPackageTypes { - if t == pkgType { - selected = true - } + if len(SelectedPackageTypes) == 0 { + return true + } + + for _, t := range SelectedPackageTypes { + if t == pkgType { + return true } - return selected } - return true + return false } type packageBuilder struct {