Add detail mode to "server" command.
[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
82             return $res;
83         } catch (\Exception $exc) {
84             $optParser->displayError($exc->getMessage());
85         }
86     }
87
88     /**
89      * Load parameters for the CLI option parser.
90      *
91      * @return \Console_CommandLine CLI option parser
92      */
93     protected function loadOptParser()
94     {
95         $optParser = new \Console_CommandLine();
96         $optParser->name        = 'shpub';
97         $optParser->description = 'Command line micropub client';
98         $optParser->version     = '0.3.0';
99         $optParser->subcommand_required = true;
100
101         $optParser->addOption(
102             'server',
103             array(
104                 'short_name'  => '-s',
105                 'long_name'   => '--server',
106                 'description' => 'Server URL',
107                 'help_name'   => 'URL',
108                 'action'      => 'StoreString',
109                 'default'     => null,
110             )
111         );
112         $optParser->addOption(
113             'debug',
114             array(
115                 'short_name'  => '-d',
116                 'long_name'   => '--debug',
117                 'description' => 'Verbose output',
118                 'action'      => 'StoreTrue',
119                 'default'     => false,
120             )
121         );
122
123         Command_Connect::opts($optParser);
124         Command_Server::opts($optParser);
125         Command_Targets::opts($optParser);
126
127         Command_Article::opts($optParser);
128         Command_Note::opts($optParser);
129         Command_Reply::opts($optParser);
130         Command_Like::opts($optParser);
131         Command_Repost::opts($optParser);
132         Command_Rsvp::opts($optParser);
133         Command_Bookmark::opts($optParser);
134
135         Command_Delete::opts($optParser);
136         Command_Undelete::opts($optParser);
137         Command_Update::opts($optParser);
138
139         Command_Upload::opts($optParser);
140
141         return $optParser;
142     }
143
144     protected function requireValidHost()
145     {
146         if ($this->cfg->host->server === null
147             || $this->cfg->host->user === null
148             || $this->cfg->host->token === null
149         ) {
150             throw new \Exception(
151                 'Server data incomplete. "shpub connect" first.'
152             );
153         }
154
155         $this->cfg->host->loadEndpoints();
156     }
157 }
158 ?>