1 /* 2 Copyright 2022 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 "fmt" 21 "io" 22 ) 23 24 const ( 25 yellowColor = "\u001b[33;1m" 26 resetColor = "\u001b[0m" 27 ) 28 29 type WarningPrinter struct { 30 // out is the writer to output warnings to 31 out io.Writer 32 // opts contains options controlling warning output 33 opts WarningPrinterOptions 34 } 35 36 // WarningPrinterOptions controls the behavior of a WarningPrinter constructed using NewWarningPrinter() 37 type WarningPrinterOptions struct { 38 // Color indicates that warning output can include ANSI color codes 39 Color bool 40 } 41 42 // NewWarningPrinter returns an implementation of warningPrinter that outputs warnings to the specified writer. 43 func NewWarningPrinter(out io.Writer, opts WarningPrinterOptions) *WarningPrinter { 44 h := &WarningPrinter{out: out, opts: opts} 45 return h 46 } 47 48 // Print prints warnings to the configured writer. 49 func (w *WarningPrinter) Print(message string) { 50 if w.opts.Color { 51 fmt.Fprintf(w.out, "%sWarning:%s %s\n", yellowColor, resetColor, message) 52 } else { 53 fmt.Fprintf(w.out, "Warning: %s\n", message) 54 } 55 } 56