Add bookmark command
[shpub.git] / src / shpub / Command / Bookmark.php
1 <?php
2 namespace shpub;
3
4 class Command_Bookmark extends Command_AbstractProps
5 {
6     public static function opts(\Console_CommandLine $optParser)
7     {
8         $cmd = $optParser->addCommand('bookmark');
9         static::optsGeneric($cmd);
10         $cmd->addArgument(
11             'url',
12             [
13                 'optional'    => false,
14                 'description' => 'URL to bookmark',
15             ]
16         );
17         $cmd->addArgument(
18             'text',
19             [
20                 'optional'    => true,
21                 'multiple'    => false,
22                 'description' => 'Bookmark text',
23             ]
24         );
25     }
26
27     public function run(\Console_CommandLine_Result $cmdRes)
28     {
29         $url = Validator::url($cmdRes->args['url'], 'url');
30         if ($url === false) {
31             exit(10);
32         }
33
34         $req = new Request($this->cfg->host, $this->cfg);
35         $req->req->addPostParameter('h', 'entry');
36         $req->req->addPostParameter('bookmark-of', $url);
37
38         if ($cmdRes->args['text']) {
39             $req->req->addPostParameter('content', $cmdRes->args['text']);
40         }
41
42
43         $this->handleGenericOptions($cmdRes, $req);
44         $res = $req->send();
45
46         $postUrl = $res->getHeader('Location');
47         if ($postUrl === null) {
48             Log::err('Error: Server sent no "Location" header and said:');
49             Log::err($res->getBody());
50             exit(20);
51         } else {
52             Log::info('Bookmark created at server');
53             Log::msg($postUrl);
54         }
55     }
56 }
57 ?>