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);
+ }
+
}
$conduit = $this->getConduit();
- $this->writeStatus("Getting file information...\n");
+ $this->writeStatusMessage("Getting file information...\n");
$info = $conduit->callMethodSynchronous(
'file.info',
array(
$desc = "'".$info['name']."' ".$desc;
}
- $this->writeStatus("Downloading file {$desc}...\n");
+ $this->writeStatusMessage("Downloading file {$desc}...\n");
$data = $conduit->callMethodSynchronous(
'file.download',
array(
$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);
- }
-
}
$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',
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.
public function getArguments() {
return array(
+ 'json' => array(
+ 'help' => 'Output upload information in JSON format.',
+ ),
'*' => 'paths',
);
}
}
$this->paths = $this->getArgument('paths');
+ $this->json = $this->getArgument('json');
}
public function requiresAuthentication() {
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;
}
'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;
}