From 8bffc9ea0efe2237ae08be6ecaa54c3bed6f7fa0 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 11 Oct 2018 12:35:11 -0700 Subject: [PATCH] In "bin/bulk export", require "--output " by default Summary: Depends on D19743. Ref T13210. Since this command can easily dump a bunch of binary data (or just a huge long blob of nonsense) to stdout, default to requiring "--output ". Using `--output -` will print to stdout. Test Plan: Ran with: no `--output`, `--output file`, `--output -`, `--output - --overwrite`. Got sensible results or errors in all cases. Reviewers: amckinley Reviewed By: amckinley Maniphest Tasks: T13210 Differential Revision: https://secure.phabricator.com/D19744 --- ...habricatorBulkManagementExportWorkflow.php | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/applications/transactions/bulk/management/PhabricatorBulkManagementExportWorkflow.php b/src/applications/transactions/bulk/management/PhabricatorBulkManagementExportWorkflow.php index 53d8441f91..7f10a28d4b 100644 --- a/src/applications/transactions/bulk/management/PhabricatorBulkManagementExportWorkflow.php +++ b/src/applications/transactions/bulk/management/PhabricatorBulkManagementExportWorkflow.php @@ -77,14 +77,27 @@ final class PhabricatorBulkManagementExportWorkflow $is_overwrite = $args->getArg('overwrite'); $output_path = $args->getArg('output'); - if (!strlen($output_path) && $is_overwrite) { + if (!strlen($output_path)) { throw new PhutilArgumentUsageException( pht( - 'Flag "--overwrite" has no effect without "--output".')); + 'Use "--output " to specify an output file, or "--output -" '. + 'to print to stdout.')); + } + + if ($output_path === '-') { + $is_stdout = true; + } else { + $is_stdout = false; + } + + if ($is_stdout && $is_overwrite) { + throw new PhutilArgumentUsageException( + pht( + 'Flag "--overwrite" has no effect when outputting to stdout.')); } if (!$is_overwrite) { - if (Filesystem::pathExists($output_path)) { + if (!$is_stdout && Filesystem::pathExists($output_path)) { throw new PhutilArgumentUsageException( pht( 'Output path already exists. Use "--overwrite" to overwrite '. @@ -113,7 +126,7 @@ final class PhabricatorBulkManagementExportWorkflow $iterator = $file->getFileDataIterator(); - if (strlen($output_path)) { + if (!$is_stdout) { // Empty the file before we start writing to it. Otherwise, "--overwrite" // will really mean "--append". Filesystem::writeFile($output_path, ''); @@ -121,6 +134,12 @@ final class PhabricatorBulkManagementExportWorkflow foreach ($iterator as $chunk) { Filesystem::appendFile($output_path, $chunk); } + + echo tsprintf( + "%s\n", + pht( + 'Exported data to "%s".', + Filesystem::readablePath($output_path))); } else { foreach ($iterator as $chunk) { echo $chunk;