Add detail mode to "server" command.
authorChristian Weiske <cweiske@cweiske.de>
Mon, 28 Aug 2017 15:08:08 +0000 (17:08 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 28 Aug 2017 15:08:45 +0000 (17:08 +0200)
Resolves: https://github.com/cweiske/shpub/issues/8

src/shpub/Cli.php
src/shpub/Command/Server.php

index 733f4df..6cc77dc 100644 (file)
@@ -31,7 +31,10 @@ class Cli
 
             case 'server':
                 $cmd = new Command_Server($this->cfg);
-                $cmd->run($res->command->options['verbose']);
+                $cmd->run(
+                    $res->command->args['server'],
+                    $res->command->options['verbose']
+                );
                 break;
 
             default:
index 34d939e..b0200f2 100644 (file)
@@ -1,6 +1,13 @@
 <?php
 namespace shpub;
 
+/**
+ * Inspect the list of saved connections/servers.
+ *
+ * @author  Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link    http://cweiske.de/shpub.htm
+ */
 class Command_Server
 {
     public function __construct(Config $cfg)
@@ -22,17 +29,71 @@ class Command_Server
                 'default'     => false,
             )
         );
+        $cmd->addArgument(
+            'server',
+            [
+                'default'     => null,
+                'optional'    => true,
+                'description' => 'Connection name',
+            ]
+        );
     }
 
-    public function run($verbose)
+    public function run($server, $verbose)
+    {
+        if ($server === null) {
+            $this->showConnections($verbose);
+        } else {
+            $this->showConnectionDetails($server, $verbose);
+        }
+    }
+
+    /**
+     * Show a list of all connections
+     *
+     * @param bool $verbose Show some details
+     *
+     * @return void
+     */
+    protected function showConnections($verbose)
     {
         foreach ($this->cfg->hosts as $key => $host) {
             Log::msg($key);
             if ($verbose) {
-                Log::msg('  URL:  ' . $host->server);
-                Log::msg('  User: ' . $host->user);
+                Log::msg(' URL:  ' . $host->server);
+                Log::msg(' User: ' . $host->user);
             }
         }
     }
+
+    /**
+     * Show detailled information for single connection
+     *
+     * @param string $server  Connection name
+     * @param bool   $verbose Show the token
+     *
+     * @return void
+     */
+    protected function showConnectionDetails($server, $verbose)
+    {
+        if (!isset($this->cfg->hosts[$server])) {
+            Log::err('Connection does not exist: ' . $server);
+            exit(1);
+        }
+
+        $host = $this->cfg->hosts[$server];
+        Log::msg($server);
+        Log::msg(' URL:   ' . $host->server);
+        Log::msg(' User:  ' . $host->user);
+        if ($verbose) {
+            Log::msg(' Token: ' . $host->token);
+        }
+
+        Log::msg(' Endpoints:');
+        $host->loadEndpoints();
+        foreach ($host->endpoints as $key => $value) {
+            Log::msg('  ' . str_pad($key . ': ', 15, ' ') . $value);
+        }
+    }
 }
 ?>