printer/yaml.go
changeset 0 5abace724584
child 83 57afac822019
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     1 // Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
       
     2 //
       
     3 // Licensed under the MIT license.
       
     4 // Please see the LICENSE file is this directory.
       
     5 
       
     6 package printer
       
     7 
       
     8 import (
       
     9 	"fmt"
       
    10 	"io"
       
    11 	"os"
       
    12 
       
    13 	"github.com/ghodss/yaml"
       
    14 )
       
    15 
       
    16 // YAMLPrinter represents a YAML printer
       
    17 type YAMLPrinter struct {
       
    18 }
       
    19 
       
    20 // NewPrinterYAML returns a YAML ResourcePrinter
       
    21 func NewPrinterYAML(option string) (*YAMLPrinter, error) {
       
    22 	return &YAMLPrinter{}, nil
       
    23 }
       
    24 
       
    25 // PrintObj sends the object as text to the writer
       
    26 // If the writer w is nil, standard output will be used.
       
    27 // For YAMLPrinter, the option parameter is currently not used.
       
    28 func (p *YAMLPrinter) PrintObj(obj interface{}, w io.Writer, option string) error {
       
    29 	if w == nil {
       
    30 		w = os.Stdout
       
    31 	}
       
    32 
       
    33 	//yamlEncoder := yaml.NewEncoder(w)
       
    34 	//return yamlEncoder.Encode(obj)
       
    35 
       
    36 	output, err := yaml.Marshal(obj)
       
    37 	if err != nil {
       
    38 		return err
       
    39 	}
       
    40 	_, err = fmt.Fprint(w, string(output))
       
    41 	return err
       
    42 }