github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/template.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "bytes" 21 "go/doc" 22 "io" 23 "strings" 24 "text/template" 25 ) 26 27 func wrap(indent string, s string) string { 28 var buf bytes.Buffer 29 doc.ToText(&buf, s, indent, indent+" ", 80-len(indent)) 30 return buf.String() 31 } 32 33 // ExecuteTemplate executes templateText with data and output written to w. 34 func ExecuteTemplate(w io.Writer, templateText string, data interface{}) error { 35 t := template.New("top") 36 t.Funcs(template.FuncMap{ 37 "trim": strings.TrimSpace, 38 "wrap": wrap, 39 }) 40 template.Must(t.Parse(templateText)) 41 return t.Execute(w, data) 42 } 43 44 func ExecuteTemplateToString(templateText string, data interface{}) (string, error) { 45 b := bytes.Buffer{} 46 err := ExecuteTemplate(&b, templateText, data) 47 return b.String(), err 48 }