sigs.k8s.io/cluster-api-provider-aws@v1.5.5/cmd/clusterawsadm/printers/printers.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 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 printers 18 19 import ( 20 "encoding/json" 21 "errors" 22 "fmt" 23 "io" 24 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/cli-runtime/pkg/printers" 28 "sigs.k8s.io/yaml" 29 ) 30 31 // PrinterType is a type declaration for a printer type. 32 type PrinterType string 33 34 var ( 35 // PrinterTypeTable is a table printer type. 36 PrinterTypeTable = PrinterType("table") 37 // PrinterTypeYAML is a yaml printer type. 38 PrinterTypeYAML = PrinterType("yaml") 39 // PrinterTypeJSON is a json printer type. 40 PrinterTypeJSON = PrinterType("json") 41 ) 42 43 var ( 44 // ErrUnknowPrinterType is an error if a printer type isn't known. 45 ErrUnknowPrinterType = errors.New("unknown printer type") 46 // ErrTableRequired is an error if the object being printed 47 // isn't a metav1.Table. 48 ErrTableRequired = errors.New("metav1.Table is required") 49 ) 50 51 // Printer is an interface for a printer. 52 type Printer interface { 53 // Print is a method to print an object 54 Print(in interface{}) error 55 } 56 57 // New creates a new printer. 58 func New(printerType string, writer io.Writer) (Printer, error) { 59 switch printerType { 60 case string(PrinterTypeTable): 61 return &tablePrinter{writer: writer}, nil 62 case string(PrinterTypeJSON): 63 return &jsonPrinter{writer: writer}, nil 64 case string(PrinterTypeYAML): 65 return &yamlPrinter{writer: writer}, nil 66 default: 67 return nil, ErrUnknowPrinterType 68 } 69 } 70 71 type tablePrinter struct { 72 writer io.Writer 73 } 74 75 func (p *tablePrinter) Print(in interface{}) error { 76 table, ok := in.(*metav1.Table) 77 if !ok { 78 return ErrTableRequired 79 } 80 81 options := printers.PrintOptions{} 82 tablePrinter := printers.NewTablePrinter(options) 83 scheme := runtime.NewScheme() 84 printer, err := printers.NewTypeSetter(scheme).WrapToPrinter(tablePrinter, nil) 85 if err != nil { 86 return err 87 } 88 89 return printer.PrintObj(table, p.writer) 90 } 91 92 type yamlPrinter struct { 93 writer io.Writer 94 } 95 96 func (p *yamlPrinter) Print(in interface{}) error { 97 data, err := yaml.Marshal(in) 98 if err != nil { 99 return fmt.Errorf("marshalling object as yaml: %w", err) 100 } 101 _, err = p.writer.Write(data) 102 return err 103 } 104 105 type jsonPrinter struct { 106 writer io.Writer 107 } 108 109 func (p *jsonPrinter) Print(in interface{}) error { 110 data, err := json.MarshalIndent(in, "", " ") 111 if err != nil { 112 return fmt.Errorf("marshalling object as json: %w", err) 113 } 114 _, err = p.writer.Write(data) 115 return err 116 }