diff --git a/modules/common/util/template_util.go b/modules/common/util/template_util.go index a641bb89..5448dd73 100644 --- a/modules/common/util/template_util.go +++ b/modules/common/util/template_util.go @@ -59,6 +59,47 @@ type Template struct { Version string // optional version string to separate templates inside the InstanceType/Type directory. E.g. placementapi/config/18.0 } +// This function removes extra space and new-lines from conf data +func cleanConfig(rawConf string) string { + lines := strings.Split(rawConf, "\n") + var result []string + var prevLineWasBlank bool + + for _, line := range lines { + trimmedLine := strings.TrimSpace(line) + + if trimmedLine == "" { + prevLineWasBlank = true + // if current line is blank, no need to add it in result + } else { + if strings.HasPrefix(trimmedLine, "[") && strings.HasSuffix(trimmedLine, "]") { + // section-header + if len(result) > 0 && !prevLineWasBlank { + result = append(result, "") + } + var sectionHeaderLine string + if len(result) > 0 { + // new section-hearder + sectionHeaderLine = "\n" + line + } else { + sectionHeaderLine = line + } + result = append(result, sectionHeaderLine) + } else { + // secion-body + result = append(result, line) + } + // reset flag + prevLineWasBlank = false + } + } + + // add an extra line at EOF + result = append(result, "") + + return strings.Join(result, "\n") +} + // GetTemplatesPath get path to templates, either running local or deployed as container func GetTemplatesPath() (string, error) { @@ -135,6 +176,12 @@ func ExecuteTemplate(templateFile string, data interface{}) (string, error) { if err != nil { return "", err } + + if strings.HasSuffix(templateFile, ".conf") { + // clean for only conf files + renderedTemplate = cleanConfig(renderedTemplate) + } + return renderedTemplate, nil }