Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim spaces from beginning and end of templates #141

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions cmd/kubebuilder/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package util

import (
"bytes"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -50,10 +51,6 @@ func WriteIfNotFound(path, templateName, templateValue string, data interface{})
}

func Write(path, templateName, templateValue string, data interface{}) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
create(path)
}

t := template.Must(template.New(templateName).Funcs(
template.FuncMap{
"title": strings.Title,
Expand All @@ -62,16 +59,14 @@ func Write(path, templateName, templateValue string, data interface{}) bool {
},
).Parse(templateValue))

f, err := os.OpenFile(path, os.O_WRONLY, 0)
var tmp bytes.Buffer
err := t.Execute(&tmp, data)
if err != nil {
log.Fatalf("Failed to create %s: %v", path, err)
log.Fatalf("Failed to render template %s: %v", templateName, err)
}
defer f.Close()

err = t.Execute(f, data)
if err != nil {
log.Fatalf("Failed to create %s: %v", path, err)
}
trimmed := strings.TrimSpace(tmp.String()) + "\n"
WriteString(path, trimmed)

return true
}
Expand Down