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 113c149b8829a45a7e8a146bbda23923ea12b714..fcc6070b436991de008a3592a6994f4cc1a3a582 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 2ca7d9adeea3c376196ebe0972fd313d53879ba0..f3ca5433d13a94904a6aeeae0148bafb28b9baf0 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 dce163f42a2c51f470cb9fe13000b262f4cec9fe..4437164af30f2cfda0836ebd7b174d98f9a54776 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) {