mirror of https://github.com/kubernetes/kops.git
Template Fixes
- fixing an issue in the way it handled inline yaml documents - cleaning it up somewhat, the prior version was a little cryptic to read; it's easier to just split into docs, format and rejoin at the end
This commit is contained in:
parent
e0d8eef16b
commit
fad62c284f
|
@ -17,12 +17,13 @@ limitations under the License.
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -139,16 +140,14 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
|
|||
if err != nil {
|
||||
return fmt.Errorf("unable to read snippet: %s, error: %s", j, err)
|
||||
}
|
||||
snippets[j] = string(content)
|
||||
snippets[path.Base(j)] = string(content)
|
||||
}
|
||||
}
|
||||
|
||||
b := new(bytes.Buffer)
|
||||
|
||||
// @step: render each of the template and write to location
|
||||
// @step: render each of the templates, splitting on the documents
|
||||
r := templater.NewTemplater()
|
||||
size := len(templates) - 1
|
||||
for i, x := range templates {
|
||||
var documents []string
|
||||
for _, x := range templates {
|
||||
content, err := ioutil.ReadFile(x)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read template: %s, error: %s", x, err)
|
||||
|
@ -158,37 +157,35 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
|
|||
if err != nil {
|
||||
return fmt.Errorf("unable to render template: %s, error: %s", x, err)
|
||||
}
|
||||
// @check if we have a zero length string and if so, forgo it
|
||||
// @check if the content is zero ignore it
|
||||
if len(rendered) <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// @check if we need to format the yaml
|
||||
if options.formatYAML {
|
||||
var data interface{}
|
||||
if err := utils.YamlUnmarshal([]byte(rendered), &data); err != nil {
|
||||
if !options.formatYAML {
|
||||
documents = append(documents, strings.Split(rendered, "---\n")...)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, x := range strings.Split(rendered, "---\n") {
|
||||
var data map[string]interface{}
|
||||
if err := yaml.Unmarshal([]byte(x), &data); err != nil {
|
||||
return fmt.Errorf("unable to unmarshall content from template: %s, error: %s", x, err)
|
||||
}
|
||||
// @TODO: i'm not sure how this could happen but best to heck none the less
|
||||
if len(data) <= 0 {
|
||||
continue
|
||||
}
|
||||
formatted, err := yaml.Marshal(&data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to marhshal formated content to yaml: %s", err)
|
||||
}
|
||||
rendered = string(formatted)
|
||||
documents = append(documents, string(formatted))
|
||||
}
|
||||
if _, err := b.WriteString(rendered); err != nil {
|
||||
return fmt.Errorf("unable to write template: %s, error: %s", x, err)
|
||||
}
|
||||
// join in harmony all the YAML documents back together
|
||||
content := strings.Join(documents, "---\n")
|
||||
|
||||
// @check if we should need to add document separator
|
||||
if i < size {
|
||||
if _, err := b.WriteString("---\n"); err != nil {
|
||||
return fmt.Errorf("unable to write to template: %s, error: %s", x, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
iowriter := out
|
||||
|
||||
// @check if we are writing to a file rather than stdout
|
||||
if options.outputPath != "" {
|
||||
w, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0660)
|
||||
|
@ -199,7 +196,7 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
|
|||
iowriter = w
|
||||
}
|
||||
|
||||
if _, err := iowriter.Write(b.Bytes()); err != nil {
|
||||
if _, err := iowriter.Write([]byte(content)); err != nil {
|
||||
return fmt.Errorf("unable to write template: %s", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ package templater
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
|
@ -51,11 +50,10 @@ func (r *Templater) Render(content string, context map[string]interface{}, snipp
|
|||
|
||||
// @step: add the snippits into the mix
|
||||
for filename, snippet := range snippets {
|
||||
name := filepath.Base(filename)
|
||||
if name == templateName {
|
||||
return "", fmt.Errorf("snippet cannot have the same name as the template: %s", name)
|
||||
if filename == templateName {
|
||||
return "", fmt.Errorf("snippet cannot have the same name as the template: %s", filename)
|
||||
}
|
||||
if _, err = tm.New(name).Parse(snippet); err != nil {
|
||||
if _, err = tm.New(filename).Parse(snippet); err != nil {
|
||||
return rendered, fmt.Errorf("unable to parse snippet: %s, error: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue