From fad62c284f261aa1f916ff9a380cbed691f25c33 Mon Sep 17 00:00:00 2001 From: Rohith Date: Mon, 30 Oct 2017 16:30:51 +0000 Subject: [PATCH] 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 --- cmd/kops/toolbox_template.go | 49 ++++++++++++++++----------------- pkg/util/templater/templater.go | 8 ++---- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/cmd/kops/toolbox_template.go b/cmd/kops/toolbox_template.go index f3eb2b52a0..58de13f85c 100644 --- a/cmd/kops/toolbox_template.go +++ b/cmd/kops/toolbox_template.go @@ -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) - } - if _, err := b.WriteString(rendered); err != nil { - return fmt.Errorf("unable to write template: %s, error: %s", x, err) - } - - // @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) - } + documents = append(documents, string(formatted)) } } - iowriter := out + // join in harmony all the YAML documents back together + content := strings.Join(documents, "---\n") + 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) } diff --git a/pkg/util/templater/templater.go b/pkg/util/templater/templater.go index 6f131bc746..d10ef6b4d4 100644 --- a/pkg/util/templater/templater.go +++ b/pkg/util/templater/templater.go @@ -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) } }