Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistPasteWorkflow.php
<?php | <?php | ||||
/** | final class ArcanistPasteWorkflow | ||||
* Upload a chunk of text to the Paste application, or download one. | extends ArcanistArcWorkflow { | ||||
*/ | |||||
final class ArcanistPasteWorkflow extends ArcanistWorkflow { | |||||
private $id; | |||||
private $language; | |||||
private $title; | |||||
private $json; | |||||
public function getWorkflowName() { | public function getWorkflowName() { | ||||
return 'paste'; | return 'paste'; | ||||
} | } | ||||
public function getCommandSynopses() { | public function getWorkflowInformation() { | ||||
return phutil_console_format(<<<EOTEXT | $help = pht(<<<EOTEXT | ||||
**paste** [--title __title__] [--lang __language__] [--json] | |||||
**paste** __id__ [--json] | |||||
EOTEXT | |||||
); | |||||
} | |||||
public function getCommandHelp() { | |||||
return phutil_console_format(<<<EOTEXT | |||||
Supports: text | |||||
Share and grab text using the Paste application. To create a paste, | Share and grab text using the Paste application. To create a paste, | ||||
use stdin to provide the text: | use stdin to provide the text: | ||||
$ cat list_of_ducks.txt | arc paste | $ cat list_of_ducks.txt | arc paste | ||||
To retrieve a paste, specify the paste ID: | To retrieve a paste, specify the paste ID: | ||||
$ arc paste P123 | $ arc paste P123 | ||||
EOTEXT | EOTEXT | ||||
); | ); | ||||
return $this->newWorkflowInformation() | |||||
->addExample('**paste** [__options__] --') | |||||
->addExample('**paste** [__options__] -- __object__') | |||||
->setHelp($help); | |||||
} | } | ||||
public function getArguments() { | public function getWorkflowArguments() { | ||||
return array( | return array( | ||||
'title' => array( | $this->newWorkflowArgument('title') | ||||
'param' => 'title', | ->setParameter('title') | ||||
'help' => pht('Title for the paste.'), | ->setHelp(pht('Title for the paste.')), | ||||
), | $this->newWorkflowArgument('lang') | ||||
'lang' => array( | ->setParameter('language') | ||||
'param' => 'language', | ->setHelp(pht('Language for the paste.')), | ||||
'help' => pht('Language for syntax highlighting.'), | $this->newWorkflowArgument('json') | ||||
), | ->setHelp(pht('Output in JSON format.')), | ||||
'json' => array( | $this->newWorkflowArgument('argv') | ||||
'help' => pht('Output in JSON format.'), | ->setWildcard(true), | ||||
), | |||||
'*' => 'argv', | |||||
); | ); | ||||
} | } | ||||
public function requiresAuthentication() { | public function runWorkflow() { | ||||
return true; | $set_language = $this->getArgument('lang'); | ||||
} | $set_title = $this->getArgument('title'); | ||||
protected function didParseArguments() { | |||||
$this->json = $this->getArgument('json'); | |||||
$this->language = $this->getArgument('lang'); | |||||
$this->title = $this->getArgument('title'); | |||||
$argv = $this->getArgument('argv'); | $argv = $this->getArgument('argv'); | ||||
if (count($argv) > 1) { | if (count($argv) > 1) { | ||||
throw new ArcanistUsageException( | throw new PhutilArgumentUsageException( | ||||
pht('Specify only one paste to retrieve.')); | |||||
} else if (count($argv) == 1) { | |||||
$id = $argv[0]; | |||||
if (!preg_match('/^P?\d+/', $id)) { | |||||
throw new ArcanistUsageException( | |||||
pht( | pht( | ||||
'Specify a paste ID, like %s.', | 'Specify only one paste to retrieve.')); | ||||
'P123')); | |||||
} | } | ||||
$this->id = (int)ltrim($id, 'P'); | |||||
if ($this->language || $this->title) { | $symbols = $this->getSymbolEngine(); | ||||
throw new ArcanistUsageException( | |||||
if (count($argv) === 1) { | |||||
if ($set_language !== null) { | |||||
throw new PhutilArgumentUsageException( | |||||
pht( | pht( | ||||
'Use options %s and %s only when creating pastes.', | 'Flag "--lang" is not supported when reading pastes.')); | ||||
'--lang', | |||||
'--title')); | |||||
} | |||||
} | |||||
} | } | ||||
public function run() { | if ($set_title !== null) { | ||||
if ($this->id) { | throw new PhutilArgumentUsageException( | ||||
return $this->getPaste(); | pht( | ||||
} else { | 'Flag "--title" is not supported when reading pastes.')); | ||||
return $this->createPaste(); | |||||
} | |||||
} | } | ||||
private function getPaste() { | $paste_symbol = $argv[0]; | ||||
$conduit = $this->getConduit(); | |||||
$info = $conduit->callMethodSynchronous( | |||||
'paste.query', | |||||
array( | |||||
'ids' => array($this->id), | |||||
)); | |||||
$info = head($info); | |||||
if ($this->json) { | $paste_ref = $symbols->loadPasteForSymbol($paste_symbol); | ||||
echo json_encode($info)."\n"; | if (!$paste_ref) { | ||||
} else { | throw new PhutilArgumentUsageException( | ||||
echo $info['content']; | pht( | ||||
if (!preg_match('/\\n$/', $info['content'])) { | 'Paste "%s" does not exist, or you do not have access '. | ||||
// If there's no newline, add one, since it looks stupid otherwise. If | 'to see it.')); | ||||
// you want byte-for-byte equivalence you can use `--json`. | |||||
echo "\n"; | |||||
} | |||||
} | } | ||||
echo $paste_ref->getContent(); | |||||
return 0; | return 0; | ||||
} | } | ||||
private function createPaste() { | $content = $this->readStdin(); | ||||
$conduit = $this->getConduit(); | |||||
$xactions = array(); | |||||
if (!function_exists('posix_isatty') || posix_isatty(STDIN)) { | if ($set_title === null) { | ||||
$this->writeStatusMessage(pht('Reading paste from stdin...')."\n"); | $set_title = pht('Command-Line Input'); | ||||
} | } | ||||
$info = $conduit->callMethodSynchronous( | $xactions[] = array( | ||||
'paste.create', | 'type' => 'title', | ||||
array( | 'value' => $set_title, | ||||
'content' => file_get_contents('php://stdin'), | ); | ||||
'title' => $this->title, | |||||
'language' => $this->language, | if ($set_language !== null) { | ||||
)); | $xactions[] = array( | ||||
'type' => 'language', | |||||
if ($this->getArgument('json')) { | 'value' => $set_language, | ||||
echo json_encode($info)."\n"; | ); | ||||
} else { | |||||
echo $info['objectName'].': '.$info['uri']."\n"; | |||||
} | } | ||||
$xactions[] = array( | |||||
'type' => 'text', | |||||
'value' => $content, | |||||
); | |||||
$method = 'paste.edit'; | |||||
$parameters = array( | |||||
'transactions' => $xactions, | |||||
); | |||||
$conduit_engine = $this->getConduitEngine(); | |||||
$conduit_call = $conduit_engine->newCall($method, $parameters); | |||||
$conduit_future = $conduit_engine->newFuture($conduit_call); | |||||
$result = $conduit_future->resolve(); | |||||
$paste_phid = idxv($result, array('object', 'phid')); | |||||
$paste_ref = $symbols->loadPasteForSymbol($paste_phid); | |||||
$log = $this->getLogEngine(); | |||||
$log->writeSuccess( | |||||
pht('DONE'), | |||||
pht('Created a new paste.')); | |||||
echo tsprintf( | |||||
'%s', | |||||
$paste_ref->newDisplayRef() | |||||
->setURI($paste_ref->getURI())); | |||||
return 0; | return 0; | ||||
} | } | ||||
} | } |