Skip to content

Commit

Permalink
[apache#5746] improve(CLI): Support table format output for Audit com…
Browse files Browse the repository at this point in the history
…mand

fix some bugs.
  • Loading branch information
Abyss-lord committed Feb 19, 2025
1 parent 2985aff commit 950cf34
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class CommandContext {
private final String url;
private final boolean quiet;
private final CommandLine line;
private final String auth;
// TODO make it final
private int outputLimit;
private final String auth;

private String ignoreEnv;
private boolean ignoreSet = false;
Expand All @@ -57,7 +57,6 @@ public CommandContext(CommandLine line) {
? line.getOptionValue(GravitinoOptions.OUTPUT)
: Command.OUTPUT_FORMAT_PLAIN;
this.quiet = line.hasOption(GravitinoOptions.QUIET);
// TODO add limit option to CLI
this.outputLimit = -1;

this.url = getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void handle() {
}

if (tablesList.isEmpty()) {
printResults("No tables exist.");
printInformation("No tables exist.");
} else {
printResults(tablesList.toArray(new Table[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public BaseOutputFormat(CommandContext context) {
*/
public static void output(String message, OutputStream os) {
if (message == null || os == null) {
throw new IllegalArgumentException("Message and OutputStream cannot be null");
throw new IllegalArgumentException(
"Message and OutputStream cannot be null, message: " + message + ", os: " + os);
}
boolean isSystemStream = (os == System.out || os == System.err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public static void output(Object entity, CommandContext context) {
if (entity instanceof Metalake) {
new MetalakePlainFormat(context).output((Metalake) entity);
} else if (entity instanceof Metalake[]) {
new MetalakesPlainFormat(context).output((Metalake[]) entity);
new MetalakeListPlainFormat(context).output((Metalake[]) entity);
} else if (entity instanceof Catalog) {
new CatalogPlainFormat(context).output((Catalog) entity);
} else if (entity instanceof Catalog[]) {
new CatalogsPlainFormat(context).output((Catalog[]) entity);
new CatalogListPlainFormat(context).output((Catalog[]) entity);
} else if (entity instanceof Schema) {
new SchemaPlainFormat(context).output((Schema) entity);
} else if (entity instanceof Schema[]) {
new SchemasPlainFormat(context).output((Schema[]) entity);
new SchemaListPlainFormat(context).output((Schema[]) entity);
} else if (entity instanceof Table) {
new TablePlainFormat(context).output((Table) entity);
} else if (entity instanceof Table[]) {
new TablesPlainFormat(context).output((Table[]) entity);
new TableListPlainFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditPlainFormat(context).output((Audit) entity);
} else {
Expand Down Expand Up @@ -97,9 +97,9 @@ public String getOutput(Metalake metalake) {
* Formats an array of Metalakes, outputting one name per line. Returns null if the array is empty
* or null.
*/
static final class MetalakesPlainFormat extends PlainFormat<Metalake[]> {
static final class MetalakeListPlainFormat extends PlainFormat<Metalake[]> {

public MetalakesPlainFormat(CommandContext context) {
public MetalakeListPlainFormat(CommandContext context) {
super(context);
}

Expand Down Expand Up @@ -137,8 +137,8 @@ public String getOutput(Catalog catalog) {
* Formats an array of Catalogs, outputting one name per line. Returns null if the array is empty
* or null.
*/
static final class CatalogsPlainFormat extends PlainFormat<Catalog[]> {
public CatalogsPlainFormat(CommandContext context) {
static final class CatalogListPlainFormat extends PlainFormat<Catalog[]> {
public CatalogListPlainFormat(CommandContext context) {
super(context);
}

Expand Down Expand Up @@ -176,8 +176,8 @@ public String getOutput(Schema schema) {
* Formats an array of Schemas, outputting one name per line. Returns null if the array is empty
* or null.
*/
static final class SchemasPlainFormat extends PlainFormat<Schema[]> {
public SchemasPlainFormat(CommandContext context) {
static final class SchemaListPlainFormat extends PlainFormat<Schema[]> {
public SchemaListPlainFormat(CommandContext context) {
super(context);
}

Expand Down Expand Up @@ -226,8 +226,8 @@ public String getOutput(Table table) {
* Formats an array of Tables, outputting one name per line. Returns null if the array is empty or
* null.
*/
static final class TablesPlainFormat extends PlainFormat<Table[]> {
public TablesPlainFormat(CommandContext context) {
static final class TableListPlainFormat extends PlainFormat<Table[]> {
public TableListPlainFormat(CommandContext context) {
super(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.rel.Table;

/**
* Abstract base class for formatting entity information into ASCII-art tables. Provides
Expand Down Expand Up @@ -79,15 +80,19 @@ public static void output(Object entity, CommandContext context) {
if (entity instanceof Metalake) {
new MetalakeTableFormat(context).output((Metalake) entity);
} else if (entity instanceof Metalake[]) {
new MetalakesTableFormat(context).output((Metalake[]) entity);
new MetalakeListTableFormat(context).output((Metalake[]) entity);
} else if (entity instanceof Catalog) {
new CatalogTableFormat(context).output((Catalog) entity);
} else if (entity instanceof Catalog[]) {
new CatalogsTableFormat(context).output((Catalog[]) entity);
new CatalogListTableFormat(context).output((Catalog[]) entity);
} else if (entity instanceof Schema) {
new SchemaTableFormat(context).output((Schema) entity);
} else if (entity instanceof Schema[]) {
new SchemasTableFormat(context).output((Schema[]) entity);
new SchemaListTableFormat(context).output((Schema[]) entity);
} else if (entity instanceof Table) {
new TableTableFormat(context).output((Table) entity);
} else if (entity instanceof Table[]) {
new TableListTableFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditTableFormat(context).output((Audit) entity);
} else {
Expand Down Expand Up @@ -524,23 +529,23 @@ public MetalakeTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Metalake metalake) {
Column columnA = new Column(context, "METALAKE");
Column columnB = new Column(context, "COMMENT");
Column columnName = new Column(context, "METALAKE");
Column columnComment = new Column(context, "COMMENT");

columnA.addCell(metalake.name());
columnB.addCell(metalake.comment());
columnName.addCell(metalake.name());
columnComment.addCell(metalake.comment());

return getTableFormat(columnA, columnB);
return getTableFormat(columnName, columnComment);
}
}

/**
* Formats an array of Metalakes into a single-column table display. Lists all metalake names in a
* vertical format.
*/
static final class MetalakesTableFormat extends TableFormat<Metalake[]> {
static final class MetalakeListTableFormat extends TableFormat<Metalake[]> {

public MetalakesTableFormat(CommandContext context) {
public MetalakeListTableFormat(CommandContext context) {
super(context);
}

Expand All @@ -551,10 +556,10 @@ public String getOutput(Metalake[] metalakes) {
output("No metalakes exist.", System.err);
return null;
} else {
Column columnA = new Column(context, "METALAKE");
Arrays.stream(metalakes).forEach(metalake -> columnA.addCell(metalake.name()));
Column columnName = new Column(context, "METALAKE");
Arrays.stream(metalakes).forEach(metalake -> columnName.addCell(metalake.name()));

return getTableFormat(columnA);
return getTableFormat(columnName);
}
}
}
Expand All @@ -572,27 +577,27 @@ public CatalogTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Catalog catalog) {
Column columnA = new Column(context, "CATALOG");
Column columnB = new Column(context, "TYPE");
Column columnC = new Column(context, "PROVIDER");
Column columnD = new Column(context, "COMMENT");
Column columnName = new Column(context, "CATALOG");
Column columnType = new Column(context, "TYPE");
Column columnProvider = new Column(context, "PROVIDER");
Column columnComment = new Column(context, "COMMENT");

columnA.addCell(catalog.name());
columnB.addCell(catalog.type().name());
columnC.addCell(catalog.provider());
columnD.addCell(catalog.comment());
columnName.addCell(catalog.name());
columnType.addCell(catalog.type().name());
columnProvider.addCell(catalog.provider());
columnComment.addCell(catalog.comment());

return getTableFormat(columnA, columnB, columnC, columnD);
return getTableFormat(columnName, columnType, columnProvider, columnComment);
}
}

/**
* Formats an array of Catalogs into a single-column table display. Lists all catalog names in a
* vertical format.
*/
static final class CatalogsTableFormat extends TableFormat<Catalog[]> {
static final class CatalogListTableFormat extends TableFormat<Catalog[]> {

public CatalogsTableFormat(CommandContext context) {
public CatalogListTableFormat(CommandContext context) {
super(context);
}

Expand All @@ -603,10 +608,10 @@ public String getOutput(Catalog[] catalogs) {
output("No metalakes exist.", System.err);
return null;
} else {
Column columnA = new Column(context, "CATALOG");
Arrays.stream(catalogs).forEach(metalake -> columnA.addCell(metalake.name()));
Column columnName = new Column(context, "CATALOG");
Arrays.stream(catalogs).forEach(metalake -> columnName.addCell(metalake.name()));

return getTableFormat(columnA);
return getTableFormat(columnName);
}
}
}
Expand All @@ -623,22 +628,22 @@ public SchemaTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Schema schema) {
Column columnA = new Column(context, "SCHEMA");
Column columnB = new Column(context, "COMMENT");
Column columnName = new Column(context, "SCHEMA");
Column columnComment = new Column(context, "COMMENT");

columnA.addCell(schema.name());
columnB.addCell(schema.comment());
columnName.addCell(schema.name());
columnComment.addCell(schema.comment());

return getTableFormat(columnA, columnB);
return getTableFormat(columnName, columnComment);
}
}

/**
* Formats an array of Schemas into a single-column table display. Lists all schema names in a
* vertical format.
*/
static final class SchemasTableFormat extends TableFormat<Schema[]> {
public SchemasTableFormat(CommandContext context) {
static final class SchemaListTableFormat extends TableFormat<Schema[]> {
public SchemaListTableFormat(CommandContext context) {
super(context);
}

Expand All @@ -649,10 +654,61 @@ public String getOutput(Schema[] schemas) {
output("No schemas exist.", System.err);
return null;
} else {
Column column = new Column(context, "SCHEMA");
Arrays.stream(schemas).forEach(schema -> column.addCell(schema.name()));
Column columnName = new Column(context, "SCHEMA");
Arrays.stream(schemas).forEach(schema -> columnName.addCell(schema.name()));

return getTableFormat(column);
return getTableFormat(columnName);
}
}
}

/**
* Formats a single Table instance into a three-column table display. Displays table details
* including column-name, column-type, and column-comment information.
*/
static final class TableTableFormat extends TableFormat<Table> {
public TableTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Table entity) {
Column columnName = new Column(context, "NAME");
Column columnType = new Column(context, "TYPE");
Column columnComment = new Column(context, "COMMENT");

for (org.apache.gravitino.rel.Column column : entity.columns()) {
columnName.addCell(column.name());
columnType.addCell(column.dataType().simpleString());
columnComment.addCell(column.comment());
}

return getTableFormat(columnName, columnType, columnComment);
}
}

/**
* Formats an array of Tables into a single-column table display. Lists all table names in a
* vertical format.
*/
static final class TableListTableFormat extends TableFormat<Table[]> {
public TableListTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Table[] entities) {
if (entities.length == 0) {
return null;
} else {
Column columnName = new Column(context, "NAME");
for (Table table : entities) {
columnName.addCell(table.name());
}

return getTableFormat(columnName);
}
}
}
Expand All @@ -665,17 +721,17 @@ public AuditTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Audit audit) {
Column columnA = new Column(context, "creator");
Column columnB = new Column(context, "create_time");
Column columnC = new Column(context, "modified");
Column columnD = new Column(context, "modify_time");
Column columnCreator = new Column(context, "creator");
Column columnCreateTime = new Column(context, "create_time");
Column columnModified = new Column(context, "modified");
Column columnModifyTime = new Column(context, "modify_time");

columnA.addCell(audit.creator());
columnB.addCell(audit.createTime());
columnC.addCell(audit.lastModifier());
columnD.addCell(audit.lastModifiedTime());
columnCreator.addCell(audit.creator());
columnCreateTime.addCell(audit.createTime());
columnModified.addCell(audit.lastModifier());
columnModifyTime.addCell(audit.lastModifiedTime());

return getTableFormat(columnA, columnB, columnC, columnD);
return getTableFormat(columnCreator, columnCreateTime, columnModified, columnModifyTime);
}
}
}

0 comments on commit 950cf34

Please sign in to comment.