Skip to content

Commit

Permalink
Add --pretty to other commands
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Mar 25, 2022
1 parent 6750897 commit 87705ff
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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 ) );
}

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

/**
Expand All @@ -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.
*/
Expand All @@ -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 ) );
}


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

0 comments on commit 87705ff

Please sign in to comment.