Add bookmark command
authorChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 15:03:31 +0000 (17:03 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 15:03:31 +0000 (17:03 +0200)
src/shpub/Cli.php
src/shpub/Command/Bookmark.php [new file with mode: 0644]

index 77b62cc..697d934 100644 (file)
@@ -166,6 +166,7 @@ class Cli
         Command_Like::opts($optParser);
         Command_Repost::opts($optParser);
         Command_Rsvp::opts($optParser);
         Command_Like::opts($optParser);
         Command_Repost::opts($optParser);
         Command_Rsvp::opts($optParser);
+        Command_Bookmark::opts($optParser);
 
         Command_Delete::opts($optParser);
         Command_Undelete::opts($optParser);
 
         Command_Delete::opts($optParser);
         Command_Undelete::opts($optParser);
diff --git a/src/shpub/Command/Bookmark.php b/src/shpub/Command/Bookmark.php
new file mode 100644 (file)
index 0000000..e25365c
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+namespace shpub;
+
+class Command_Bookmark extends Command_AbstractProps
+{
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('bookmark');
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL to bookmark',
+            ]
+        );
+        $cmd->addArgument(
+            'text',
+            [
+                'optional'    => true,
+                'multiple'    => false,
+                'description' => 'Bookmark text',
+            ]
+        );
+    }
+
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $url = Validator::url($cmdRes->args['url'], 'url');
+        if ($url === false) {
+            exit(10);
+        }
+
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('bookmark-of', $url);
+
+        if ($cmdRes->args['text']) {
+            $req->req->addPostParameter('content', $cmdRes->args['text']);
+        }
+
+
+        $this->handleGenericOptions($cmdRes, $req);
+        $res = $req->send();
+
+        $postUrl = $res->getHeader('Location');
+        if ($postUrl === null) {
+            Log::err('Error: Server sent no "Location" header and said:');
+            Log::err($res->getBody());
+            exit(20);
+        } else {
+            Log::info('Bookmark created at server');
+            Log::msg($postUrl);
+        }
+    }
+}
+?>