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

[#5746] improve(CLI): Support table format output for Audit command #6461

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class CommandContext {
private final boolean quiet;
private final CommandLine line;
private final String auth;
// TODO make it final
private int outputLimit;
Copy link
Member

Choose a reason for hiding this comment

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

can it be made final?


private String ignoreEnv;
private boolean ignoreSet = false;
Expand All @@ -55,6 +57,7 @@ public CommandContext(CommandLine line) {
? line.getOptionValue(GravitinoOptions.OUTPUT)
: Command.OUTPUT_FORMAT_PLAIN;
this.quiet = line.hasOption(GravitinoOptions.QUIET);
this.outputLimit = -1;
Copy link
Member

Choose a reason for hiding this comment

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

It's unclear to me what this is for - perhaps it needs a better name or better still in another PR


this.url = getUrl();
this.ignoreVersions = getIgnore();
Expand Down Expand Up @@ -106,6 +109,15 @@ public boolean quiet() {
return quiet;
}

/**
* Returns the output limit.
*
* @return The output limit.
*/
public int outputLimit() {
return outputLimit;
}

/**
* Returns the authentication type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ public void handle() {}
* @param audit from a class that implements the Auditable interface.
*/
public void displayAuditInfo(Audit audit) {
String auditInfo =
"creator,create_time,modified,modified_time"
+ System.lineSeparator()
+ audit.creator()
+ ","
+ audit.createTime()
+ ","
+ audit.lastModifier()
+ ","
+ audit.lastModifiedTime();

printResults(auditInfo);
printResults(audit);
}
}
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 @@ -218,22 +229,16 @@ protected <T extends GravitinoClientBase> Builder<T> constructClient(Builder<T>
return builder;
}

/**
* Outputs the entity to the console.
*
* @param entity The entity to output.
* @param <T> The type of entity.
*/
protected <T> void output(T entity) {
private <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,7 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
catalogs = client.listCatalogsInfo();
output(catalogs);
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,7 @@ public void handle() {
try {
GravitinoAdminClient client = buildAdminClient();
metalakes = client.listMetalakes();
output(metalakes);
printResults(metalakes);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package org.apache.gravitino.cli.commands;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.util.List;
import org.apache.gravitino.Schema;
import org.apache.gravitino.SupportsSchemas;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
Expand Down Expand Up @@ -48,10 +51,14 @@ public ListSchema(CommandContext context, String metalake, String catalog) {
/** List all schema names in a schema. */
@Override
public void handle() {
String[] schemas = new String[0];
List<Schema> schemas = Lists.newArrayList();
try {
// There may be a performance overhead
GravitinoClient client = buildClient(metalake);
schemas = client.loadCatalog(catalog).asSchemas().listSchemas();
SupportsSchemas gObject = client.loadCatalog(catalog).asSchemas();
for (String schemaName : gObject.listSchemas()) {
schemas.add(gObject.loadSchema(schemaName));
}
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
Expand All @@ -60,8 +67,10 @@ public void handle() {
exitWithError(exp.getMessage());
}

String all = schemas.length == 0 ? "No schemas exist." : Joiner.on(",").join(schemas);

printInformation(all);
if (schemas.isEmpty()) {
printInformation("No schemas exist.");
} else {
printResults(schemas.toArray(new Schema[0]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

package org.apache.gravitino.cli.commands;

import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.rel.Table;
import org.apache.gravitino.rel.TableCatalog;

/** List the names of all tables in a schema. */
public class ListTables extends TableCommand {
Expand All @@ -47,24 +48,26 @@ public ListTables(CommandContext context, String metalake, String catalog, Strin
/** List the names of all tables in a schema. */
@Override
public void handle() {
NameIdentifier[] tables = null;
NameIdentifier[] tables;
Namespace name = Namespace.of(schema);
TableCatalog tableCatalog;
List<Table> tablesList = new ArrayList<>();

try {
tables = tableCatalog().listTables(name);
// There may be a performance overhead
tableCatalog = tableCatalog();
tables = tableCatalog.listTables(name);
for (NameIdentifier table : tables) {
tablesList.add(tableCatalog.loadTable(table));
}
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

List<String> tableNames = new ArrayList<>();
for (int i = 0; i < tables.length; i++) {
tableNames.add(tables[i].name());
if (tablesList.isEmpty()) {
printInformation("No tables exist.");
} else {
printResults(tablesList.toArray(new Table[0]));
}

String all =
tableNames.isEmpty()
? "No tables exist."
: Joiner.on(System.lineSeparator()).join(tableNames);

printResults(all);
}
}
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
Expand Up @@ -68,7 +68,7 @@ public void handle() {
}

if (result != null) {
printInformation(result.name() + "," + result.comment());
printResults(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

printInformation(gTable.name() + "," + gTable.comment());
printResults(gTable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 com.google.common.base.Preconditions;
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. This class handles basic output operations and provides
* configurable behavior for quiet mode, output limiting.
*/
public abstract class BaseOutputFormat<T> implements OutputFormat<T> {
protected int limit;
protected CommandContext context;

/**
* Creates a new {@link BaseOutputFormat} with specified configuration.
*
* @param context the command context, must not be null;
*/
public BaseOutputFormat(CommandContext context) {
Preconditions.checkNotNull(context, "CommandContext cannot be null");
this.context = context;
this.limit = context.outputLimit();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this limit is gonna be a generic parameter for most commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am worried that too many schemas or tables will affect the display

}

/**
* 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