diff --git a/README.md b/README.md index 02731fd..2ae6c7a 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Run these commands to see some sample behavior: composer audit composer audit --format=simple composer audit --format=json + composer audit --format=json --output-file=report.json composer validate composer require symfony/symfony --update-with-all-dependencies composer audit diff --git a/src/Command/AuditCommand.php b/src/Command/AuditCommand.php index 3f5abe9..43466ea 100644 --- a/src/Command/AuditCommand.php +++ b/src/Command/AuditCommand.php @@ -11,6 +11,7 @@ use FancyGuy\Composer\SecurityCheck\Formatter\JsonFormatter; use FancyGuy\Composer\SecurityCheck\Formatter\SimpleFormatter; use FancyGuy\Composer\SecurityCheck\Formatter\TextFormatter; +use FancyGuy\Composer\SecurityCheck\Output\FileOutput; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -26,6 +27,7 @@ protected function configure() ->setDefinition(array( new InputOption('audit-db', '', InputOption::VALUE_REQUIRED, 'Path to the advisory database'), new InputOption('format', '', InputOption::VALUE_REQUIRED, 'Output format', 'text'), + new InputOption('output-file', '', InputOption::VALUE_REQUIRED, 'File to append the report output to'), new InputOption('endpoint', '', InputOption::VALUE_REQUIRED, 'Security checker server URL', HttpCheckerInterface::DEFAULT_ENDPOINT), new InputOption('timeout', '', InputOption::VALUE_REQUIRED, 'HTTP timeout in seconds', HttpCheckerInterface::DEFAULT_TIMEOUT), new InputOption('file', '', InputOption::VALUE_REQUIRED, 'Path to composer.lock file', './composer.lock'), @@ -87,7 +89,17 @@ protected function execute(InputInterface $input, OutputInterface $output) return 127; } - $formatter->displayResults($output, $composerFile, $vulnerabilities); + if ($outputFile = $input->getOption('output-file')) { + $formatter->displayResults( + new FileOutput($outputFile, $output->getVerbosity(), $output->isDecorated(), $output->getFormatter()), + $composerFile, + $vulnerabilities + ); + $output->writeln(sprintf('Report written to: %s', $outputFile)); + } else { + $formatter->displayResults($output, $composerFile, $vulnerabilities); + } + if ($checker->getLastVulnerabilityCount() > 0) { return 1; diff --git a/src/Output/FileOutput.php b/src/Output/FileOutput.php new file mode 100644 index 0000000..c56327a --- /dev/null +++ b/src/Output/FileOutput.php @@ -0,0 +1,36 @@ +getStream())) { + throw new RuntimeException('Unable to close write stream handle'); + } + } +} \ No newline at end of file