github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/helpers/warning.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package helpers
     5  
     6  import (
     7  	"github.com/mattn/go-isatty"
     8  	"io"
     9  	"k8s.io/client-go/rest"
    10  	"os"
    11  )
    12  
    13  // getWarningHandler returns an implementation of WarningHandler that outputs code 299 warnings to the specified writer.
    14  func getWarningHandler(w io.Writer) rest.WarningHandler {
    15  	// deduplicate and attempt color warnings when running from a terminal
    16  	return rest.NewWarningWriter(w, rest.WarningWriterOptions{
    17  		Deduplicate: true,
    18  		Color:       isTerminal(w),
    19  	})
    20  }
    21  
    22  func isTerminal(w io.Writer) bool {
    23  	if w, ok := w.(*os.File); ok && isatty.IsTerminal(w.Fd()) {
    24  		return true
    25  	}
    26  	return false
    27  }