|
| 1 | +/* |
| 2 | +Copyright 2023 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 utils |
| 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 | +) |
| 29 | + |
| 30 | +// PrinterType is a type declaration for a printer type. |
| 31 | +type PrinterType string |
| 32 | + |
| 33 | +var ( |
| 34 | + // PrinterTypeTable is a table printer type. |
| 35 | + PrinterTypeTable = PrinterType("table") |
| 36 | + // PrinterTypeJSON is a json printer type. |
| 37 | + PrinterTypeJSON = PrinterType("json") |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + // ErrUnknowPrinterType is an error if a printer type isn't known. |
| 42 | + ErrUnknowPrinterType = errors.New("unknown printer type") |
| 43 | + // ErrTableRequired is an error if the object being printed |
| 44 | + // isn't a metav1.Table. |
| 45 | + ErrTableRequired = errors.New("metav1.Table is required") |
| 46 | +) |
| 47 | + |
| 48 | +// Printer is an interface for a printer. |
| 49 | +type Printer interface { |
| 50 | + // Print is a method to print an object |
| 51 | + Print(in interface{}) error |
| 52 | +} |
| 53 | + |
| 54 | +// NewPrinter creates a new printer. |
| 55 | +func NewPrinter(printerType string, writer io.Writer) (Printer, error) { |
| 56 | + switch printerType { |
| 57 | + case string(PrinterTypeTable): |
| 58 | + return &tablePrinter{writer: writer}, nil |
| 59 | + case string(PrinterTypeJSON): |
| 60 | + return &jsonPrinter{writer: writer}, nil |
| 61 | + default: |
| 62 | + return nil, ErrUnknowPrinterType |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +type tablePrinter struct { |
| 67 | + writer io.Writer |
| 68 | +} |
| 69 | + |
| 70 | +func (p *tablePrinter) Print(in interface{}) error { |
| 71 | + table, ok := in.(*metav1.Table) |
| 72 | + if !ok { |
| 73 | + return ErrTableRequired |
| 74 | + } |
| 75 | + |
| 76 | + options := printers.PrintOptions{} |
| 77 | + tablePrinter := printers.NewTablePrinter(options) |
| 78 | + scheme := runtime.NewScheme() |
| 79 | + printer, err := printers.NewTypeSetter(scheme).WrapToPrinter(tablePrinter, nil) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + return printer.PrintObj(table, p.writer) |
| 85 | +} |
| 86 | + |
| 87 | +type jsonPrinter struct { |
| 88 | + writer io.Writer |
| 89 | +} |
| 90 | + |
| 91 | +func (p *jsonPrinter) Print(in interface{}) error { |
| 92 | + data, err := json.MarshalIndent(in, "", " ") |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("marshalling object as json: %w", err) |
| 95 | + } |
| 96 | + _, err = p.writer.Write(data) |
| 97 | + return err |
| 98 | +} |
0 commit comments