]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Provide "arc upload --json"
authorepriestley <git@epriestley.com>
Sat, 30 Jul 2011 03:17:49 +0000 (20:17 -0700)
committerepriestley <git@epriestley.com>
Tue, 2 Aug 2011 00:44:43 +0000 (17:44 -0700)
Summary:
  - Provide a "--json" flag for "arc upload"
  - Unify some of the stderr stuff across upload/download/paste.

Test Plan:
  - Ran "arc upload" and "arc upload --json", piped stderr away with 2>/dev/null
to verify only JSON got emitted to stdout
  - Ran "arc paste"

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

src/workflow/base/ArcanistBaseWorkflow.php
src/workflow/download/ArcanistDownloadWorkflow.php
src/workflow/paste/ArcanistPasteWorkflow.php
src/workflow/upload/ArcanistUploadWorkflow.php

index 7d0de227bf90ac1b7391252c29e9a535344f867c..59de92e21521f1cfa6a56e3fec3f8246096ff0df 100644 (file)
@@ -919,4 +919,15 @@ class ArcanistBaseWorkflow {
     return $user_config;
   }
 
+  /**
+   * Write a message to stderr so that '--json' flags or stdout which is meant
+   * to be piped somewhere aren't disrupted.
+   *
+   * @param string  Message to write to stderr.
+   * @return void
+   */
+  protected function writeStatusMessage($msg) {
+    file_put_contents('php://stderr', $msg);
+  }
+
 }
index 51f6f19517f9dccc8b73907fff4810d7ba436eb0..6c40f762f9c6c4db7768e9162e64f0a28bebbc23 100644 (file)
@@ -83,7 +83,7 @@ EOTEXT
 
     $conduit = $this->getConduit();
 
-    $this->writeStatus("Getting file information...\n");
+    $this->writeStatusMessage("Getting file information...\n");
     $info = $conduit->callMethodSynchronous(
       'file.info',
       array(
@@ -96,7 +96,7 @@ EOTEXT
       $desc = "'".$info['name']."' ".$desc;
     }
 
-    $this->writeStatus("Downloading file {$desc}...\n");
+    $this->writeStatusMessage("Downloading file {$desc}...\n");
     $data = $conduit->callMethodSynchronous(
       'file.download',
       array(
@@ -111,16 +111,10 @@ EOTEXT
       $path = Filesystem::writeUniqueFile(
         nonempty($this->saveAs, $info['name'], 'file'),
         $data);
-      $this->writeStatus("Saved file as '{$path}'.\n");
+      $this->writeStatusMessage("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);
-  }
-
 }
index 968036562402577a3a2c82bc5f6cc14a8d23315e..798ab3d8a9644f2a9ec0ad2cc0ebdb066b1345a3 100644 (file)
@@ -137,7 +137,7 @@ EOTEXT
     $conduit = $this->getConduit();
 
     // Avoid confusion when people type "arc paste" with nothing else.
-    file_put_contents('php://stderr', "Reading paste from stdin...\n");
+    $this->writeStatusMessage("Reading paste from stdin...\n");
 
     $info = $conduit->callMethodSynchronous(
       'paste.create',
index e501c14bf1eb123a04909d0ec500313826ce0a5b..0bbbe1a1f93f4e3d8ecf53d4027c3ce035440a69 100644 (file)
 final class ArcanistUploadWorkflow extends ArcanistBaseWorkflow {
 
   private $paths;
+  private $json;
 
   public function getCommandHelp() {
     return phutil_console_format(<<<EOTEXT
-      **upload** __file__ [__file__]
+      **upload** __file__ [__file__ ...] [--json]
           Supports: filesystems
           Upload a file from local disk.
 
@@ -37,6 +38,9 @@ EOTEXT
 
   public function getArguments() {
     return array(
+      'json' => array(
+        'help' => 'Output upload information in JSON format.',
+      ),
       '*' => 'paths',
     );
   }
@@ -47,6 +51,7 @@ EOTEXT
     }
 
     $this->paths = $this->getArgument('paths');
+    $this->json = $this->getArgument('json');
   }
 
   public function requiresAuthentication() {
@@ -57,17 +62,25 @@ EOTEXT
     return $this->paths;
   }
 
+  private function getJSON() {
+    return $this->json;
+  }
+
   public function run() {
 
     $conduit = $this->getConduit();
 
+    $results = array();
+
     foreach ($this->paths as $path) {
       $name = basename($path);
-      echo "Uploading '{$name}'...\n";
+      $this->writeStatusMessage("Uploading '{$name}'...\n");
       try {
         $data = Filesystem::readFile($path);
       } catch (FilesystemException $ex) {
-        echo "Unable to upload file: ".$ex->getMessage()."\n";
+        $this->writeStatusMessage(
+          "Unable to upload file: ".$ex->getMessage()."\n");
+        $results[$path] = null;
         continue;
       }
 
@@ -83,10 +96,18 @@ EOTEXT
           'phid'        => $phid,
         ));
 
-      echo "  {$name}: ".$info['uri']."\n\n";
+      $results[$path] = $info;
+
+      if (!$this->getJSON()) {
+        echo "  {$name}: ".$info['uri']."\n\n";
+      }
     }
 
-    echo "Done.\n";
+    if ($this->getJSON()) {
+      echo json_encode($results)."\n";
+    } else {
+      $this->writeStatusMessage("Done.\n");
+    }
 
     return 0;
   }