From ab3972c696d7f8b8f7680b457ef6da20e2c691f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 25 Mar 2022 16:18:02 +0100 Subject: [PATCH] Manager: add platform for variables named "all" Variables defined for the "all" platform will be available on all platforms. Platform-specific values overrule the "all" platform values. --- internal/manager/config/enums.go | 3 ++- internal/manager/config/settings.go | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) 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) }