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.
This commit is contained in:
Sybren A. Stüvel 2022-03-25 16:18:02 +01:00
parent 1a79c0958c
commit ab3972c696
2 changed files with 17 additions and 7 deletions

View File

@ -13,7 +13,8 @@ type VariableAudience string
const ( const (
// the "platform" of task variables. It's a free-form string field, but it has // 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" VariablePlatformLinux VariablePlatform = "linux"
VariablePlatformWindows VariablePlatform = "windows" VariablePlatformWindows VariablePlatform = "windows"
VariablePlatformDarwin VariablePlatform = "darwin" VariablePlatformDarwin VariablePlatform = "darwin"

View File

@ -311,20 +311,29 @@ func (c *Conf) constructVariableLookupTable(logLevel zerolog.Level) {
c.VariablesLookup = lookup 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. // 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 { func (c *Conf) ExpandVariables(valueToExpand string, audience VariableAudience, platform VariablePlatform) string {
audienceMap := c.VariablesLookup[audience] platformsForAudience := c.VariablesLookup[audience]
if audienceMap == nil { if platformsForAudience == nil {
log.Warn(). log.Warn().
Str("valueToExpand", valueToExpand). Str("valueToExpand", valueToExpand).
Str("audience", string(audience)). Str("audience", string(audience)).
Str("platform", platform). Str("platform", string(platform)).
Msg("no variables defined for this audience") Msg("no variables defined for this audience")
return valueToExpand return valueToExpand
} }
platformMap := audienceMap[platform] varsForPlatform := map[string]string{}
if platformMap == nil { updateMap(varsForPlatform, platformsForAudience[platform])
updateMap(varsForPlatform, platformsForAudience[VariablePlatformAll])
if varsForPlatform == nil {
log.Warn(). log.Warn().
Str("valueToExpand", valueToExpand). Str("valueToExpand", valueToExpand).
Str("audience", string(audience)). Str("audience", string(audience)).
@ -334,7 +343,7 @@ func (c *Conf) ExpandVariables(valueToExpand string, audience VariableAudience,
} }
// Variable replacement // Variable replacement
for varname, varvalue := range platformMap { for varname, varvalue := range varsForPlatform {
placeholder := fmt.Sprintf("{%s}", varname) placeholder := fmt.Sprintf("{%s}", varname)
valueToExpand = strings.Replace(valueToExpand, placeholder, varvalue, -1) valueToExpand = strings.Replace(valueToExpand, placeholder, varvalue, -1)
} }