2011-07-30 03:55:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a chunk of text to the Paste application, or download one.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
|
|
|
final class ArcanistPasteWorkflow extends ArcanistBaseWorkflow {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
private $language;
|
|
|
|
private $title;
|
|
|
|
private $json;
|
|
|
|
|
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 'paste';
|
|
|
|
}
|
|
|
|
|
2012-03-05 19:02:37 +01:00
|
|
|
public function getCommandSynopses() {
|
2011-07-30 03:55:01 +02:00
|
|
|
return phutil_console_format(<<<EOTEXT
|
|
|
|
**paste** [--title __title__] [--lang __language__] [--json]
|
|
|
|
**paste** __id__ [--json]
|
2012-03-05 19:02:37 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-07-30 03:55:01 +02:00
|
|
|
Supports: text
|
|
|
|
Share and grab text using the Paste application. To create a paste,
|
|
|
|
use stdin to provide the text:
|
|
|
|
|
|
|
|
$ cat list_of_ducks.txt | arc paste
|
|
|
|
|
|
|
|
To retrieve a paste, specify the paste ID:
|
|
|
|
|
|
|
|
$ arc paste P123
|
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArguments() {
|
|
|
|
return array(
|
|
|
|
'title' => array(
|
|
|
|
'param' => 'title',
|
|
|
|
'help' => 'Title for the paste.',
|
|
|
|
),
|
|
|
|
'lang' => array(
|
|
|
|
'param' => 'language',
|
|
|
|
'help' => 'Language for syntax highlighting.',
|
|
|
|
),
|
|
|
|
'json' => array(
|
|
|
|
'help' => 'Output in JSON format.',
|
|
|
|
),
|
|
|
|
'*' => 'argv',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresAuthentication() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function didParseArguments() {
|
|
|
|
$this->language = $this->getArgument('lang');
|
|
|
|
$this->title = $this->getArgument('title');
|
|
|
|
$this->json = $this->getArgument('json');
|
|
|
|
|
|
|
|
$argv = $this->getArgument('argv');
|
|
|
|
if (count($argv) > 1) {
|
|
|
|
throw new ArcanistUsageException("Specify only one paste to retrieve.");
|
|
|
|
} else if (count($argv) == 1) {
|
|
|
|
$id = $argv[0];
|
|
|
|
if (!preg_match('/^P?\d+/', $id)) {
|
|
|
|
throw new ArcanistUsageException("Specify a paste ID, like P123.");
|
|
|
|
}
|
|
|
|
$this->id = (int)ltrim($id, 'P');
|
|
|
|
|
|
|
|
if ($this->language || $this->title) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"Use options --lang and --title only when creating pastes.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getLanguage() {
|
|
|
|
return $this->language;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getJSON() {
|
|
|
|
return $this->json;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
|
|
|
|
if ($this->id) {
|
|
|
|
return $this->getPaste();
|
|
|
|
} else {
|
|
|
|
return $this->createPaste();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPaste() {
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
|
|
|
$info = $conduit->callMethodSynchronous(
|
2012-08-29 20:04:19 +02:00
|
|
|
'paste.query',
|
2011-07-30 03:55:01 +02:00
|
|
|
array(
|
2012-08-29 20:04:19 +02:00
|
|
|
'ids' => array($this->id),
|
2011-07-30 03:55:01 +02:00
|
|
|
));
|
2012-08-29 20:04:19 +02:00
|
|
|
$info = head($info);
|
2011-07-30 03:55:01 +02:00
|
|
|
|
|
|
|
if ($this->getJSON()) {
|
|
|
|
echo json_encode($info)."\n";
|
|
|
|
} else {
|
|
|
|
echo $info['content'];
|
|
|
|
if (!preg_match('/\\n$/', $info['content'])) {
|
|
|
|
// If there's no newline, add one, since it looks stupid otherwise. If
|
|
|
|
// you want byte-for-byte equivalence you can use --json.
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPaste() {
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
|
|
|
// Avoid confusion when people type "arc paste" with nothing else.
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage("Reading paste from stdin...\n");
|
2011-07-30 03:55:01 +02:00
|
|
|
|
|
|
|
$info = $conduit->callMethodSynchronous(
|
|
|
|
'paste.create',
|
|
|
|
array(
|
|
|
|
'content' => file_get_contents('php://stdin'),
|
|
|
|
'title' => $this->getTitle(),
|
|
|
|
'language' => $this->getLanguage(),
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($this->getArgument('json')) {
|
|
|
|
echo json_encode($info)."\n";
|
|
|
|
} else {
|
|
|
|
echo $info['objectName'].': '.$info['uri']."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|