diff --git a/src/workflow/base/ArcanistBaseWorkflow.php b/src/workflow/base/ArcanistBaseWorkflow.php index 7d0de227..59de92e2 100644 --- a/src/workflow/base/ArcanistBaseWorkflow.php +++ b/src/workflow/base/ArcanistBaseWorkflow.php @@ -919,4 +919,15 @@ class ArcanistBaseWorkflow { return $user_config; } + /** + * Write a message to stderr so that '--json' flags or stdout which is meant + * to be piped somewhere aren't disrupted. + * + * @param string Message to write to stderr. + * @return void + */ + protected function writeStatusMessage($msg) { + file_put_contents('php://stderr', $msg); + } + } diff --git a/src/workflow/download/ArcanistDownloadWorkflow.php b/src/workflow/download/ArcanistDownloadWorkflow.php index 51f6f195..6c40f762 100644 --- a/src/workflow/download/ArcanistDownloadWorkflow.php +++ b/src/workflow/download/ArcanistDownloadWorkflow.php @@ -83,7 +83,7 @@ EOTEXT $conduit = $this->getConduit(); - $this->writeStatus("Getting file information...\n"); + $this->writeStatusMessage("Getting file information...\n"); $info = $conduit->callMethodSynchronous( 'file.info', array( @@ -96,7 +96,7 @@ EOTEXT $desc = "'".$info['name']."' ".$desc; } - $this->writeStatus("Downloading file {$desc}...\n"); + $this->writeStatusMessage("Downloading file {$desc}...\n"); $data = $conduit->callMethodSynchronous( 'file.download', array( @@ -111,16 +111,10 @@ EOTEXT $path = Filesystem::writeUniqueFile( nonempty($this->saveAs, $info['name'], 'file'), $data); - $this->writeStatus("Saved file as '{$path}'.\n"); + $this->writeStatusMessage("Saved file as '{$path}'.\n"); } return 0; } - private function writeStatus($msg) { - // Use stderr instead of stdout since we may echo file contents to - // stdout with --show. - file_put_contents('php://stderr', $msg); - } - } diff --git a/src/workflow/paste/ArcanistPasteWorkflow.php b/src/workflow/paste/ArcanistPasteWorkflow.php index 96803656..798ab3d8 100644 --- a/src/workflow/paste/ArcanistPasteWorkflow.php +++ b/src/workflow/paste/ArcanistPasteWorkflow.php @@ -137,7 +137,7 @@ EOTEXT $conduit = $this->getConduit(); // Avoid confusion when people type "arc paste" with nothing else. - file_put_contents('php://stderr', "Reading paste from stdin...\n"); + $this->writeStatusMessage("Reading paste from stdin...\n"); $info = $conduit->callMethodSynchronous( 'paste.create', diff --git a/src/workflow/upload/ArcanistUploadWorkflow.php b/src/workflow/upload/ArcanistUploadWorkflow.php index e501c14b..0bbbe1a1 100644 --- a/src/workflow/upload/ArcanistUploadWorkflow.php +++ b/src/workflow/upload/ArcanistUploadWorkflow.php @@ -24,10 +24,11 @@ final class ArcanistUploadWorkflow extends ArcanistBaseWorkflow { private $paths; + private $json; public function getCommandHelp() { return phutil_console_format(<< array( + 'help' => 'Output upload information in JSON format.', + ), '*' => 'paths', ); } @@ -47,6 +51,7 @@ EOTEXT } $this->paths = $this->getArgument('paths'); + $this->json = $this->getArgument('json'); } public function requiresAuthentication() { @@ -57,17 +62,25 @@ EOTEXT return $this->paths; } + private function getJSON() { + return $this->json; + } + public function run() { $conduit = $this->getConduit(); + $results = array(); + foreach ($this->paths as $path) { $name = basename($path); - echo "Uploading '{$name}'...\n"; + $this->writeStatusMessage("Uploading '{$name}'...\n"); try { $data = Filesystem::readFile($path); } catch (FilesystemException $ex) { - echo "Unable to upload file: ".$ex->getMessage()."\n"; + $this->writeStatusMessage( + "Unable to upload file: ".$ex->getMessage()."\n"); + $results[$path] = null; continue; } @@ -83,10 +96,18 @@ EOTEXT 'phid' => $phid, )); - echo " {$name}: ".$info['uri']."\n\n"; + $results[$path] = $info; + + if (!$this->getJSON()) { + echo " {$name}: ".$info['uri']."\n\n"; + } } - echo "Done.\n"; + if ($this->getJSON()) { + echo json_encode($results)."\n"; + } else { + $this->writeStatusMessage("Done.\n"); + } return 0; }