Add detail mode to "server" command.
[shpub.git] / src / shpub / Command / Server.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Inspect the list of saved connections/servers.
6  *
7  * @author  Christian Weiske <cweiske@cweiske.de>
8  * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
9  * @link    http://cweiske.de/shpub.htm
10  */
11 class Command_Server
12 {
13     public function __construct(Config $cfg)
14     {
15         $this->cfg = $cfg;
16     }
17
18     public static function opts(\Console_CommandLine $optParser)
19     {
20         $cmd = $optParser->addCommand('server');
21         $cmd->description = 'List all connections';
22         $cmd->addOption(
23             'verbose',
24             array(
25                 'short_name'  => '-v',
26                 'long_name'   => '--verbose',
27                 'description' => 'Show more server infos',
28                 'action'      => 'StoreTrue',
29                 'default'     => false,
30             )
31         );
32         $cmd->addArgument(
33             'server',
34             [
35                 'default'     => null,
36                 'optional'    => true,
37                 'description' => 'Connection name',
38             ]
39         );
40     }
41
42     public function run($server, $verbose)
43     {
44         if ($server === null) {
45             $this->showConnections($verbose);
46         } else {
47             $this->showConnectionDetails($server, $verbose);
48         }
49     }
50
51     /**
52      * Show a list of all connections
53      *
54      * @param bool $verbose Show some details
55      *
56      * @return void
57      */
58     protected function showConnections($verbose)
59     {
60         foreach ($this->cfg->hosts as $key => $host) {
61             Log::msg($key);
62             if ($verbose) {
63                 Log::msg(' URL:  ' . $host->server);
64                 Log::msg(' User: ' . $host->user);
65             }
66         }
67     }
68
69     /**
70      * Show detailled information for single connection
71      *
72      * @param string $server  Connection name
73      * @param bool   $verbose Show the token
74      *
75      * @return void
76      */
77     protected function showConnectionDetails($server, $verbose)
78     {
79         if (!isset($this->cfg->hosts[$server])) {
80             Log::err('Connection does not exist: ' . $server);
81             exit(1);
82         }
83
84         $host = $this->cfg->hosts[$server];
85         Log::msg($server);
86         Log::msg(' URL:   ' . $host->server);
87         Log::msg(' User:  ' . $host->user);
88         if ($verbose) {
89             Log::msg(' Token: ' . $host->token);
90         }
91
92         Log::msg(' Endpoints:');
93         $host->loadEndpoints();
94         foreach ($host->endpoints as $key => $value) {
95             Log::msg('  ' . str_pad($key . ': ', 15, ' ') . $value);
96         }
97     }
98 }
99 ?>