github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/internal/ui/select.go (about)

     1  //go:build linux || darwin || netbsd
     2  // +build linux darwin netbsd
     3  
     4  package ui
     5  
     6  import (
     7  	"os"
     8  	"runtime"
     9  
    10  	"golang.org/x/term"
    11  
    12  	"github.com/anchore/clio"
    13  	handler "github.com/anchore/syft/cmd/syft/cli/ui"
    14  )
    15  
    16  // Select is responsible for determining the specific UI function given select user option, the current platform
    17  // config values, and environment status (such as a TTY being present). The first UI in the returned slice of UIs
    18  // is intended to be used and the UIs that follow are meant to be attempted only in a fallback posture when there
    19  // are environmental problems (e.g. cannot write to the terminal). A writer is provided to capture the output of
    20  // the final SBOM report.
    21  func Select(verbose, quiet bool) (uis []clio.UI) {
    22  	isStdoutATty := term.IsTerminal(int(os.Stdout.Fd()))
    23  	isStderrATty := term.IsTerminal(int(os.Stderr.Fd()))
    24  	notATerminal := !isStderrATty && !isStdoutATty
    25  
    26  	switch {
    27  	case runtime.GOOS == "windows" || verbose || quiet || notATerminal || !isStderrATty:
    28  		uis = append(uis, None(quiet))
    29  	default:
    30  		// TODO: it may make sense in the future to pass handler options into select
    31  		h := handler.New(handler.DefaultHandlerConfig())
    32  		uis = append(uis, New(h, verbose, quiet))
    33  	}
    34  
    35  	return uis
    36  }