github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/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  
    13  // Select is responsible for determining the specific UI function given select user option, the current platform
    14  // config values, and environment status (such as a TTY being present). The first UI in the returned slice of UIs
    15  // is intended to be used and the UIs that follow are meant to be attempted only in a fallback posture when there
    16  // are environmental problems (e.g. cannot write to the terminal). A writer is provided to capture the output of
    17  // the final SBOM report.
    18  func Select(verbose, quiet bool) (uis []UI) {
    19  	isStdoutATty := term.IsTerminal(int(os.Stdout.Fd()))
    20  	isStderrATty := term.IsTerminal(int(os.Stderr.Fd()))
    21  	notATerminal := !isStderrATty && !isStdoutATty
    22  
    23  	switch {
    24  	case runtime.GOOS == "windows" || verbose || quiet || notATerminal || !isStderrATty:
    25  		uis = append(uis, NewLoggerUI())
    26  	default:
    27  		uis = append(uis, NewEphemeralTerminalUI())
    28  	}
    29  
    30  	return uis
    31  }