debug option to print curl requests
[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             }
83             if ($opts['user'] !== null) {
84                 $this->cfg->host->user = $opts['user'];
85             }
86             $this->cfg->setDebug($opts['debug']);
87
88             return $res;
89         } catch (\Exception $exc) {
90             $optParser->displayError($exc->getMessage());
91         }
92     }
93
94     /**
95      * Load parameters for the CLI option parser.
96      *
97      * @return \Console_CommandLine CLI option parser
98      */
99     protected function loadOptParser()
100     {
101         $optParser = new \Console_CommandLine();
102         $optParser->description = 'shpub';
103         $optParser->version = '0.0.0';
104         $optParser->subcommand_required = true;
105
106         $optParser->addOption(
107             'server',
108             array(
109                 'short_name'  => '-s',
110                 'long_name'   => '--server',
111                 'description' => 'Server URL',
112                 'help_name'   => 'URL',
113                 'action'      => 'StoreString',
114                 'default'     => null,
115             )
116         );
117         $optParser->addOption(
118             'user',
119             array(
120                 'short_name'  => '-u',
121                 'long_name'   => '--user',
122                 'description' => 'User URL',
123                 'help_name'   => 'URL',
124                 'action'      => 'StoreString',
125                 'default'     => null,
126             )
127         );
128         $optParser->addOption(
129             'debug',
130             array(
131                 'short_name'  => '-d',
132                 'long_name'   => '--debug',
133                 'description' => 'Verbose output',
134                 'action'      => 'StoreTrue',
135                 'default'     => false,
136             )
137         );
138
139         $cmd = $optParser->addCommand('connect');
140         $cmd->addOption(
141             'force',
142             array(
143                 'short_name'  => '-f',
144                 'long_name'   => '--force-update',
145                 'description' => 'Force token update if token already available',
146                 'action'      => 'StoreTrue',
147                 'default'     => false,
148             )
149         );
150         $cmd->addArgument(
151             'server',
152             [
153                 'optional'    => false,
154                 'description' => 'Server URL',
155             ]
156         );
157         $cmd->addArgument(
158             'user',
159             [
160                 'optional'    => true,
161                 'description' => 'User URL',
162             ]
163         );
164         $cmd->addArgument(
165             'key',
166             [
167                 'optional'    => true,
168                 'description' => 'Short name (key)',
169             ]
170         );
171
172         $cmd = $optParser->addCommand('server');
173         $cmd->addOption(
174             'verbose',
175             array(
176                 'short_name'  => '-v',
177                 'long_name'   => '--verbose',
178                 'description' => 'Show more server infos',
179                 'action'      => 'StoreTrue',
180                 'default'     => false,
181             )
182         );
183
184         //$cmd = $optParser->addCommand('post');
185         $cmd = $optParser->addCommand('reply');
186         $cmd->addArgument(
187             'url',
188             [
189                 'optional'    => false,
190                 'description' => 'URL that is replied to',
191             ]
192         );
193         $cmd->addArgument(
194             'text',
195             [
196                 'optional'    => false,
197                 'multiple'    => true,
198                 'description' => 'Reply text',
199             ]
200         );
201
202         $cmd = $optParser->addCommand('like');
203         $cmd->addArgument(
204             'url',
205             [
206                 'optional'    => false,
207                 'description' => 'URL that is liked',
208             ]
209         );
210
211         return $optParser;
212     }
213
214     protected function requireValidHost()
215     {
216         if ($this->cfg->host->server === null
217             || $this->cfg->host->user === null
218             || $this->cfg->host->token === null
219         ) {
220             throw new \Exception(
221                 'Server data incomplete. "shpub connect" first.'
222             );
223         }
224
225         $this->cfg->host->loadEndpoints();
226     }
227 }
228 ?>