github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/internal/input.go (about)

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  // IsPipedInput returns true if there is no input device, which means the user **may** be providing input via a pipe.
     9  func IsPipedInput() (bool, error) {
    10  	fi, err := os.Stdin.Stat()
    11  	if err != nil {
    12  		return false, fmt.Errorf("unable to determine if there is piped input: %w", err)
    13  	}
    14  
    15  	// note: we should NOT use the absence of a character device here as the hint that there may be input expected
    16  	// on stdin, as running syft as a subprocess you would expect no character device to be present but input can
    17  	// be from either stdin or indicated by the CLI. Checking if stdin is a pipe is the most direct way to determine
    18  	// if there *may* be bytes that will show up on stdin that should be used for the analysis source.
    19  	return fi.Mode()&os.ModeNamedPipe != 0, nil
    20  }
    21  
    22  // IsTerminal returns true if there is a terminal present.
    23  func IsTerminal() bool {
    24  	stat, _ := os.Stdin.Stat()
    25  	return (stat.Mode() & os.ModeCharDevice) != 0
    26  }