Add detail mode to "server" command.
[shpub.git] / src / shpub / Command / Server.php
index a3c52eed2bf34cdd6841d22806c44144510cb6f7..b0200f22ce36611371abdaa0587914eb8d1d1540 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)
@@ -11,6 +18,7 @@ class Command_Server
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('server');
+        $cmd->description = 'List all connections';
         $cmd->addOption(
             'verbose',
             array(
@@ -21,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);
+        }
+    }
 }
 ?>