]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add 'arc upload' and 'arc download'
authorepriestley <git@epriestley.com>
Fri, 29 Jul 2011 17:05:07 +0000 (10:05 -0700)
committerepriestley <git@epriestley.com>
Fri, 29 Jul 2011 20:38:24 +0000 (13:38 -0700)
Summary: Mechanisms for interacting with Files via Arcanist.
Test Plan:
  - Ran 'arc upload x', 'arc upload x y z'
  - Ran 'arc download' with --as and --show.

Reviewed By: codeblock
Reviewers: codeblock, jungejason, tuomaspelkonen, aran
CC: aran, codeblock, epriestley
Differential Revision: 742

src/__phutil_library_map__.php
src/workflow/download/ArcanistDownloadWorkflow.php [new file with mode: 0644]
src/workflow/download/__init__.php [new file with mode: 0644]
src/workflow/upload/ArcanistUploadWorkflow.php [new file with mode: 0644]
src/workflow/upload/__init__.php [new file with mode: 0644]

index d2696b2997ff54a1c09951c34995a0339767e62e..d20303df962be08be87c45a76de928b6b67cfccb 100644 (file)
@@ -31,6 +31,7 @@ phutil_register_library_map(array(
     'ArcanistDifferentialCommitMessage' => 'differential/commitmessage',
     'ArcanistDifferentialCommitMessageParserException' => 'differential/commitmessage',
     'ArcanistDifferentialRevisionRef' => 'differential/revision',
+    'ArcanistDownloadWorkflow' => 'workflow/download',
     'ArcanistExportWorkflow' => 'workflow/export',
     'ArcanistFilenameLinter' => 'lint/linter/filename',
     'ArcanistGeneratedLinter' => 'lint/linter/generated',
@@ -73,6 +74,7 @@ phutil_register_library_map(array(
     'ArcanistTextLinterTestCase' => 'lint/linter/text/__tests__',
     'ArcanistUnitTestResult' => 'unit/result',
     'ArcanistUnitWorkflow' => 'workflow/unit',
+    'ArcanistUploadWorkflow' => 'workflow/upload',
     'ArcanistUsageException' => 'exception/usage',
     'ArcanistUserAbortException' => 'exception/usage/userabort',
     'ArcanistWorkingCopyIdentity' => 'workingcopyidentity',
@@ -100,6 +102,7 @@ phutil_register_library_map(array(
     'ArcanistCoverWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistDiffParserTestCase' => 'ArcanistPhutilTestCase',
     'ArcanistDiffWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistDownloadWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistExportWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistFilenameLinter' => 'ArcanistLinter',
     'ArcanistGeneratedLinter' => 'ArcanistLinter',
@@ -129,6 +132,7 @@ phutil_register_library_map(array(
     'ArcanistTextLinter' => 'ArcanistLinter',
     'ArcanistTextLinterTestCase' => 'ArcanistLinterTestCase',
     'ArcanistUnitWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistUploadWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistUserAbortException' => 'ArcanistUsageException',
     'ArcanistXHPASTLinter' => 'ArcanistLinter',
     'ArcanistXHPASTLinterTestCase' => 'ArcanistLinterTestCase',
diff --git a/src/workflow/download/ArcanistDownloadWorkflow.php b/src/workflow/download/ArcanistDownloadWorkflow.php
new file mode 100644 (file)
index 0000000..51f6f19
--- /dev/null
@@ -0,0 +1,126 @@
+<?php
+
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Download a file from Phabricator.
+ *
+ * @group workflow
+ */
+final class ArcanistDownloadWorkflow extends ArcanistBaseWorkflow {
+
+  private $id;
+  private $saveAs;
+  private $show;
+
+  public function getCommandHelp() {
+    return phutil_console_format(<<<EOTEXT
+      **download** __file__ [--as __name__] [--show]
+          Supports: filesystems
+          Download a file to local disk, e.g.:
+
+            $ arc download F33              # Download file 'F33'
+
+EOTEXT
+      );
+  }
+
+  public function getArguments() {
+    return array(
+      'show' => array(
+        'conflicts' => array(
+          'as' => 'Use --show to direct the file to stdout, or --as to direct '.
+                  'it to a named location.',
+        ),
+        'help' => 'Write file to stdout instead of to disk.',
+      ),
+      'as' => array(
+        'param' => 'name',
+        'help' => 'Save the file with a specific name rather than the default.',
+      ),
+      '*' => 'argv',
+    );
+  }
+
+  protected function didParseArguments() {
+    $argv = $this->getArgument('argv');
+    if (!$argv) {
+      throw new ArcanistUsageException("Specify a file to download.");
+    }
+    if (count($argv) > 1) {
+      throw new ArcanistUsageException("Specify exactly one file to download.");
+    }
+
+    $file = reset($argv);
+    if (!preg_match('/^F?\d+/', $file)) {
+      throw new ArcanistUsageException("Specify file by ID, e.g. F123.");
+    }
+
+    $this->id = (int)ltrim($file, 'F');
+    $this->saveAs = $this->getArgument('as');
+    $this->show = $this->getArgument('show');
+  }
+
+  public function requiresAuthentication() {
+    return true;
+  }
+
+  public function run() {
+
+    $conduit = $this->getConduit();
+
+    $this->writeStatus("Getting file information...\n");
+    $info = $conduit->callMethodSynchronous(
+      'file.info',
+      array(
+        'id' => $this->id,
+      ));
+
+    $bytes = number_format($info['byteSize']);
+    $desc = '('.$bytes.' bytes)';
+    if ($info['name']) {
+      $desc = "'".$info['name']."' ".$desc;
+    }
+
+    $this->writeStatus("Downloading file {$desc}...\n");
+    $data = $conduit->callMethodSynchronous(
+      'file.download',
+      array(
+        'phid' => $info['phid'],
+      ));
+
+    $data = base64_decode($data);
+
+    if ($this->show) {
+      echo $data;
+    } else {
+      $path = Filesystem::writeUniqueFile(
+        nonempty($this->saveAs, $info['name'], 'file'),
+        $data);
+      $this->writeStatus("Saved file as '{$path}'.\n");
+    }
+
+    return 0;
+  }
+
+  private function writeStatus($msg) {
+    // Use stderr instead of stdout since we may echo file contents to
+    // stdout with --show.
+    file_put_contents('php://stderr', $msg);
+  }
+
+}
diff --git a/src/workflow/download/__init__.php b/src/workflow/download/__init__.php
new file mode 100644 (file)
index 0000000..f005112
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'exception/usage');
+phutil_require_module('arcanist', 'workflow/base');
+
+phutil_require_module('phutil', 'console');
+phutil_require_module('phutil', 'filesystem');
+phutil_require_module('phutil', 'utils');
+
+
+phutil_require_source('ArcanistDownloadWorkflow.php');
diff --git a/src/workflow/upload/ArcanistUploadWorkflow.php b/src/workflow/upload/ArcanistUploadWorkflow.php
new file mode 100644 (file)
index 0000000..e501c14
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Upload a file to Phabricator.
+ *
+ * @group workflow
+ */
+final class ArcanistUploadWorkflow extends ArcanistBaseWorkflow {
+
+  private $paths;
+
+  public function getCommandHelp() {
+    return phutil_console_format(<<<EOTEXT
+      **upload** __file__ [__file__]
+          Supports: filesystems
+          Upload a file from local disk.
+
+EOTEXT
+      );
+  }
+
+  public function getArguments() {
+    return array(
+      '*' => 'paths',
+    );
+  }
+
+  protected function didParseArguments() {
+    if (!$this->getArgument('paths')) {
+      throw new ArcanistUsageException("Specify one or more files to upload.");
+    }
+
+    $this->paths = $this->getArgument('paths');
+  }
+
+  public function requiresAuthentication() {
+    return true;
+  }
+
+  private function getPaths() {
+    return $this->paths;
+  }
+
+  public function run() {
+
+    $conduit = $this->getConduit();
+
+    foreach ($this->paths as $path) {
+      $name = basename($path);
+      echo "Uploading '{$name}'...\n";
+      try {
+        $data = Filesystem::readFile($path);
+      } catch (FilesystemException $ex) {
+        echo "Unable to upload file: ".$ex->getMessage()."\n";
+        continue;
+      }
+
+      $phid = $conduit->callMethodSynchronous(
+        'file.upload',
+        array(
+          'data_base64' => base64_encode($data),
+          'name'        => $name,
+        ));
+      $info = $conduit->callMethodSynchronous(
+        'file.info',
+        array(
+          'phid'        => $phid,
+        ));
+
+      echo "  {$name}: ".$info['uri']."\n\n";
+    }
+
+    echo "Done.\n";
+
+    return 0;
+  }
+
+}
diff --git a/src/workflow/upload/__init__.php b/src/workflow/upload/__init__.php
new file mode 100644 (file)
index 0000000..d7c813b
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'exception/usage');
+phutil_require_module('arcanist', 'workflow/base');
+
+phutil_require_module('phutil', 'console');
+phutil_require_module('phutil', 'filesystem');
+
+
+phutil_require_source('ArcanistUploadWorkflow.php');