Explain that "server" command takes a parameter.
[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             . "\nPass the connection name to see all details:"
23             . " URL, user, endpoint URLs";
24         $cmd->addOption(
25             'verbose',
26             array(
27                 'short_name'  => '-v',
28                 'long_name'   => '--verbose',
29                 'description' => 'Show more server infos',
30                 'action'      => 'StoreTrue',
31                 'default'     => false,
32             )
33         );
34         $cmd->addArgument(
35             'server',
36             [
37                 'default'     => null,
38                 'optional'    => true,
39                 'description' => 'Connection name',
40             ]
41         );
42     }
43
44     public function run($server, $verbose)
45     {
46         if ($server === null) {
47             $this->showConnections($verbose);
48         } else {
49             $this->showConnectionDetails($server, $verbose);
50         }
51     }
52
53     /**
54      * Show a list of all connections
55      *
56      * @param bool $verbose Show some details
57      *
58      * @return void
59      */
60     protected function showConnections($verbose)
61     {
62         foreach ($this->cfg->hosts as $key => $host) {
63             Log::msg($key);
64             if ($verbose) {
65                 Log::msg(' URL:  ' . $host->server);
66                 Log::msg(' User: ' . $host->user);
67             }
68         }
69     }
70
71     /**
72      * Show detailled information for single connection
73      *
74      * @param string $server  Connection name
75      * @param bool   $verbose Show the token
76      *
77      * @return void
78      */
79     protected function showConnectionDetails($server, $verbose)
80     {
81         if (!isset($this->cfg->hosts[$server])) {
82             Log::err('Connection does not exist: ' . $server);
83             exit(1);
84         }
85
86         $host = $this->cfg->hosts[$server];
87         Log::msg($server);
88         Log::msg(' URL:   ' . $host->server);
89         Log::msg(' User:  ' . $host->user);
90         if ($verbose) {
91             Log::msg(' Token: ' . $host->token);
92         }
93
94         Log::msg(' Endpoints:');
95         $host->loadEndpoints();
96         foreach ($host->endpoints as $key => $value) {
97             Log::msg('  ' . str_pad($key . ': ', 15, ' ') . $value);
98         }
99     }
100 }
101 ?>