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