Skip to content

Commit

Permalink
Trim spaces from beginning and end of templates
Browse files Browse the repository at this point in the history
This prevents some slightly strange looking packages being generated
with init.
  • Loading branch information
kragniz committed May 10, 2018
1 parent ddb0309 commit d3c4393
Showing 1 changed file with 6 additions and 11 deletions.
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())
WriteString(path, trimmed)

return true
}
Expand Down

0 comments on commit d3c4393

Please sign in to comment.