From 4e67ee4a6c7cc62f9c12f091660aae6b1e87d53b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 8 Sep 2016 17:04:16 +0200 Subject: [PATCH 1/1] move option definition into command classes --- src/shpub/Cli.php | 49 ++++++------------------------------- src/shpub/Command/Like.php | 16 ++++++++++-- src/shpub/Command/Note.php | 45 ++++++++++++++++++++++++++++++++++ src/shpub/Command/Reply.php | 19 ++++++++++++++ 4 files changed, 85 insertions(+), 44 deletions(-) create mode 100644 src/shpub/Command/Note.php diff --git a/src/shpub/Cli.php b/src/shpub/Cli.php index f807d32..8ec4acb 100644 --- a/src/shpub/Cli.php +++ b/src/shpub/Cli.php @@ -33,24 +33,12 @@ class Cli $cmd->run($res->command->options['verbose']); break; - case 'like': - $this->requireValidHost(); - $cmd = new Command_Like($this->cfg); - $cmd->run($res->command->args['url']); - break; - - case 'reply': + default: + $class = 'shpub\\Command_' . ucfirst($res->command_name); $this->requireValidHost(); - $cmd = new Command_Reply($this->cfg); - $cmd->run( - $res->command->args['url'], - implode(' ', $res->command->args['text']) - ); + $cmd = new $class($this->cfg); + $cmd->run($res->command); break; - - default: - var_dump($this->cfg->host, $res); - Log::err('FIXME'); } } catch (\Exception $e) { echo 'Error: ' . $e->getMessage() . "\n"; @@ -186,32 +174,9 @@ class Cli ) ); - //$cmd = $optParser->addCommand('post'); - $cmd = $optParser->addCommand('reply'); - $cmd->addArgument( - 'url', - [ - 'optional' => false, - 'description' => 'URL that is replied to', - ] - ); - $cmd->addArgument( - 'text', - [ - 'optional' => false, - 'multiple' => true, - 'description' => 'Reply text', - ] - ); - - $cmd = $optParser->addCommand('like'); - $cmd->addArgument( - 'url', - [ - 'optional' => false, - 'description' => 'URL that is liked', - ] - ); + Command_Note::opts($optParser); + Command_Reply::opts($optParser); + Command_Like::opts($optParser); return $optParser; } diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php index 26f615a..e7c6052 100644 --- a/src/shpub/Command/Like.php +++ b/src/shpub/Command/Like.php @@ -13,9 +13,21 @@ class Command_Like $this->cfg = $cfg; } - public function run($url) + public static function opts(\Console_CommandLine $optParser) { - $url = Validator::url($url, 'url'); + $cmd = $optParser->addCommand('like'); + $cmd->addArgument( + 'url', + [ + 'optional' => false, + 'description' => 'URL that is liked', + ] + ); + } + + public function run($command) + { + $url = Validator::url($command->args['url'], 'url'); if ($url === false) { exit(10); } diff --git a/src/shpub/Command/Note.php b/src/shpub/Command/Note.php new file mode 100644 index 0000000..a60422a --- /dev/null +++ b/src/shpub/Command/Note.php @@ -0,0 +1,45 @@ +cfg = $cfg; + } + + public static function opts(\Console_CommandLine $optParser) + { + $cmd = $optParser->addCommand('note'); + $cmd->addArgument( + 'text', + [ + 'optional' => false, + 'multiple' => false, + 'description' => 'Post text', + ] + ); + } + + public function run($command) + { + $data = [ + 'h' => 'entry', + 'content' => $command->args['text'], + ]; + + $body = http_build_query($data); + + $req = new Request($this->cfg->host, $this->cfg); + $res = $req->send($body); + $postUrl = $res->getHeader('Location'); + echo "Post created at server\n"; + echo $postUrl . "\n"; + } +} +?> diff --git a/src/shpub/Command/Reply.php b/src/shpub/Command/Reply.php index 327111d..622361a 100644 --- a/src/shpub/Command/Reply.php +++ b/src/shpub/Command/Reply.php @@ -13,6 +13,25 @@ class Command_Reply $this->cfg = $cfg; } + public static function opts(\Console_CommandLine $optParser) + { + $cmd = $optParser->addCommand('reply'); + $cmd->addArgument( + 'url', + [ + 'optional' => false, + 'description' => 'URL that is replied to', + ] + ); + $cmd->addArgument( + 'text', + [ + 'optional' => false, + 'multiple' => true, + 'description' => 'Reply text', + ] + ); + } public function run($url, $text) { $url = Validator::url($url, 'url'); -- 2.30.2