Projects STRLCPY grype Commits e1bdbc7d
🤬
  • Fix reading syft json from stdin by redirect (#1299)

    I figured out that running `cat syft.json | grype` works but
    `grype < syft.json` does not work. This happens, because the
    IsPipedInput method only checks if stdin is a pipe which will be false
    if stdin is fed by a redirect.
    
    The go idiomatic way to fix this is by just checking if the file
    produced by stat has a size > 0.
    
    Implemented this check, that will recognize stdin by redirect, in the
    IsPipedInput() method. Renamed the method to IsStdinPipeOrRedirect().
    
    Signed-off-by: Felix Becker <[email protected]>
    Co-authored-by: Benjamin Neff <[email protected]>
  • Loading...
  • devfbe committed with GitHub 11 months ago
    e1bdbc7d
    1 parent d74e8538
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    cmd/root.go
    skipped 282 lines
    283 283  }
    284 284   
    285 285  func isVerbose() (result bool) {
    286  - isPipedInput, err := internal.IsPipedInput()
     286 + isStdinPipeOrRedirect, err := internal.IsStdinPipeOrRedirect()
    287 287   if err != nil {
    288 288   // since we can't tell if there was piped input we assume that there could be to disable the ETUI
    289 289   log.Warnf("unable to determine if there is piped input: %+v", err)
    290 290   return true
    291 291   }
    292 292   // verbosity should consider if there is piped input (in which case we should not show the ETUI)
    293  - return appConfig.CliOptions.Verbosity > 0 || isPipedInput
     293 + return appConfig.CliOptions.Verbosity > 0 || isStdinPipeOrRedirect
    294 294  }
    295 295   
    296 296  //nolint:funlen
    skipped 205 lines
    502 502  }
    503 503   
    504 504  func validateRootArgs(cmd *cobra.Command, args []string) error {
    505  - isPipedInput, err := internal.IsPipedInput()
     505 + isStdinPipeOrRedirect, err := internal.IsStdinPipeOrRedirect()
    506 506   if err != nil {
    507 507   log.Warnf("unable to determine if there is piped input: %+v", err)
    508  - isPipedInput = false
     508 + isStdinPipeOrRedirect = false
    509 509   }
    510 510   
    511  - if len(args) == 0 && !isPipedInput {
     511 + if len(args) == 0 && !isStdinPipeOrRedirect {
    512 512   // in the case that no arguments are given and there is no piped input we want to show the help text and return with a non-0 return code.
    513 513   if err := cmd.Help(); err != nil {
    514 514   return fmt.Errorf("unable to display help: %w", err)
    skipped 7 lines
  • ■ ■ ■ ■ ■ ■
    grype/pkg/syft_sbom_provider.go
    skipped 141 lines
    142 142  }
    143 143   
    144 144  func stdinReader() io.Reader {
    145  - isPipedInput, err := internal.IsPipedInput()
     145 + isStdinPipeOrRedirect, err := internal.IsStdinPipeOrRedirect()
    146 146   if err != nil {
    147 147   log.Warnf("unable to determine if there is piped input: %+v", err)
    148 148   return nil
    149 149   }
    150 150   
    151  - if !isPipedInput {
     151 + if !isStdinPipeOrRedirect {
    152 152   return nil
    153 153   }
    154 154   
    skipped 62 lines
  • ■ ■ ■ ■ ■ ■
    internal/input.go
    skipped 4 lines
    5 5   "os"
    6 6  )
    7 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) {
     8 +// IsStdinPipeOrRedirect returns true if stdin is provided via pipe or redirect
     9 +func IsStdinPipeOrRedirect() (bool, error) {
    10 10   fi, err := os.Stdin.Stat()
    11 11   if err != nil {
    12 12   return false, fmt.Errorf("unable to determine if there is piped input: %w", err)
    skipped 3 lines
    16 16   // on stdin, as running grype as a subprocess you would expect no character device to be present but input can
    17 17   // be from either stdin or indicated by the CLI. Checking if stdin is a pipe is the most direct way to determine
    18 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
     19 + return fi.Mode()&os.ModeNamedPipe != 0 || fi.Size() > 0, nil
    20 20  }
    21 21   
Please wait...
Page is in error, reload to recover