2011-07-29 19:05:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a file from Phabricator.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
|
|
|
final class ArcanistDownloadWorkflow extends ArcanistBaseWorkflow {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
private $saveAs;
|
|
|
|
private $show;
|
|
|
|
|
Make Arcanist workflow names explicit
Summary:
Currently, adding a new workflow requires you to override ArcanistConfiguration, which is messy. Instead, just load everything that extends ArcanistBaseWorkflow.
Remove all the rules tying workflow names to class names through arcane incantations.
This has a very small performance cost in that we need to load every Workflow class every time now, but we don't hit __init__ and such anymore and it was pretty negligible on my machine (98ms vs 104ms or something).
Test Plan: Ran "arc help", "arc which", "arc diff", etc.
Reviewers: edward, vrana, btrahan
Reviewed By: edward
CC: aran, zeeg
Differential Revision: https://secure.phabricator.com/D3691
2012-10-17 17:35:03 +02:00
|
|
|
public function getWorkflowName() {
|
|
|
|
return 'download';
|
|
|
|
}
|
|
|
|
|
2012-03-05 19:02:37 +01:00
|
|
|
public function getCommandSynopses() {
|
2011-07-29 19:05:07 +02:00
|
|
|
return phutil_console_format(<<<EOTEXT
|
|
|
|
**download** __file__ [--as __name__] [--show]
|
2012-03-05 19:02:37 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-07-29 19:05:07 +02:00
|
|
|
Supports: filesystems
|
|
|
|
Download a file to local disk, e.g.:
|
|
|
|
|
|
|
|
$ arc download F33 # Download file 'F33'
|
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArguments() {
|
|
|
|
return array(
|
|
|
|
'show' => array(
|
|
|
|
'conflicts' => array(
|
|
|
|
'as' => 'Use --show to direct the file to stdout, or --as to direct '.
|
|
|
|
'it to a named location.',
|
|
|
|
),
|
|
|
|
'help' => 'Write file to stdout instead of to disk.',
|
|
|
|
),
|
|
|
|
'as' => array(
|
|
|
|
'param' => 'name',
|
|
|
|
'help' => 'Save the file with a specific name rather than the default.',
|
|
|
|
),
|
|
|
|
'*' => 'argv',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function didParseArguments() {
|
|
|
|
$argv = $this->getArgument('argv');
|
|
|
|
if (!$argv) {
|
|
|
|
throw new ArcanistUsageException("Specify a file to download.");
|
|
|
|
}
|
|
|
|
if (count($argv) > 1) {
|
|
|
|
throw new ArcanistUsageException("Specify exactly one file to download.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = reset($argv);
|
2012-03-04 08:55:50 +01:00
|
|
|
if (!preg_match('/^F?\d+$/', $file)) {
|
2011-07-29 19:05:07 +02:00
|
|
|
throw new ArcanistUsageException("Specify file by ID, e.g. F123.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->id = (int)ltrim($file, 'F');
|
|
|
|
$this->saveAs = $this->getArgument('as');
|
|
|
|
$this->show = $this->getArgument('show');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresAuthentication() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage("Getting file information...\n");
|
2011-07-29 19:05:07 +02:00
|
|
|
$info = $conduit->callMethodSynchronous(
|
|
|
|
'file.info',
|
|
|
|
array(
|
|
|
|
'id' => $this->id,
|
|
|
|
));
|
|
|
|
|
|
|
|
$bytes = number_format($info['byteSize']);
|
|
|
|
$desc = '('.$bytes.' bytes)';
|
|
|
|
if ($info['name']) {
|
|
|
|
$desc = "'".$info['name']."' ".$desc;
|
|
|
|
}
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage("Downloading file {$desc}...\n");
|
2011-07-29 19:05:07 +02:00
|
|
|
$data = $conduit->callMethodSynchronous(
|
|
|
|
'file.download',
|
|
|
|
array(
|
|
|
|
'phid' => $info['phid'],
|
|
|
|
));
|
|
|
|
|
|
|
|
$data = base64_decode($data);
|
|
|
|
|
|
|
|
if ($this->show) {
|
|
|
|
echo $data;
|
|
|
|
} else {
|
|
|
|
$path = Filesystem::writeUniqueFile(
|
|
|
|
nonempty($this->saveAs, $info['name'], 'file'),
|
|
|
|
$data);
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage("Saved file as '{$path}'.\n");
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|