in cmd/hub/lifecycle/template.go [431:552]
func processReplacement(content, filename, componentName string, componentDepends []string,
kv map[string]interface{}, replacement *regexp.Regexp, strip func(string) string) (string, []error) {
errs := make([]error, 0)
replaced := false
outContent := replacement.ReplaceAllStringFunc(content,
func(variable string) string {
variable = strip(variable)
variable, encodings := head(variable, "/", "|")
substitution, exist := parameters.FindValue(variable, componentName, componentDepends, kv)
if !exist {
errs = append(errs, fmt.Errorf("Template `%s` refer to unknown substitution `%s`", filename, variable))
return "(unknown)"
}
if parameters.RequireExpansion(substitution) {
util.WarnOnce("Template `%s` substitution `%s` refer to a value `%s` that is not expanded",
filename, variable, substitution)
}
if config.Trace {
log.Printf("--- %s | %s => %v", variable, componentName, substitution)
}
replaced = true
if len(encodings) > 0 {
if unknown := util.OmitAll(encodings, templateSubstitutionSupportedEncodings); len(unknown) > 0 {
errs = append(errs, fmt.Errorf("Unknown encoding(s) %v processing template `%s` substitution `%s`",
unknown, filename, variable))
}
for _, encoding := range encodings {
switch encoding {
case "base64":
substitution = base64.StdEncoding.EncodeToString([]byte(util.String(substitution)))
case "unbase64":
decoded, err := base64.StdEncoding.DecodeString(util.String(substitution))
if err != nil {
errs = append(errs, fmt.Errorf("Unable to decode base64 from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = string(decoded)
}
case "json":
jsonBytes, err := json.Marshal(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to marshal JSON from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = string(jsonBytes)
}
case "yaml":
// TODO YAML fragment on a single line
yamlBytes, err := yaml.Marshal(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to marshal YAML from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = string(yamlBytes)
}
case "first":
str := util.String(substitution)
if strings.Contains(str, " ") {
substitution = strings.Split(str, " ")[0]
}
case "parseURL":
str := util.String(substitution)
url, err := parseURL(str)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = url
}
case "isSecure":
url, err := toURL(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = isSecure(url)
}
case "insecure":
url, err := toURL(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = !isSecure(url)
}
case "hostname":
url, err := toURL(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = url.Hostname()
}
case "port":
url, err := toURL(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = url.Port()
}
case "scheme":
url, err := toURL(substitution)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse URL from %v while processing template `%s` substitution `%s`: %v",
substitution, filename, variable, err))
} else {
substitution = url.Scheme
}
}
}
}
return strings.TrimSpace(util.String(substitution))
})
if !replaced && len(errs) == 0 {
util.Warn("No substitutions found in template `%s`", filename)
}
return outContent, errs
}