Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistPasteWorkflow.php
| <?php | <?php | ||||
| /** | /** | ||||
| * Upload a chunk of text to the Paste application, or download one. | * Upload a chunk of text to the Paste application, or download one. | ||||
| */ | */ | ||||
| final class ArcanistPasteWorkflow extends ArcanistWorkflow { | final class ArcanistPasteWorkflow extends ArcanistWorkflow { | ||||
| private $id; | private $id; | ||||
| private $language; | private $language; | ||||
| private $title; | private $title; | ||||
| private $json; | private $json; | ||||
| private $edit; | |||||
| public function getWorkflowName() { | public function getWorkflowName() { | ||||
| return 'paste'; | return 'paste'; | ||||
| } | } | ||||
| public function getCommandSynopses() { | public function getCommandSynopses() { | ||||
| return phutil_console_format(<<<EOTEXT | return phutil_console_format(<<<EOTEXT | ||||
| **paste** [--title __title__] [--lang __language__] [--json] | **paste** [--title __title__] [--lang __language__] [--json] | ||||
| **paste** __id__ [--json] | **paste** __id__ [--json] | ||||
| **paste** __id__ --edit [--title __title__] [--lang __language__] [--json] | |||||
| EOTEXT | EOTEXT | ||||
| ); | ); | ||||
| } | } | ||||
| public function getCommandHelp() { | public function getCommandHelp() { | ||||
| return phutil_console_format(<<<EOTEXT | return phutil_console_format(<<<EOTEXT | ||||
| Supports: text | 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 | ||||
| To edit a paste, specify the paste ID and --edit option: | |||||
| $ cat list_of_ducks.txt | arc paste P123 --edit | |||||
| EOTEXT | EOTEXT | ||||
| ); | ); | ||||
| } | } | ||||
| public function getArguments() { | public function getArguments() { | ||||
| return array( | return array( | ||||
| 'title' => array( | 'title' => array( | ||||
| 'param' => 'title', | 'param' => 'title', | ||||
| 'help' => 'Title for the paste.', | 'help' => 'Title for the paste.', | ||||
| ), | ), | ||||
| 'lang' => array( | 'lang' => array( | ||||
| 'param' => 'language', | 'param' => 'language', | ||||
| 'help' => 'Language for syntax highlighting.', | 'help' => 'Language for syntax highlighting.', | ||||
| ), | ), | ||||
| 'json' => array( | 'json' => array( | ||||
| 'help' => 'Output in JSON format.', | 'help' => 'Output in JSON format.', | ||||
| ), | ), | ||||
| 'edit' => array( | |||||
| 'help' => 'Edit an existing paste.', | |||||
| ), | |||||
| '*' => 'argv', | '*' => 'argv', | ||||
| ); | ); | ||||
| } | } | ||||
| public function requiresAuthentication() { | public function requiresAuthentication() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| protected function didParseArguments() { | protected function didParseArguments() { | ||||
| $this->language = $this->getArgument('lang'); | $this->language = $this->getArgument('lang'); | ||||
| $this->title = $this->getArgument('title'); | $this->title = $this->getArgument('title'); | ||||
| $this->json = $this->getArgument('json'); | $this->json = $this->getArgument('json'); | ||||
| $this->edit = $this->getArgument('edit'); | |||||
| $argv = $this->getArgument('argv'); | $argv = $this->getArgument('argv'); | ||||
| if (count($argv) > 1) { | if (count($argv) > 1) { | ||||
| throw new ArcanistUsageException('Specify only one paste to retrieve.'); | throw new ArcanistUsageException('Specify only one paste to retrieve.'); | ||||
| } else if (count($argv) == 1) { | } else if (count($argv) == 1) { | ||||
| $id = $argv[0]; | $id = $argv[0]; | ||||
| if (!preg_match('/^P?\d+/', $id)) { | if (!preg_match('/^P?\d+/', $id)) { | ||||
| throw new ArcanistUsageException('Specify a paste ID, like P123.'); | throw new ArcanistUsageException('Specify a paste ID, like P123.'); | ||||
| } | } | ||||
| $this->id = (int)ltrim($id, 'P'); | $this->id = (int)ltrim($id, 'P'); | ||||
| if ($this->language || $this->title) { | if (!$this->getEdit() && ($this->language || $this->title)) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| 'Use options --lang and --title only when creating pastes.'); | 'Use options --lang and --title only when creating or editing '. | ||||
| 'pastes.'); | |||||
| } | } | ||||
| } | } | ||||
| if ($this->getEdit() && !$this->id) { | |||||
| throw new ArcanistUsageException('Specify a paste to edit.'); | |||||
| } | |||||
| } | } | ||||
| private function getTitle() { | private function getTitle() { | ||||
| return $this->title; | return $this->title; | ||||
| } | } | ||||
| private function getLanguage() { | private function getLanguage() { | ||||
| return $this->language; | return $this->language; | ||||
| } | } | ||||
| private function getJSON() { | private function getJSON() { | ||||
| return $this->json; | return $this->json; | ||||
| } | } | ||||
| private function getEdit() { | |||||
| return $this->edit; | |||||
| } | |||||
| public function run() { | public function run() { | ||||
| if ($this->id) { | if ($this->getEdit()) { | ||||
| return $this->editPaste(); | |||||
| } else if ($this->id) { | |||||
| return $this->getPaste(); | return $this->getPaste(); | ||||
| } else { | } else { | ||||
| return $this->createPaste(); | return $this->createPaste(); | ||||
| } | } | ||||
| } | } | ||||
| private function getPaste() { | private function getPaste() { | ||||
| $conduit = $this->getConduit(); | $conduit = $this->getConduit(); | ||||
| Show All 37 Lines | if ($this->getArgument('json')) { | ||||
| echo json_encode($info)."\n"; | echo json_encode($info)."\n"; | ||||
| } else { | } else { | ||||
| echo $info['objectName'].': '.$info['uri']."\n"; | echo $info['objectName'].': '.$info['uri']."\n"; | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function editPaste() { | |||||
| $conduit = $this->getConduit(); | |||||
| // Avoid confusion when people type "arc paste" with nothing else. | |||||
| $this->writeStatusMessage("Reading paste from stdin...\n"); | |||||
| $info = $conduit->callMethodSynchronous( | |||||
| 'paste.edit', | |||||
| array( | |||||
| 'paste_id' => $this->id, | |||||
| '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; | |||||
| } | |||||
| } | } | ||||