From cfae9d7894891947492a4a9c61f8ee933d54c6b4 Mon Sep 17 00:00:00 2001 From: John Lamp Date: Fri, 12 Apr 2013 21:21:33 -0400 Subject: [PATCH] Added the `--feature` option for `post get`. --- features/post.feature | 34 ++++++++++++++++++++++++ features/steps/basic_steps.php | 60 ++++++++++++++++++++++++++++++++++++++++++ man-src/post-get.txt | 13 +++++++++ man/post-get.1 | 20 +++++++++++++- php/commands/post.php | 57 ++++++++++++++++++++++++++++++++------- 5 files changed, 173 insertions(+), 11 deletions(-) diff --git a/features/post.feature b/features/post.feature index 87460906..74f6cd84 100644 --- a/features/post.feature +++ b/features/post.feature @@ -42,3 +42,37 @@ Feature: Manage WordPress posts """ Test content. """ + + When I run `wp post get --format=content {POST_ID}` + Then it should run without errors + And STDOUT should be: + """ + Test content. + """ + + When I run `wp post get --format=table {POST_ID}` + Then it should run without errors + And STDOUT should be a table containing rows: + """ + Field Value + ID {POST_ID} + post_title Test post + post_content Test content. + """ + + When I run `wp post get --format=csv {POST_ID}` + Then it should run without errors + And STDOUT should be a table containing rows: + """ + Field,Value + ID,{POST_ID} + post_title,"Test post" + post_content,"Test content." + """ + + When I run `wp post get --format=json {POST_ID}` + Then it should run without errors + And STDOUT should be JSON containing: + """ + {"ID":{POST_ID},"post_title":"Test post","post_content":"Test content."} + """ diff --git a/features/steps/basic_steps.php b/features/steps/basic_steps.php index 352bdb77..d6eb182e 100644 --- a/features/steps/basic_steps.php +++ b/features/steps/basic_steps.php @@ -178,6 +178,41 @@ $steps->Then( '/^(STDOUT|STDERR) should match \'([^\']+)\'$/', } ); +$steps->Then( '/^STDOUT should be a table containing rows:$/', + function ( $world, PyStringNode $expected ) { + $output = $world->result->STDOUT; + $outputRows = explode( "\n", rtrim( $output, "\n" ) ); + + $expected = $world->replace_variables( (string) $expected ); + $expectedRows = explode( "\n", rtrim( $expected, "\n" ) ); + + // the first row is the header and must be present + if ( $expectedRows[0] != $outputRows[0] ) { + throw new \Exception( $output ); + } + + unset($outputRows[0]); + unset($expectedRows[0]); + $matches = array_intersect( $expectedRows, $outputRows ); + if ( count( $expectedRows ) != count( $matches ) ) { + throw new \Exception( $output ); + } + } +); + +$steps->Then( '/^STDOUT should be JSON containing:$/', + function ( $world, PyStringNode $expected ) { + $output = $world->result->STDOUT; + $outputJson = json_decode( $output ); + + $expected = $world->replace_variables( (string) $expected ); + $expectedJson = json_decode( $expected ); + + if ( !compareJson( $expectedJson, $outputJson ) ) { + throw new \Exception( $output ); + } +}); + $steps->Then( '/^(STDOUT|STDERR) should be empty$/', function ( $world, $stream ) { if ( !empty( $world->result->$stream ) ) { @@ -197,3 +232,28 @@ $steps->Then( '/^the (.+) file should exist$/', assertFileExists( $world->get_path( $path ) ); } ); + + +function compareJson( $expected, $actual ) { + if ( gettype( $expected ) != gettype( $actual ) ) { + return false; + } + + if ( is_object( $expected ) ) { + foreach ( get_object_vars( $expected ) as $name => $value ) { + if ( !compareJson( $value, $actual->$name ) ) { + return false; + } + } + } else if ( is_array( $expected ) ) { + foreach ( $expected as $key => $value ) { + if ( !compareJson( $value, $actual[$key] ) ) { + return false; + } + } + } else { + return $expected === $actual; + } + + return true; +} diff --git a/man-src/post-get.txt b/man-src/post-get.txt index 38d10df7..ff20d1df 100644 --- a/man-src/post-get.txt +++ b/man-src/post-get.txt @@ -1,5 +1,18 @@ ## OPTIONS +* `[--format=]`: + + The format to use when printing the post, acceptable values: + + content: outputs only the post's content (default) + + table: output all fields of the post as a table, note that the + post_content field is output last to simplify parsing + + csv: output all fields in Comma Separated Value format + + json: output all fields in JavaScript Object Notation format + * ``: The ID of the post to get. diff --git a/man/post-get.1 b/man/post-get.1 index 76665514..1a44a829 100644 --- a/man/post-get.1 +++ b/man/post-get.1 @@ -7,11 +7,29 @@ \fBwp\-post\-get\fR \- Get a post\'s content by ID\. . .SH "SYNOPSIS" -wp post get \fIid\fR +wp post get [\-\-format=\fIformat\fR] \fIid\fR . .SH "OPTIONS" . .TP +\fB[\-\-format=]\fR: +. +.IP +The format to use when printing the post, acceptable values: +. +.IP +content: outputs only the post\'s content (default) +. +.IP +table: output all fields of the post as a table, note that the post_content field is output last to simplify parsing +. +.IP +csv: output all fields in Comma Separated Value format +. +.IP +json: output all fields in JavaScript Object Notation format +. +.TP \fB\fR: . .IP diff --git a/php/commands/post.php b/php/commands/post.php index e2bd9343..b84d1934 100644 --- a/php/commands/post.php +++ b/php/commands/post.php @@ -65,14 +65,14 @@ class Post_Command extends \WP_CLI\CommandWithDBObject { public function edit( $args, $_ ) { $post_id = $args[0]; if ( !$post_id || !$post = get_post( $post_id ) ) - \WP_CLI::error( "Failed opening post $post_id to edit." ); + \WP_CLI::error( "Failed opening post $post_id to edit." ); $r = $this->_edit( $post->post_content, "WP-CLI post $post_id" ); if ( $r === false ) - \WP_CLI::warning( 'No change made to post content.', 'Aborted' ); + \WP_CLI::warning( 'No change made to post content.', 'Aborted' ); else - parent::update( $args, array( 'post_content' => $r ) ); + parent::update( $args, array( 'post_content' => $r ) ); } protected function _edit( $content, $title ) { @@ -80,16 +80,53 @@ class Post_Command extends \WP_CLI\CommandWithDBObject { } /** - * Get a post's content by ID. - * - * @synopsis - */ - public function get( $args, $_ ) { + * Get a post's content by ID. + * + * @synopsis [--format=] + */ + public function get( $args, $assoc_args ) { + $assoc_args = wp_parse_args( $assoc_args, array( + 'format' => 'content' + ) ); + $format = $assoc_args['format']; + $post_id = $args[0]; if ( !$post_id || !$post = get_post( $post_id ) ) - \WP_CLI::error( "Failed opening post $post_id to get." ); + \WP_CLI::error( "Failed opening post $post_id to get." ); + + switch ( $assoc_args['format'] ) { + + case 'content': + echo($post->post_content); + break; - echo($post->post_content); + case 'table': + case 'csv': + $items = array(); + foreach ( get_object_vars( $post ) as $field => $value ) { + if ( 'post_content' === $field ) + continue; + + $item = new \stdClass; + $item->Field = $field; + $item->Value = $value; + $items[] = $item; + } + $content = new \stdClass; + $content->Field = 'post_content'; + $content->Value = $post->post_content; + $items[] = $content; + + \WP_CLI\Utils\format_items( $format, array( 'Field', 'Value' ), $items ); + break; + + case 'json': + echo( json_encode( $post ) ); + break; + + default: + \WP_CLI::error( "Invalid value for format: " . $format ); + } } /** -- 2.11.0