56898511e8ac8405013a6abbad53e77f82acffd9
[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             Log::err('Error: ' . $e->getMessage());
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.1.0';
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         Command_Connect::opts($optParser);
119         Command_Server::opts($optParser);
120
121         Command_Article::opts($optParser);
122         Command_Note::opts($optParser);
123         Command_Reply::opts($optParser);
124         Command_Like::opts($optParser);
125         Command_Repost::opts($optParser);
126         Command_Rsvp::opts($optParser);
127         Command_Bookmark::opts($optParser);
128
129         Command_Delete::opts($optParser);
130         Command_Undelete::opts($optParser);
131         Command_Update::opts($optParser);
132
133         Command_Upload::opts($optParser);
134
135         return $optParser;
136     }
137
138     protected function requireValidHost()
139     {
140         if ($this->cfg->host->server === null
141             || $this->cfg->host->user === null
142             || $this->cfg->host->token === null
143         ) {
144             throw new \Exception(
145                 'Server data incomplete. "shpub connect" first.'
146             );
147         }
148
149         $this->cfg->host->loadEndpoints();
150     }
151 }
152 ?>