Fix CS
[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                     $res->command->options['scope']
29                 );
30                 break;
31
32             case 'server':
33                 $cmd = new Command_Server($this->cfg);
34                 $cmd->run($res->command->options['verbose']);
35                 break;
36
37             default:
38                 $class = 'shpub\\Command_' . ucfirst($res->command_name);
39                 $this->requireValidHost();
40                 $cmd = new $class($this->cfg);
41                 $cmd->run($res->command);
42                 break;
43             }
44         } catch (\Exception $e) {
45             Log::err('Error: ' . $e->getMessage());
46             exit(1);
47         }
48     }
49
50     /**
51      * Let the CLI option parser parse the options.
52      *
53      * @param object $optParser Option parser
54      *
55      * @return object Parsed command line parameters
56      */
57     protected function parseParameters(\Console_CommandLine $optParser)
58     {
59         try {
60             $res  = $optParser->parse();
61             $opts = $res->options;
62
63             $this->cfg->host = new Config_Host();
64             if ($opts['server'] !== null) {
65                 $key = $this->cfg->getHostByName($opts['server']);
66                 if ($key === null) {
67                     $this->cfg->host->server = $opts['server'];
68                 } else {
69                     $this->cfg->host = $this->cfg->hosts[$key];
70                 }
71             } else {
72                 $key = $this->cfg->getDefaultHost();
73                 if ($key !== null) {
74                     $this->cfg->host = $this->cfg->hosts[$key];
75                 }
76             }
77             $this->cfg->setDebug($opts['debug']);
78
79             return $res;
80         } catch (\Exception $exc) {
81             $optParser->displayError($exc->getMessage());
82         }
83     }
84
85     /**
86      * Load parameters for the CLI option parser.
87      *
88      * @return \Console_CommandLine CLI option parser
89      */
90     protected function loadOptParser()
91     {
92         $optParser = new \Console_CommandLine();
93         $optParser->name        = 'shpub';
94         $optParser->description = 'Command line micropub client';
95         $optParser->version     = '0.3.0';
96         $optParser->subcommand_required = true;
97
98         $optParser->addOption(
99             'server',
100             array(
101                 'short_name'  => '-s',
102                 'long_name'   => '--server',
103                 'description' => 'Server URL',
104                 'help_name'   => 'URL',
105                 'action'      => 'StoreString',
106                 'default'     => null,
107             )
108         );
109         $optParser->addOption(
110             'debug',
111             array(
112                 'short_name'  => '-d',
113                 'long_name'   => '--debug',
114                 'description' => 'Verbose output',
115                 'action'      => 'StoreTrue',
116                 'default'     => false,
117             )
118         );
119
120         Command_Connect::opts($optParser);
121         Command_Server::opts($optParser);
122
123         Command_Article::opts($optParser);
124         Command_Note::opts($optParser);
125         Command_Reply::opts($optParser);
126         Command_Like::opts($optParser);
127         Command_Repost::opts($optParser);
128         Command_Rsvp::opts($optParser);
129         Command_Bookmark::opts($optParser);
130
131         Command_Delete::opts($optParser);
132         Command_Undelete::opts($optParser);
133         Command_Update::opts($optParser);
134
135         Command_Upload::opts($optParser);
136
137         return $optParser;
138     }
139
140     protected function requireValidHost()
141     {
142         if ($this->cfg->host->server === null
143             || $this->cfg->host->user === null
144             || $this->cfg->host->token === null
145         ) {
146             throw new \Exception(
147                 'Server data incomplete. "shpub connect" first.'
148             );
149         }
150
151         $this->cfg->host->loadEndpoints();
152     }
153 }
154 ?>