44cf8c86744ecaf7b149a1b3039cd3026ed4b69e
[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
31             case 'server':
32                 $cmd = new Command_Server($this->cfg);
33                 $cmd->run($res->command->options['verbose']);
34                 break;
35
36             case 'like':
37                 $this->requireValidHost();
38                 $cmd = new Command_Like($this->cfg);
39                 $cmd->run($res->command->args['url']);
40                 break;
41
42             case 'reply':
43                 $this->requireValidHost();
44                 $cmd = new Command_Reply($this->cfg);
45                 $cmd->run(
46                     $res->command->args['url'],
47                     implode(' ', $res->command->args['text'])
48                 );
49                 break;
50
51             default:
52                 var_dump($this->cfg->host, $res);
53                 Log::err('FIXME');
54             }
55         } catch (\Exception $e) {
56             echo 'Error: ' . $e->getMessage() . "\n";
57             exit(1);
58         }
59     }
60
61     /**
62      * Let the CLI option parser parse the options.
63      *
64      * @param object $parser Option parser
65      *
66      * @return object Parsed command line parameters
67      */
68     protected function parseParameters(\Console_CommandLine $optParser)
69     {
70         try {
71             $res  = $optParser->parse();
72             $opts = $res->options;
73
74             $this->cfg->host = new Config_Host();
75             if ($opts['server'] !== null) {
76                 $key = $this->cfg->getHostByName($opts['server']);
77                 if ($key === null) {
78                     $this->cfg->host->server = $opts['server'];
79                 } else {
80                     $this->cfg->host = $this->cfg->hosts[$key];
81                 }
82             } else {
83                 $key = $this->cfg->getDefaultHost();
84                 if ($key !== null) {
85                     $this->cfg->host = $this->cfg->hosts[$key];
86                 }
87             }
88             if ($opts['user'] !== null) {
89                 $this->cfg->host->user = $opts['user'];
90             }
91             $this->cfg->setDebug($opts['debug']);
92
93             return $res;
94         } catch (\Exception $exc) {
95             $optParser->displayError($exc->getMessage());
96         }
97     }
98
99     /**
100      * Load parameters for the CLI option parser.
101      *
102      * @return \Console_CommandLine CLI option parser
103      */
104     protected function loadOptParser()
105     {
106         $optParser = new \Console_CommandLine();
107         $optParser->description = 'shpub';
108         $optParser->version = '0.0.0';
109         $optParser->subcommand_required = true;
110
111         $optParser->addOption(
112             'server',
113             array(
114                 'short_name'  => '-s',
115                 'long_name'   => '--server',
116                 'description' => 'Server URL',
117                 'help_name'   => 'URL',
118                 'action'      => 'StoreString',
119                 'default'     => null,
120             )
121         );
122         $optParser->addOption(
123             'user',
124             array(
125                 'short_name'  => '-u',
126                 'long_name'   => '--user',
127                 'description' => 'User URL',
128                 'help_name'   => 'URL',
129                 'action'      => 'StoreString',
130                 'default'     => null,
131             )
132         );
133         $optParser->addOption(
134             'debug',
135             array(
136                 'short_name'  => '-d',
137                 'long_name'   => '--debug',
138                 'description' => 'Verbose output',
139                 'action'      => 'StoreTrue',
140                 'default'     => false,
141             )
142         );
143
144         $cmd = $optParser->addCommand('connect');
145         $cmd->addOption(
146             'force',
147             array(
148                 'short_name'  => '-f',
149                 'long_name'   => '--force-update',
150                 'description' => 'Force token update if token already available',
151                 'action'      => 'StoreTrue',
152                 'default'     => false,
153             )
154         );
155         $cmd->addArgument(
156             'server',
157             [
158                 'optional'    => false,
159                 'description' => 'Server URL',
160             ]
161         );
162         $cmd->addArgument(
163             'user',
164             [
165                 'optional'    => true,
166                 'description' => 'User URL',
167             ]
168         );
169         $cmd->addArgument(
170             'key',
171             [
172                 'optional'    => true,
173                 'description' => 'Short name (key)',
174             ]
175         );
176
177         $cmd = $optParser->addCommand('server');
178         $cmd->addOption(
179             'verbose',
180             array(
181                 'short_name'  => '-v',
182                 'long_name'   => '--verbose',
183                 'description' => 'Show more server infos',
184                 'action'      => 'StoreTrue',
185                 'default'     => false,
186             )
187         );
188
189         //$cmd = $optParser->addCommand('post');
190         $cmd = $optParser->addCommand('reply');
191         $cmd->addArgument(
192             'url',
193             [
194                 'optional'    => false,
195                 'description' => 'URL that is replied to',
196             ]
197         );
198         $cmd->addArgument(
199             'text',
200             [
201                 'optional'    => false,
202                 'multiple'    => true,
203                 'description' => 'Reply text',
204             ]
205         );
206
207         $cmd = $optParser->addCommand('like');
208         $cmd->addArgument(
209             'url',
210             [
211                 'optional'    => false,
212                 'description' => 'URL that is liked',
213             ]
214         );
215
216         return $optParser;
217     }
218
219     protected function requireValidHost()
220     {
221         if ($this->cfg->host->server === null
222             || $this->cfg->host->user === null
223             || $this->cfg->host->token === null
224         ) {
225             throw new \Exception(
226                 'Server data incomplete. "shpub connect" first.'
227             );
228         }
229
230         $this->cfg->host->loadEndpoints();
231     }
232 }
233 ?>