From bcc35f62abca08eb85af38ce0c2bf44b7429f2a4 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 14 Mar 2022 08:41:08 -0300 Subject: [PATCH 1/3] Add a --pretty flag to WP-CLI get-mapping + docs --- includes/classes/Command.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 249b450dcc..0eea02bdd9 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -364,11 +364,14 @@ private function put_mapping_helper( $args, $assoc_args ) { * * ## OPTIONS * - * [--index-name] + * [--index-name=] * : The name of the index for which to return the mapping. If not passed, all mappings will be returned * + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. + * * @subcommand get-mapping - * @since 3.6.4 + * @since 3.6.4, `--pretty` introduced in 4.1.0 * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args. */ @@ -379,9 +382,16 @@ public function get_mapping( $args, $assoc_args ) { $response = Elasticsearch::factory()->remote_request( $path ); - $body = wp_remote_retrieve_body( $response ); + $response_body = wp_remote_retrieve_body( $response ); - WP_CLI::line( $body ); + if ( ! empty( $assoc_args['pretty'] ) ) { + $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); + if ( preg_match( '/json/', $content_type ) ) { + // Re-encode the JSON to add space formatting + $response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT ); + } + } + WP_CLI::line( $response_body ); } /** From 87705ffc37ab404149d32e5e9ee9e6c7fca7ab59 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 25 Mar 2022 14:48:47 -0300 Subject: [PATCH 2/3] Add --pretty to other commands --- includes/classes/Command.php | 75 +++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index a9c4e2192c..36da049c0d 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -382,23 +382,17 @@ public function get_mapping( $args, $assoc_args ) { $response = Elasticsearch::factory()->remote_request( $path ); - $response_body = wp_remote_retrieve_body( $response ); - - if ( ! empty( $assoc_args['pretty'] ) ) { - $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); - if ( preg_match( '/json/', $content_type ) ) { - // Re-encode the JSON to add space formatting - $response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT ); - } - } - WP_CLI::line( $response_body ); + $this->print_json_response( $response, ! empty( $assoc_args['pretty'] ) ); } /** * Return all indexes from the cluster as a JSON object. * + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. + * * @subcommand get-cluster-indexes - * @since 3.2 + * @since 3.2, `--pretty` introduced in 4.1.0 * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args. */ @@ -407,23 +401,26 @@ public function get_cluster_indexes( $args, $assoc_args ) { $response = Elasticsearch::factory()->remote_request( $path ); - $body = wp_remote_retrieve_body( $response ); - - WP_CLI::line( $body ); + $this->print_json_response( $response, ! empty( $assoc_args['pretty'] ) ); } /** * Return all index names as a JSON object. * + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. + * * @subcommand get-indexes - * @since 3.2 + * @since 3.2, `--pretty` introduced in 4.1.0 * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args. */ public function get_indexes( $args, $assoc_args ) { $index_names = $this->get_index_names(); - WP_CLI::line( wp_json_encode( $index_names ) ); + $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; + + WP_CLI::line( wp_json_encode( $index_names, $flag ) ); } /** @@ -995,7 +992,11 @@ public function clear_index() { * items_indexed | integer | Total number of items indexed * total_items | integer | Total number of items indexed or -1 if not yet determined * + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. + * * @subcommand get-indexing-status + * @since 3.5.1, `--pretty` introduced in 4.1.0 */ public function get_indexing_status() { $indexing_status = Utils\get_indexing_status(); @@ -1009,7 +1010,9 @@ public function get_indexing_status() { ]; } - WP_CLI::line( wp_json_encode( $indexing_status ) ); + $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; + + WP_CLI::line( wp_json_encode( $indexing_status, $flag ) ); } /** @@ -1020,8 +1023,11 @@ public function get_indexing_status() { * [--clear] * : Clear the `ep_last_cli_index` option. * - * @subcommand get-last-cli-index + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. * + * @subcommand get-last-cli-index + * @since 3.5.1, `--pretty` introduced in 4.1.0 * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args. */ @@ -1033,8 +1039,9 @@ public function get_last_cli_index( $args, $assoc_args ) { delete_site_option( 'ep_last_cli_index' ); } - WP_CLI::line( wp_json_encode( $last_sync ) ); + $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; + WP_CLI::line( wp_json_encode( $last_sync, $flag ) ); } @@ -1371,9 +1378,12 @@ public function call_ep_cli_put_mapping( $index_meta, $indexable ) { * [--debug-http-request] * : Enable debugging * + * [--pretty] + * : Use this flag to render a pretty-printed version of the JSON response. + * * @subcommand request * - * @since 3.6.6 + * @since 3.6.6, `--pretty` introduced in 4.1.0 * * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args. @@ -1441,13 +1451,26 @@ function ( $response, $context, $transport, $request_args, $url ) { WP_CLI::error( $response->get_error_message() ); } + $this->print_json_response( $response, ! empty( $assoc_args['pretty'] ) ); + } + + /** + * Print an HTTP response. + * + * @since 4.1.0 + * @param array $response HTTP Response. + * @param boolean $pretty Whether the JSON response should be formatted or not. + */ + protected function print_json_response( $response, $pretty ) { $response_body = wp_remote_retrieve_body( $response ); - $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); - if ( preg_match( '/json/', $content_type ) ) { - // Re-encode the JSON to add space formatting - $response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT ); - } - WP_CLI::log( $response_body ); + if ( $pretty ) { + $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); + if ( preg_match( '/json/', $content_type ) ) { + // Re-encode the JSON to add space formatting + $response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT ); + } + } + WP_CLI::line( $response_body ); } } From 54d4ad0b26c72d578e0706299432f6c607a979ca Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 28 Mar 2022 14:50:51 -0300 Subject: [PATCH 3/3] Add the `pretty_json_encode` method --- includes/classes/Command.php | 45 ++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 36da049c0d..928758ac21 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -418,9 +418,7 @@ public function get_cluster_indexes( $args, $assoc_args ) { public function get_indexes( $args, $assoc_args ) { $index_names = $this->get_index_names(); - $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; - - WP_CLI::line( wp_json_encode( $index_names, $flag ) ); + $this->pretty_json_encode( $index_names, ! empty( $assoc_args['pretty'] ) ); } /** @@ -997,8 +995,10 @@ public function clear_index() { * * @subcommand get-indexing-status * @since 3.5.1, `--pretty` introduced in 4.1.0 + * @param array $args Positional CLI args. + * @param array $assoc_args Associative CLI args. */ - public function get_indexing_status() { + public function get_indexing_status( $args, $assoc_args ) { $indexing_status = Utils\get_indexing_status(); if ( empty( $indexing_status ) ) { @@ -1010,9 +1010,7 @@ public function get_indexing_status() { ]; } - $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; - - WP_CLI::line( wp_json_encode( $indexing_status, $flag ) ); + $this->pretty_json_encode( $indexing_status, ! empty( $assoc_args['pretty'] ) ); } /** @@ -1039,9 +1037,7 @@ public function get_last_cli_index( $args, $assoc_args ) { delete_site_option( 'ep_last_cli_index' ); } - $flag = ( ! empty( $assoc_args['pretty'] ) ) ? JSON_PRETTY_PRINT : null; - - WP_CLI::line( wp_json_encode( $last_sync, $flag ) ); + $this->pretty_json_encode( $last_sync, ! empty( $assoc_args['pretty'] ) ); } @@ -1464,13 +1460,28 @@ function ( $response, $context, $transport, $request_args, $url ) { protected function print_json_response( $response, $pretty ) { $response_body = wp_remote_retrieve_body( $response ); - if ( $pretty ) { - $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); - if ( preg_match( '/json/', $content_type ) ) { - // Re-encode the JSON to add space formatting - $response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT ); - } + $content_type = wp_remote_retrieve_header( $response, 'Content-Type' ); + + if ( ! $pretty || ! preg_match( '/json/', $content_type ) ) { + WP_CLI::line( $response_body ); + return; } - WP_CLI::line( $response_body ); + + // Re-encode the JSON to add space formatting + $response_body_obj = json_decode( $response_body ); + + $this->pretty_json_encode( $response_body_obj, JSON_PRETTY_PRINT ); + } + + /** + * Output a JSON object. Conditionally format it before doing so. + * + * @since 4.1.0 + * @param array $json_obj The JSON object or array. + * @param boolean $pretty_print_flag Whether it should or not be formatted. + */ + protected function pretty_json_encode( $json_obj, $pretty_print_flag ) { + $flag = $pretty_print_flag ? JSON_PRETTY_PRINT : null; + WP_CLI::line( wp_json_encode( $json_obj, $flag ) ); } }