From 0e4f7774fb25880c909aa345070882535d4370cb Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 29 Jul 2011 18:55:01 -0700 Subject: [PATCH] Add an "arc paste" workflow Summary: Read and write in the same workflow! Dogs and cats living together! Test Plan: - Performed a bunch of paste reads and writes and they looked ok? Reviewed By: aran Reviewers: codeblock, jungejason, tuomaspelkonen, aran CC: aran Differential Revision: 748 --- src/__phutil_library_map__.php | 2 + src/workflow/paste/ArcanistPasteWorkflow.php | 159 +++++++++++++++++++ src/workflow/paste/__init__.php | 15 ++ 3 files changed, 176 insertions(+) create mode 100644 src/workflow/paste/ArcanistPasteWorkflow.php create mode 100644 src/workflow/paste/__init__.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index d20303df..2d7d4f02 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -60,6 +60,7 @@ phutil_register_library_map(array( 'ArcanistNoLintLinter' => 'lint/linter/nolint', 'ArcanistNoLintTestCaseMisnamed' => 'lint/linter/nolint/__tests__', 'ArcanistPEP8Linter' => 'lint/linter/pep8', + 'ArcanistPasteWorkflow' => 'workflow/paste', 'ArcanistPatchWorkflow' => 'workflow/patch', 'ArcanistPhutilModuleLinter' => 'lint/linter/phutilmodule', 'ArcanistPhutilTestCase' => 'unit/engine/phutil/testcase', @@ -122,6 +123,7 @@ phutil_register_library_map(array( 'ArcanistNoLintLinter' => 'ArcanistLinter', 'ArcanistNoLintTestCaseMisnamed' => 'ArcanistLinterTestCase', 'ArcanistPEP8Linter' => 'ArcanistLinter', + 'ArcanistPasteWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistPatchWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistPhutilModuleLinter' => 'ArcanistLinter', 'ArcanistPyFlakesLinter' => 'ArcanistLinter', diff --git a/src/workflow/paste/ArcanistPasteWorkflow.php b/src/workflow/paste/ArcanistPasteWorkflow.php new file mode 100644 index 00000000..96803656 --- /dev/null +++ b/src/workflow/paste/ArcanistPasteWorkflow.php @@ -0,0 +1,159 @@ + 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( + 'paste.info', + array( + 'paste_id' => $this->id, + )); + + 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. + file_put_contents('php://stderr', "Reading paste from stdin...\n"); + + $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; + } + +} diff --git a/src/workflow/paste/__init__.php b/src/workflow/paste/__init__.php new file mode 100644 index 00000000..3ef0f7e3 --- /dev/null +++ b/src/workflow/paste/__init__.php @@ -0,0 +1,15 @@ +