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