Add --dry-run option for getting curl commands
authorChristian Weiske <cweiske@cweiske.de>
Wed, 31 Jan 2018 10:17:42 +0000 (11:17 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 31 Jan 2018 10:17:42 +0000 (11:17 +0100)
src/shpub/Cli.php
src/shpub/Config.php
src/shpub/Request.php

index 113c149..fcc6070 100644 (file)
@@ -78,6 +78,7 @@ class Cli
                 }
             }
             $this->cfg->setDebug($opts['debug']);
+            $this->cfg->setDryRun($opts['dryrun']);
 
             return $res;
         } catch (\Exception $exc) {
@@ -119,6 +120,16 @@ class Cli
                 'default'     => false,
             )
         );
+        $optParser->addOption(
+            'dryrun',
+            array(
+                'short_name'  => '-n',
+                'long_name'   => '--dry-run',
+                'description' => 'Do not send modifying HTTP request(s) to the server',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
 
         Command_Connect::opts($optParser);
         Command_Server::opts($optParser);
index 2ca7d9a..f3ca543 100644 (file)
@@ -14,6 +14,8 @@ class Config
 
     public $debug = false;
 
+    public $dryRun = false;
+
     protected function getConfigFilePath()
     {
         if (!isset($_SERVER['HOME'])) {
@@ -91,7 +93,7 @@ class Config
                 return $key;
             }
         }
-        
+
         reset($this->hosts);
         return key($this->hosts);
     }
@@ -121,5 +123,10 @@ class Config
     {
         $this->debug = $debug;
     }
+
+    public function setDryRun($dryRun)
+    {
+        $this->dryRun = $dryRun;
+    }
 }
 ?>
index dce163f..4437164 100644 (file)
@@ -93,6 +93,16 @@ class Request
             $cp = new CurlPrinter();
             $cp->show($this->req, $this->uploadsInfo, $this->dedicatedBody);
         }
+
+        if ($this->cfg->dryRun && $this->req->getMethod() != 'GET') {
+            //do not run any modifying queries
+            //fake a successful response
+            $res = new \HTTP_Request2_Response('HTTP/1.1 200 OK', false);
+            $res->parseHeaderLine('Content-type: text/plain');
+            $res->appendBody('Fake --dry-run response');
+            return $res;
+        }
+
         $res = $this->req->send();
 
         if (intval($res->getStatus() / 100) != 2) {