Skip to content

Commit

Permalink
[apache#6481] improve(CLI): Refactor table output format (apache#6483)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Refactor table output format, make  it easier to test and scale.

### Why are the changes needed?

Fix: apache#6481 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

table format test

```bash
gcli metalake list -i  --output table

+-------------------+
|     Metalake      |
+-------------------+
| demo              |
| cli_demo          |
| demo_metalake     |
| test_cli_metalake |
| tyy               |
| demo3             |
+-------------------+

gcli metalake details -i --output table  -m demo_metalake

+---------------+-------------+
|   Metalake    |   Comment   |
+---------------+-------------+
| demo_metalake | new comment |
+---------------+-------------+

gcli catalog  list -i --output table  -m demo_metalake
+-------------------+
|      Catalog      |
+-------------------+
| File              |
| Hive_catalog      |
| Iceberg_catalog   |
| Mysql_catalog     |
| Test_hive_catalog |
+-------------------+

gcli catalog  details --name Hive_catalog  -i --output table  -m demo_metalake
+--------------+------------+----------+-------------+
|   Catalog    |    Type    | Provider |   Comment   |
+--------------+------------+----------+-------------+
| Hive_catalog | RELATIONAL | hive     | new comment |
+--------------+------------+----------+-------------+
```

plainformat test
```bash
gcli metalake list -i
# demo
# cli_demo
# demo_metalake
# test_cli_metalake
# demo3

gcli metalake details -i  -m demo_metalake
# demo_metalake, new comment

gcli catalog  list -i   -m demo_metalake
# File
# Hive_catalog
# Iceberg_catalog
# Mysql_catalog
# Test_hive_catalog

gcli catalog  details --name Hive_catalog  -i  -m demo_metalake
# Hive_catalog, RELATIONAL, hive, new comment
```
  • Loading branch information
Abyss-lord authored Feb 21, 2025
1 parent 02a20b7 commit 4ffe56c
Show file tree
Hide file tree
Showing 15 changed files with 1,652 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
result = client.loadCatalog(catalog);
output(result);
printResults(result);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.gravitino.cli.KerberosData;
import org.apache.gravitino.cli.Main;
import org.apache.gravitino.cli.OAuthData;
import org.apache.gravitino.cli.outputs.BaseOutputFormat;
import org.apache.gravitino.cli.outputs.PlainFormat;
import org.apache.gravitino.cli.outputs.TableFormat;
import org.apache.gravitino.client.DefaultOAuth2TokenProvider;
Expand Down Expand Up @@ -89,16 +90,26 @@ public void printInformation(String message) {
return;
}

System.out.print(message);
printResults(message);
}

/**
* Prints out an a results of a command.
* Outputs the entity result to the console.
*
* @param entity The entity to output.
* @param <T> The type of entity.
*/
public <T> void printResults(T entity) {
output(entity);
}

/**
* Prints out the string result of a command.
*
* @param results The results to display.
*/
public void printResults(String results) {
System.out.print(results);
BaseOutputFormat.output(results, System.out);
}

/**
Expand Down Expand Up @@ -226,14 +237,14 @@ protected <T extends GravitinoClientBase> Builder<T> constructClient(Builder<T>
*/
protected <T> void output(T entity) {
if (outputFormat == null) {
PlainFormat.output(entity);
PlainFormat.output(entity, context);
return;
}

if (outputFormat.equals(OUTPUT_FORMAT_TABLE)) {
TableFormat.output(entity);
TableFormat.output(entity, context);
} else if (outputFormat.equals(OUTPUT_FORMAT_PLAIN)) {
PlainFormat.output(entity);
PlainFormat.output(entity, context);
} else {
throw new IllegalArgumentException("Unsupported output format");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
catalogs = client.listCatalogsInfo();
output(catalogs);
if (catalogs.length == 0) {
printInformation("No catalogs exist.");
return;
}
printResults(catalogs);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public void handle() {
try {
GravitinoAdminClient client = buildAdminClient();
metalakes = client.listMetalakes();
output(metalakes);
if (metalakes.length == 0) {
printInformation("No metalakes exist.");
return;
}
printResults(metalakes);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
Metalake metalakeEntity = client.loadMetalake(metalake);
output(metalakeEntity);
printResults(metalakeEntity);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli.outputs;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import org.apache.gravitino.cli.CommandContext;

/**
* Abstract base implementation of {@link OutputFormat} interface providing common functionality for
* various output format implementations.
*/
public abstract class BaseOutputFormat<T> implements OutputFormat<T> {
protected CommandContext context;

/**
* Creates a new {@link BaseOutputFormat} with specified configuration.
*
* @param context the command context, must not be null;
*/
public BaseOutputFormat(CommandContext context) {
this.context = context;
}

/**
* Outputs a message to the specified OutputStream. This method handles both system streams
* ({@code System.out}, {@code System.err}) and regular output streams differently: - For system
* streams: Preserves the stream open after writing - For other streams: Automatically closes the
* stream after writing
*
* @param message the message to output, must not be null
* @param os the output stream to write to, must not be null If this is {@code System.out} or
* {@code System.err}, the stream will not be closed
* @throws IllegalArgumentException if either message or os is null
* @throws UncheckedIOException if an I/O error occurs during writing
*/
public static void output(String message, OutputStream os) {
if (message == null || os == null) {
throw new IllegalArgumentException(
"Message and OutputStream cannot be null, message: " + message + ", os: " + os);
}
boolean isSystemStream = (os == System.out || os == System.err);

try {
PrintStream printStream =
new PrintStream(
isSystemStream ? os : new BufferedOutputStream(os),
true,
StandardCharsets.UTF_8.name());

try {
printStream.println(message);
printStream.flush();
} finally {
if (!isSystemStream) {
printStream.close();
}
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to write message to output stream", e);
}
}

/**
* {@inheritDoc} This implementation checks the quiet flag and handles null output gracefully. If
* quiet mode is enabled, no output is produced.
*/
@Override
public void output(T entity) {
String outputMessage = getOutput(entity);
String output = outputMessage == null ? "" : outputMessage;
output(output, System.out);
}
}
Loading

0 comments on commit 4ffe56c

Please sign in to comment.