a57b287fda084518f5f47f2066105507be58a45b
[shpub.git] / src / shpub / Cli.php
1 <?php
2 namespace shpub;
3
4 class Cli
5 {
6     /**
7      * @var Config
8      */
9     protected $cfg;
10
11     public function run()
12     {
13         $this->cfg = new Config();
14         $this->cfg->load();
15
16         try {
17             $optParser = $this->loadOptParser();
18             $res = $this->parseParameters($optParser);
19
20             switch ($res->command_name) {
21             case 'connect':
22                 $cmd = new Command_Connect($this->cfg);
23                 $cmd->run(
24                     $res->command->args['server'],
25                     $res->command->args['user'],
26                     $res->command->args['key'],
27                     $res->command->options['force']
28                 );
29                 break;
30             case 'like':
31                 $this->requireValidHost();
32                 $cmd = new Command_Like($this->cfg->host);
33                 $cmd->run($res->command->args['url']);
34                 break;
35             case 'server':
36                 $cmd = new Command_Server($this->cfg);
37                 $cmd->run($res->command->options['verbose']);
38                 break;
39             default:
40                 var_dump($this->cfg->host, $res);
41                 Log::err('FIXME');
42             }
43         } catch (\Exception $e) {
44             echo 'Error: ' . $e->getMessage() . "\n";
45             exit(1);
46         }
47     }
48
49     /**
50      * Let the CLI option parser parse the options.
51      *
52      * @param object $parser Option parser
53      *
54      * @return object Parsed command line parameters
55      */
56     protected function parseParameters(\Console_CommandLine $optParser)
57     {
58         try {
59             $res  = $optParser->parse();
60             $opts = $res->options;
61
62             $this->cfg->host = new Config_Host();
63             if ($opts['server'] !== null) {
64                 $key = $this->cfg->getHostByName($opts['server']);
65                 if ($key === null) {
66                     $this->cfg->host->server = $opts['server'];
67                 } else {
68                     $this->cfg->host = $this->cfg->hosts[$key];
69                 }
70             }
71             if ($opts['user'] !== null) {
72                 $this->cfg->host->user = $opts['user'];
73             }
74
75             return $res;
76         } catch (\Exception $exc) {
77             $optParser->displayError($exc->getMessage());
78         }
79     }
80
81     /**
82      * Load parameters for the CLI option parser.
83      *
84      * @return \Console_CommandLine CLI option parser
85      */
86     protected function loadOptParser()
87     {
88         $optParser = new \Console_CommandLine();
89         $optParser->description = 'shpub';
90         $optParser->version = '0.0.0';
91         $optParser->subcommand_required = true;
92
93         $optParser->addOption(
94             'server',
95             array(
96                 'short_name'  => '-s',
97                 'long_name'   => '--server',
98                 'description' => 'Server URL',
99                 'help_name'   => 'URL',
100                 'action'      => 'StoreString',
101                 'default'     => null,
102             )
103         );
104         $optParser->addOption(
105             'user',
106             array(
107                 'short_name'  => '-u',
108                 'long_name'   => '--user',
109                 'description' => 'User URL',
110                 'help_name'   => 'URL',
111                 'action'      => 'StoreString',
112                 'default'     => null,
113             )
114         );
115
116         $cmd = $optParser->addCommand('connect');
117         $cmd->addOption(
118             'force',
119             array(
120                 'short_name'  => '-f',
121                 'long_name'   => '--force-update',
122                 'description' => 'Force token update if token already available',
123                 'action'      => 'StoreTrue',
124                 'default'     => false,
125             )
126         );
127         $cmd->addArgument(
128             'server',
129             [
130                 'optional'    => false,
131                 'description' => 'Server URL',
132             ]
133         );
134         $cmd->addArgument(
135             'user',
136             [
137                 'optional'    => false,
138                 'description' => 'User URL',
139             ]
140         );
141         $cmd->addArgument(
142             'key',
143             [
144                 'optional'    => true,
145                 'description' => 'Short name (key)',
146             ]
147         );
148
149         $cmd = $optParser->addCommand('server');
150         $cmd->addOption(
151             'verbose',
152             array(
153                 'short_name'  => '-v',
154                 'long_name'   => '--verbose',
155                 'description' => 'Show more server infos',
156                 'action'      => 'StoreTrue',
157                 'default'     => false,
158             )
159         );
160
161         //$cmd = $optParser->addCommand('post');
162         $cmd = $optParser->addCommand('reply');
163         $cmd->addArgument(
164             'url',
165             [
166                 'optional'    => false,
167                 'description' => 'URL that is replied to',
168             ]
169         );
170         $cmd->addArgument(
171             'text',
172             [
173                 'optional'    => false,
174                 'description' => 'Reply text',
175             ]
176         );
177
178         $cmd = $optParser->addCommand('like');
179         $cmd->addArgument(
180             'url',
181             [
182                 'optional'    => false,
183                 'description' => 'URL that is liked',
184             ]
185         );
186
187         return $optParser;
188     }
189
190     protected function requireValidHost()
191     {
192         if ($this->cfg->host->server === null
193             || $this->cfg->host->user === null
194             || $this->cfg->host->token === null
195         ) {
196             throw new \Exception(
197                 'Server data incomplete. "shpub connect" first.'
198             );
199         }
200
201         $this->cfg->host->loadEndpoints();
202     }
203 }
204 ?>