Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --pretty flag to WP-CLI get-mapping + docs #2653

Merged
merged 6 commits into from
Mar 28, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,14 @@ private function put_mapping_helper( $args, $assoc_args ) {
*
* ## OPTIONS
*
* [--index-name]
* [--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.
*/
Expand All @@ -379,16 +382,17 @@ public function get_mapping( $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 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.
*/
Expand All @@ -397,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 ) );
}

/**
Expand Down Expand Up @@ -985,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();
Expand All @@ -999,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 ) );
}

/**
Expand All @@ -1010,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.
*/
Expand All @@ -1023,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;
Copy link
Contributor

@oscarssanchez oscarssanchez Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1042-1044 are similar to 1013-1015 and somewhat similar to 1471 and 1474 is there a way we could make it more DRY? maybe splitting print_json_response() into two functions, one that prints the JSON based on the pretty flag, and other that processes the response/option data ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that and wondered if it wouldn't be an overengineering but you are right. I've pushed a new commit addressing that (and also passing arguments to get_indexing_status() as that wasn't working properly). Do you mind checking it again, @oscarssanchez ? Thanks!


WP_CLI::line( wp_json_encode( $last_sync, $flag ) );
}


Expand Down Expand Up @@ -1361,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.
Expand Down Expand Up @@ -1431,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 );
}
}