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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -342,6 +342,7 @@ private void handleSchemaCommand() {
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String catalog = name.getCatalogName();
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);

Command.setAuthenticationMode(auth, userName);

Expand All @@ -365,7 +366,7 @@ private void handleSchemaCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
newSchemaAudit(url, ignore, metalake, catalog, schema).handle();
} else {
newSchemaDetails(url, ignore, metalake, catalog, schema).handle();
newSchemaDetails(url, ignore, outputFormat, metalake, catalog, schema).handle();
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,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 @@ -80,4 +83,11 @@ public void output(Catalog[] catalogs) {
System.out.println(all);
}
}

static final class SchemaPlainFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
System.out.println(schema.name() + "," + schema.comment());
}
Copy link
Member

Choose a reason for hiding this comment

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

This is missing the null check of the previous code. As per more recent PRs, it should also output "No schemas exist." if there are none.

}
}
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 @@ -95,6 +98,17 @@ public void output(Catalog[] catalogs) {
}
}

static final class SchemaTableFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
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 @@ -105,7 +105,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");
commandLine.handleCommandLine();
verify(mockDetails).handle();
}
Expand Down