diff --git a/internal/manager/config/enums.go b/internal/manager/config/enums.go index 434bd3ba..9ba00d67 100644 --- a/internal/manager/config/enums.go +++ b/internal/manager/config/enums.go @@ -13,7 +13,8 @@ type VariableAudience string const ( // the "platform" of task variables. It's a free-form string field, but it has - // some predefined values here. + // one semantic value ("all") and some predefined values here. + VariablePlatformAll VariablePlatform = "all" VariablePlatformLinux VariablePlatform = "linux" VariablePlatformWindows VariablePlatform = "windows" VariablePlatformDarwin VariablePlatform = "darwin" diff --git a/internal/manager/config/settings.go b/internal/manager/config/settings.go index 06a00305..8044fbdd 100644 --- a/internal/manager/config/settings.go +++ b/internal/manager/config/settings.go @@ -311,20 +311,29 @@ func (c *Conf) constructVariableLookupTable(logLevel zerolog.Level) { c.VariablesLookup = lookup } +func updateMap[K comparable, V any](target map[K]V, updateWith map[K]V) { + for key, value := range updateWith { + target[key] = value + } +} + // ExpandVariables converts "{variable name}" to the value that belongs to the given audience and platform. func (c *Conf) ExpandVariables(valueToExpand string, audience VariableAudience, platform VariablePlatform) string { - audienceMap := c.VariablesLookup[audience] - if audienceMap == nil { + platformsForAudience := c.VariablesLookup[audience] + if platformsForAudience == nil { log.Warn(). Str("valueToExpand", valueToExpand). Str("audience", string(audience)). - Str("platform", platform). + Str("platform", string(platform)). Msg("no variables defined for this audience") return valueToExpand } - platformMap := audienceMap[platform] - if platformMap == nil { + varsForPlatform := map[string]string{} + updateMap(varsForPlatform, platformsForAudience[platform]) + updateMap(varsForPlatform, platformsForAudience[VariablePlatformAll]) + + if varsForPlatform == nil { log.Warn(). Str("valueToExpand", valueToExpand). Str("audience", string(audience)). @@ -334,7 +343,7 @@ func (c *Conf) ExpandVariables(valueToExpand string, audience VariableAudience, } // Variable replacement - for varname, varvalue := range platformMap { + for varname, varvalue := range varsForPlatform { placeholder := fmt.Sprintf("{%s}", varname) valueToExpand = strings.Replace(valueToExpand, placeholder, varvalue, -1) }