Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistPasteWorkflow.php
| Show All 35 Lines | |||||
| 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' => pht('Title for the paste.'), | ||||
| ), | ), | ||||
| 'lang' => array( | 'lang' => array( | ||||
| 'param' => 'language', | 'param' => 'language', | ||||
| 'help' => 'Language for syntax highlighting.', | 'help' => pht('Language for syntax highlighting.'), | ||||
| ), | ), | ||||
| 'json' => array( | 'json' => array( | ||||
| 'help' => 'Output in JSON format.', | 'help' => pht('Output in JSON format.'), | ||||
| ), | ), | ||||
| '*' => 'argv', | '*' => 'argv', | ||||
| ); | ); | ||||
| } | } | ||||
| public function requiresAuthentication() { | public function requiresAuthentication() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| protected function didParseArguments() { | protected function didParseArguments() { | ||||
| $this->json = $this->getArgument('json'); | |||||
| $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'); | |||||
| $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( | ||||
| pht('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( | ||||
| pht( | |||||
| 'Specify a paste ID, like %s.', | |||||
| 'P123')); | |||||
| } | } | ||||
| $this->id = (int)ltrim($id, 'P'); | $this->id = (int)ltrim($id, 'P'); | ||||
| if ($this->language || $this->title) { | if ($this->language || $this->title) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| 'Use options --lang and --title only when creating pastes.'); | pht( | ||||
| } | 'Use options %s and %s only when creating pastes.', | ||||
| } | '--lang', | ||||
| '--title')); | |||||
| } | } | ||||
| private function getTitle() { | |||||
| return $this->title; | |||||
| } | |||||
| private function getLanguage() { | |||||
| return $this->language; | |||||
| } | } | ||||
| private function getJSON() { | |||||
| return $this->json; | |||||
| } | } | ||||
| public function run() { | public function run() { | ||||
| if ($this->id) { | 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(); | ||||
| $info = $conduit->callMethodSynchronous( | $info = $conduit->callMethodSynchronous( | ||||
| 'paste.query', | 'paste.query', | ||||
| array( | array( | ||||
| 'ids' => array($this->id), | 'ids' => array($this->id), | ||||
| )); | )); | ||||
| $info = head($info); | $info = head($info); | ||||
| if ($this->getJSON()) { | if ($this->json) { | ||||
| echo json_encode($info)."\n"; | echo json_encode($info)."\n"; | ||||
| } else { | } else { | ||||
| echo $info['content']; | echo $info['content']; | ||||
| if (!preg_match('/\\n$/', $info['content'])) { | if (!preg_match('/\\n$/', $info['content'])) { | ||||
| // If there's no newline, add one, since it looks stupid otherwise. If | // If there's no newline, add one, since it looks stupid otherwise. If | ||||
| // you want byte-for-byte equivalence you can use --json. | // you want byte-for-byte equivalence you can use `--json`. | ||||
| echo "\n"; | echo "\n"; | ||||
| } | } | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function createPaste() { | private function createPaste() { | ||||
| $conduit = $this->getConduit(); | $conduit = $this->getConduit(); | ||||
| // Avoid confusion when people type "arc paste" with nothing else. | if (!function_exists('posix_isatty') || posix_isatty(STDIN)) { | ||||
| $this->writeStatusMessage("Reading paste from stdin...\n"); | $this->writeStatusMessage("Reading paste from stdin...\n"); | ||||
| } | |||||
| $info = $conduit->callMethodSynchronous( | $info = $conduit->callMethodSynchronous( | ||||
| 'paste.create', | 'paste.create', | ||||
| array( | array( | ||||
| 'content' => file_get_contents('php://stdin'), | 'content' => file_get_contents('php://stdin'), | ||||
| 'title' => $this->getTitle(), | 'title' => $this->title, | ||||
| 'language' => $this->getLanguage(), | 'language' => $this->language, | ||||
| )); | )); | ||||
| if ($this->getArgument('json')) { | 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; | ||||
| } | } | ||||
| } | } | ||||