8ec4acbfea6a0f1431fa5be6fa95283ca0994985
[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             default:
37                 $class = 'shpub\\Command_' . ucfirst($res->command_name);
38                 $this->requireValidHost();
39                 $cmd = new $class($this->cfg);
40                 $cmd->run($res->command);
41                 break;
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             } else {
71                 $key = $this->cfg->getDefaultHost();
72                 if ($key !== null) {
73                     $this->cfg->host = $this->cfg->hosts[$key];
74                 }
75             }
76             if ($opts['user'] !== null) {
77                 $this->cfg->host->user = $opts['user'];
78             }
79             $this->cfg->setDebug($opts['debug']);
80
81             return $res;
82         } catch (\Exception $exc) {
83             $optParser->displayError($exc->getMessage());
84         }
85     }
86
87     /**
88      * Load parameters for the CLI option parser.
89      *
90      * @return \Console_CommandLine CLI option parser
91      */
92     protected function loadOptParser()
93     {
94         $optParser = new \Console_CommandLine();
95         $optParser->description = 'shpub';
96         $optParser->version = '0.0.2';
97         $optParser->subcommand_required = true;
98
99         $optParser->addOption(
100             'server',
101             array(
102                 'short_name'  => '-s',
103                 'long_name'   => '--server',
104                 'description' => 'Server URL',
105                 'help_name'   => 'URL',
106                 'action'      => 'StoreString',
107                 'default'     => null,
108             )
109         );
110         $optParser->addOption(
111             'user',
112             array(
113                 'short_name'  => '-u',
114                 'long_name'   => '--user',
115                 'description' => 'User URL',
116                 'help_name'   => 'URL',
117                 'action'      => 'StoreString',
118                 'default'     => null,
119             )
120         );
121         $optParser->addOption(
122             'debug',
123             array(
124                 'short_name'  => '-d',
125                 'long_name'   => '--debug',
126                 'description' => 'Verbose output',
127                 'action'      => 'StoreTrue',
128                 'default'     => false,
129             )
130         );
131
132         $cmd = $optParser->addCommand('connect');
133         $cmd->addOption(
134             'force',
135             array(
136                 'short_name'  => '-f',
137                 'long_name'   => '--force-update',
138                 'description' => 'Force token update if token already available',
139                 'action'      => 'StoreTrue',
140                 'default'     => false,
141             )
142         );
143         $cmd->addArgument(
144             'server',
145             [
146                 'optional'    => false,
147                 'description' => 'Server URL',
148             ]
149         );
150         $cmd->addArgument(
151             'user',
152             [
153                 'optional'    => true,
154                 'description' => 'User URL',
155             ]
156         );
157         $cmd->addArgument(
158             'key',
159             [
160                 'optional'    => true,
161                 'description' => 'Short name (key)',
162             ]
163         );
164
165         $cmd = $optParser->addCommand('server');
166         $cmd->addOption(
167             'verbose',
168             array(
169                 'short_name'  => '-v',
170                 'long_name'   => '--verbose',
171                 'description' => 'Show more server infos',
172                 'action'      => 'StoreTrue',
173                 'default'     => false,
174             )
175         );
176
177         Command_Note::opts($optParser);
178         Command_Reply::opts($optParser);
179         Command_Like::opts($optParser);
180
181         return $optParser;
182     }
183
184     protected function requireValidHost()
185     {
186         if ($this->cfg->host->server === null
187             || $this->cfg->host->user === null
188             || $this->cfg->host->token === null
189         ) {
190             throw new \Exception(
191                 'Server data incomplete. "shpub connect" first.'
192             );
193         }
194
195         $this->cfg->host->loadEndpoints();
196     }
197 }
198 ?>