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