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

@ -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"

@ -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)
}