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