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

[#5957] feat(CLI): Table format output for SchemaDetails command #5975

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -35,6 +35,7 @@ public class SchemaCommandHandler extends CommandHandler {
private final String metalake;
private final String catalog;
private String schema;
private final String outputFormat;

/**
* Constructs a {@link SchemaCommandHandler} instance.
Expand All @@ -55,6 +56,7 @@ public SchemaCommandHandler(
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
this.outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
}

@Override
Expand Down Expand Up @@ -132,7 +134,7 @@ private void handleDetailsCommand() {
.handle();
} else {
gravitinoCommandLine
.newSchemaDetails(url, ignore, metalake, catalog, schema)
.newSchemaDetails(url, ignore, outputFormat, metalake, catalog, schema)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ protected SchemaAudit newSchemaAudit(
}

protected SchemaDetails newSchemaDetails(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new SchemaDetails(url, ignore, metalake, catalog, schema);
String url,
boolean ignore,
String outputFormat,
String metalake,
String catalog,
String schema) {
return new SchemaDetails(url, ignore, outputFormat, metalake, catalog, schema);
}

protected ListSchema newListSchema(String url, boolean ignore, String metalake, String catalog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ public class SchemaDetails extends Command {
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param outputFormat The output format.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schenma.
*/
public SchemaDetails(
String url, boolean ignoreVersions, String metalake, String catalog, String schema) {
super(url, ignoreVersions);
String url,
boolean ignoreVersions,
String outputFormat,
String metalake,
String catalog,
String schema) {
super(url, ignoreVersions, outputFormat);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand All @@ -58,6 +64,7 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
result = client.loadCatalog(catalog).asSchemas().loadSchema(schema);
output(result);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
Expand All @@ -67,9 +74,5 @@ public void handle() {
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

if (result != null) {
System.out.println(result.name() + "," + result.comment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Collectors;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;

/** Plain format to print a pretty string to standard out. */
public class PlainFormat {
Expand All @@ -35,6 +36,8 @@ public static void output(Object object) {
new CatalogPlainFormat().output((Catalog) object);
} else if (object instanceof Catalog[]) {
new CatalogsPlainFormat().output((Catalog[]) object);
} else if (object instanceof Schema) {
new SchemaPlainFormat().output((Schema) object);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -88,4 +91,15 @@ public void output(Catalog[] catalogs) {
}
}
}

static final class SchemaPlainFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
if (schema == null) {
System.out.println("No schemas exist.");
} else {
System.out.println(schema.name() + "," + schema.comment());
}
}
justinmclean marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.regex.Pattern;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;

/** Table format to print a pretty table to standard out. */
public class TableFormat {
Expand All @@ -37,6 +38,8 @@ public static void output(Object object) {
new CatalogTableFormat().output((Catalog) object);
} else if (object instanceof Catalog[]) {
new CatalogsTableFormat().output((Catalog[]) object);
} else if (object instanceof Schema) {
new SchemaTableFormat().output((Schema) object);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -103,6 +106,21 @@ public void output(Catalog[] catalogs) {
}
}

static final class SchemaTableFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
if (schema == null) {
System.out.println("No schemas exist.");
} else {
List<String> headers = Arrays.asList("schema", "comment");
List<List<String>> rows = new ArrayList<>();
rows.add(Arrays.asList(schema.name(), schema.comment() + ""));
TableFormatImpl tableFormat = new TableFormatImpl();
tableFormat.print(headers, rows);
}
}
}

static final class TableFormatImpl {
private int[] maxElementLengths;
// This expression is primarily used to match characters that have a display width of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void testSchemaDetailsCommand() {
doReturn(mockDetails)
.when(commandLine)
.newSchemaDetails(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema");
GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo", "catalog", "schema");
doReturn(mockDetails).when(mockDetails).validate();
commandLine.handleCommandLine();
verify(mockDetails).handle();
Expand Down
Loading