1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-21 22:32:41 +01:00

Provide "arc upload --json"

Summary:
  - Provide a "--json" flag for "arc upload"
  - Unify some of the stderr stuff across upload/download/paste.

Test Plan:
  - Ran "arc upload" and "arc upload --json", piped stderr away with 2>/dev/null
to verify only JSON got emitted to stdout
  - Ran "arc paste"

Reviewed By: codeblock
Reviewers: codeblock, jungejason, tuomaspelkonen, aran
CC: aran, codeblock
Differential Revision: 749
This commit is contained in:
epriestley 2011-07-29 20:17:49 -07:00
parent 0e4f7774fb
commit 57ff80e6c4
4 changed files with 41 additions and 15 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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',

View file

@ -24,10 +24,11 @@
final class ArcanistUploadWorkflow extends ArcanistBaseWorkflow {
private $paths;
private $json;
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
**upload** __file__ [__file__]
**upload** __file__ [__file__ ...] [--json]
Supports: filesystems
Upload a file from local disk.
@ -37,6 +38,9 @@ EOTEXT
public function getArguments() {
return array(
'json' => 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;
}