From 6e8012bac9350c990dd5619d2a6b97446f203346 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 28 Jun 2019 09:36:08 -0700 Subject: [PATCH] Allow YAML printer to separate multiple printed objects --- staging/src/k8s.io/cli-runtime/pkg/printers/json.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/json.go b/staging/src/k8s.io/cli-runtime/pkg/printers/json.go index bb5bec7488c..fef60eb944c 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/json.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/json.go @@ -68,7 +68,10 @@ func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error { // YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML. // The input object is assumed to be in the internal version of an API and is converted // to the given version first. -type YAMLPrinter struct{} +// If PrintObj() is called multiple times, objects are separated with a '---' separator. +type YAMLPrinter struct { + printCount int64 +} // PrintObj prints the data as YAML. func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { @@ -79,6 +82,13 @@ func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return fmt.Errorf(InternalObjectPrinterErr) } + count := atomic.AddInt64(&p.printCount, 1) + if count > 1 { + if _, err := w.Write([]byte("---\n")); err != nil { + return err + } + } + switch obj := obj.(type) { case *runtime.Unknown: data, err := yaml.JSONToYAML(obj.Raw)