e071252eb6020b75d70034727d3804fd3cbad5f1
[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->name        = 'shpub';
93         $optParser->description = 'Command line micropub client';
94         $optParser->version     = '0.1.0';
95         $optParser->subcommand_required = true;
96
97         $optParser->addOption(
98             'server',
99             array(
100                 'short_name'  => '-s',
101                 'long_name'   => '--server',
102                 'description' => 'Server URL',
103                 'help_name'   => 'URL',
104                 'action'      => 'StoreString',
105                 'default'     => null,
106             )
107         );
108         $optParser->addOption(
109             'debug',
110             array(
111                 'short_name'  => '-d',
112                 'long_name'   => '--debug',
113                 'description' => 'Verbose output',
114                 'action'      => 'StoreTrue',
115                 'default'     => false,
116             )
117         );
118
119         Command_Connect::opts($optParser);
120         Command_Server::opts($optParser);
121
122         Command_Article::opts($optParser);
123         Command_Note::opts($optParser);
124         Command_Reply::opts($optParser);
125         Command_Like::opts($optParser);
126         Command_Repost::opts($optParser);
127         Command_Rsvp::opts($optParser);
128         Command_Bookmark::opts($optParser);
129
130         Command_Delete::opts($optParser);
131         Command_Undelete::opts($optParser);
132         Command_Update::opts($optParser);
133
134         Command_Upload::opts($optParser);
135
136         return $optParser;
137     }
138
139     protected function requireValidHost()
140     {
141         if ($this->cfg->host->server === null
142             || $this->cfg->host->user === null
143             || $this->cfg->host->token === null
144         ) {
145             throw new \Exception(
146                 'Server data incomplete. "shpub connect" first.'
147             );
148         }
149
150         $this->cfg->host->loadEndpoints();
151     }
152 }
153 ?>