Skip to content

Commit

Permalink
Fixes extra newline from confs
Browse files Browse the repository at this point in the history
As we are using templates to dynamicaly create confs on conditions,
Often extra newlines are getting added in confs, we should remove them.
  • Loading branch information
auniyal61 committed Sep 10, 2024
1 parent 174296c commit 7a129ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
47 changes: 47 additions & 0 deletions modules/common/util/template_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions modules/common/util/template_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestGetTemplateData(t *testing.T) {
AdditionalTemplate: map[string]string{},
},
want: map[string]string{
"bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\ncompute_driver = libvirt.LibvirtDriver\n\n[oslo_concurrency]\nlock_path = /var/lib/nova/tmp\n",
"config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n",
"foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n",
},
Expand Down Expand Up @@ -178,6 +179,7 @@ func TestGetTemplateData(t *testing.T) {
"config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n",
"foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n",
"common.sh": "#!/bin/bash\nset -e\n\nfunction common_func {\n echo some common func\n}\n",
"bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\ncompute_driver = libvirt.LibvirtDriver\n\n[oslo_concurrency]\nlock_path = /var/lib/nova/tmp\n",
},
error: false,
},
Expand Down
13 changes: 13 additions & 0 deletions modules/common/util/testdata/templates/testservice/config/bar.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[DEFAULT]
state_path = /var/lib/nova


debug=true

compute_driver = libvirt.LibvirtDriver


[oslo_concurrency]
lock_path = /var/lib/nova/tmp


0 comments on commit 7a129ce

Please sign in to comment.