diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java index c0775dae966..58398585c31 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java @@ -29,8 +29,7 @@ public class ColumnCommandHandler extends CommandHandler { private final GravitinoCommandLine gravitinoCommandLine; private final CommandLine line; private final String command; - private final boolean ignore; - private final String url; + private final CommandContext context; private final FullName name; private final String metalake; private final String catalog; @@ -44,16 +43,19 @@ public class ColumnCommandHandler extends CommandHandler { * @param gravitinoCommandLine The Gravitino command line instance. * @param line The command line arguments. * @param command The command to execute. - * @param ignore Ignore server version mismatch. + * @param context The command context. */ public ColumnCommandHandler( - GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) { + GravitinoCommandLine gravitinoCommandLine, + CommandLine line, + String command, + CommandContext context) { this.gravitinoCommandLine = gravitinoCommandLine; this.line = line; this.command = command; - this.ignore = ignore; + this.context = context; - this.url = getUrl(line); + this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); @@ -120,7 +122,7 @@ private boolean executeCommand() { private void handleDetailsCommand() { if (line.hasOption(GravitinoOptions.AUDIT)) { gravitinoCommandLine - .newColumnAudit(url, ignore, metalake, catalog, schema, table, column) + .newColumnAudit(context, metalake, catalog, schema, table, column) .validate() .handle(); } else { @@ -144,8 +146,7 @@ private void handleCreateCommand() { gravitinoCommandLine .newAddColumn( - url, - ignore, + context, metalake, catalog, schema, @@ -164,7 +165,7 @@ private void handleCreateCommand() { /** Handles the "DELETE" command. */ private void handleDeleteCommand() { gravitinoCommandLine - .newDeleteColumn(url, ignore, metalake, catalog, schema, table, column) + .newDeleteColumn(context, metalake, catalog, schema, table, column) .validate() .handle(); } @@ -174,36 +175,35 @@ private void handleUpdateCommand() { if (line.hasOption(GravitinoOptions.COMMENT)) { String comment = line.getOptionValue(GravitinoOptions.COMMENT); gravitinoCommandLine - .newUpdateColumnComment(url, ignore, metalake, catalog, schema, table, column, comment) + .newUpdateColumnComment(context, metalake, catalog, schema, table, column, comment) .validate() .handle(); } if (line.hasOption(GravitinoOptions.RENAME)) { String newName = line.getOptionValue(GravitinoOptions.RENAME); gravitinoCommandLine - .newUpdateColumnName(url, ignore, metalake, catalog, schema, table, column, newName) + .newUpdateColumnName(context, metalake, catalog, schema, table, column, newName) .validate() .handle(); } if (line.hasOption(GravitinoOptions.DATATYPE) && !line.hasOption(GravitinoOptions.DEFAULT)) { String datatype = line.getOptionValue(GravitinoOptions.DATATYPE); gravitinoCommandLine - .newUpdateColumnDatatype(url, ignore, metalake, catalog, schema, table, column, datatype) + .newUpdateColumnDatatype(context, metalake, catalog, schema, table, column, datatype) .validate() .handle(); } if (line.hasOption(GravitinoOptions.POSITION)) { String position = line.getOptionValue(GravitinoOptions.POSITION); gravitinoCommandLine - .newUpdateColumnPosition(url, ignore, metalake, catalog, schema, table, column, position) + .newUpdateColumnPosition(context, metalake, catalog, schema, table, column, position) .validate() .handle(); } if (line.hasOption(GravitinoOptions.NULL)) { boolean nullable = line.getOptionValue(GravitinoOptions.NULL).equals("true"); gravitinoCommandLine - .newUpdateColumnNullability( - url, ignore, metalake, catalog, schema, table, column, nullable) + .newUpdateColumnNullability(context, metalake, catalog, schema, table, column, nullable) .validate() .handle(); } @@ -211,7 +211,7 @@ private void handleUpdateCommand() { boolean autoIncrement = line.getOptionValue(GravitinoOptions.AUTO).equals("true"); gravitinoCommandLine .newUpdateColumnAutoIncrement( - url, ignore, metalake, catalog, schema, table, column, autoIncrement) + context, metalake, catalog, schema, table, column, autoIncrement) .validate() .handle(); } @@ -220,7 +220,7 @@ private void handleUpdateCommand() { String dataType = line.getOptionValue(GravitinoOptions.DATATYPE); gravitinoCommandLine .newUpdateColumnDefault( - url, ignore, metalake, catalog, schema, table, column, defaultValue, dataType) + context, metalake, catalog, schema, table, column, defaultValue, dataType) .validate() .handle(); } @@ -229,7 +229,7 @@ private void handleUpdateCommand() { /** Handles the "LIST" command. */ private void handleListCommand() { gravitinoCommandLine - .newListColumns(url, ignore, metalake, catalog, schema, table) + .newListColumns(context, metalake, catalog, schema, table) .validate() .handle(); } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java index b5e5838f1bb..7230604655d 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java @@ -116,7 +116,7 @@ private void executeCommand() { } else if (line.hasOption(GravitinoOptions.OWNER)) { new OwnerCommandHandler(this, line, command, ignore, entity).handle(); } else if (entity.equals(CommandEntities.COLUMN)) { - new ColumnCommandHandler(this, line, command, ignore).handle(); + new ColumnCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.TABLE)) { new TableCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.SCHEMA)) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java index 8a64c6c551e..b27e24fde9e 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java @@ -538,19 +538,18 @@ protected UntagEntity newUntagEntity( } protected ColumnAudit newColumnAudit( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column) { - return new ColumnAudit(url, ignore, metalake, catalog, schema, table, column); + return new ColumnAudit(context, metalake, catalog, schema, table, column); } protected ListColumns newListColumns( - String url, boolean ignore, String metalake, String catalog, String schema, String table) { - return new ListColumns(url, ignore, metalake, catalog, schema, table); + CommandContext context, String metalake, String catalog, String schema, String table) { + return new ListColumns(context, metalake, catalog, schema, table); } protected SetOwner newSetOwner( @@ -703,8 +702,7 @@ protected RemoveFilesetProperty newRemoveFilesetProperty( } protected AddColumn newAddColumn( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, @@ -717,8 +715,7 @@ protected AddColumn newAddColumn( boolean autoIncrement, String defaultValue) { return new AddColumn( - url, - ignore, + context, metalake, catalog, schema, @@ -733,82 +730,72 @@ protected AddColumn newAddColumn( } protected DeleteColumn newDeleteColumn( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column) { - return new DeleteColumn(url, ignore, metalake, catalog, schema, table, column); + return new DeleteColumn(context, metalake, catalog, schema, table, column); } protected UpdateColumnComment newUpdateColumnComment( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String comment) { - return new UpdateColumnComment(url, ignore, metalake, catalog, schema, table, column, comment); + return new UpdateColumnComment(context, metalake, catalog, schema, table, column, comment); } protected UpdateColumnName newUpdateColumnName( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String rename) { - return new UpdateColumnName(url, ignore, metalake, catalog, schema, table, column, rename); + return new UpdateColumnName(context, metalake, catalog, schema, table, column, rename); } protected UpdateColumnDatatype newUpdateColumnDatatype( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String datatype) { - return new UpdateColumnDatatype( - url, ignore, metalake, catalog, schema, table, column, datatype); + return new UpdateColumnDatatype(context, metalake, catalog, schema, table, column, datatype); } protected UpdateColumnPosition newUpdateColumnPosition( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String position) { - return new UpdateColumnPosition( - url, ignore, metalake, catalog, schema, table, column, position); + return new UpdateColumnPosition(context, metalake, catalog, schema, table, column, position); } protected UpdateColumnNullability newUpdateColumnNullability( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, String table, String column, boolean nullable) { - return new UpdateColumnNullability( - url, ignore, metalake, catalog, schema, table, column, nullable); + return new UpdateColumnNullability(context, metalake, catalog, schema, table, column, nullable); } protected UpdateColumnAutoIncrement newUpdateColumnAutoIncrement( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, @@ -816,12 +803,11 @@ protected UpdateColumnAutoIncrement newUpdateColumnAutoIncrement( String column, boolean autoIncrement) { return new UpdateColumnAutoIncrement( - url, ignore, metalake, catalog, schema, table, column, autoIncrement); + context, metalake, catalog, schema, table, column, autoIncrement); } protected UpdateColumnDefault newUpdateColumnDefault( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String schema, @@ -830,7 +816,7 @@ protected UpdateColumnDefault newUpdateColumnDefault( String defaultValue, String dataType) { return new UpdateColumnDefault( - url, ignore, metalake, catalog, schema, table, column, defaultValue, dataType); + context, metalake, catalog, schema, table, column, defaultValue, dataType); } protected CreateTable newCreateTable( diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AddColumn.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AddColumn.java index 71603607166..6227f695d96 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AddColumn.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AddColumn.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.DefaultConverter; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.cli.ParseType; @@ -49,8 +50,7 @@ public class AddColumn extends Command { /** * Adds an optional column to a table. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -64,8 +64,7 @@ public class AddColumn extends Command { * @param defaultValue Default value of the column (optional). */ public AddColumn( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, @@ -77,7 +76,7 @@ public AddColumn( boolean nullable, boolean autoIncrement, String defaultValue) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -123,6 +122,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " added to table " + table + "."); + printInformation(column + " added to table " + table + "."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ColumnAudit.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ColumnAudit.java index db17f6551f8..7cee2f9eb64 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ColumnAudit.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ColumnAudit.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.Catalog; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -38,8 +39,7 @@ public class ColumnAudit extends AuditCommand { /** * Displays the audit information of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -47,14 +47,13 @@ public class ColumnAudit extends AuditCommand { * @param column The name of the column. */ public ColumnAudit( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -65,25 +64,20 @@ public ColumnAudit( /** Displays the audit information of a specified column. */ @Override public void handle() { - Catalog result; + Catalog result = null; try (GravitinoClient client = buildClient(metalake)) { result = client.loadCatalog(this.catalog); } catch (NoSuchMetalakeException err) { - System.err.println(ErrorMessages.UNKNOWN_METALAKE); - return; + exitWithError(ErrorMessages.UNKNOWN_METALAKE); } catch (NoSuchCatalogException err) { - System.err.println(ErrorMessages.UNKNOWN_CATALOG); - return; + exitWithError(ErrorMessages.UNKNOWN_CATALOG); } catch (NoSuchTableException err) { - System.err.println(ErrorMessages.UNKNOWN_TABLE); - return; + exitWithError(ErrorMessages.UNKNOWN_TABLE); } catch (NoSuchColumnException err) { - System.err.println(ErrorMessages.UNKNOWN_COLUMN); - return; + exitWithError(ErrorMessages.UNKNOWN_COLUMN); } catch (Exception exp) { - System.err.println(exp.getMessage()); - return; + exitWithError(exp.getMessage()); } if (result != null) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteColumn.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteColumn.java index 17ac22d1284..34ed73b2ee2 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteColumn.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteColumn.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -40,8 +41,7 @@ public class DeleteColumn extends Command { /** * Delete a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -49,14 +49,13 @@ public class DeleteColumn extends Command { * @param column The name of the column. */ public DeleteColumn( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -88,6 +87,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " deleted."); + printInformation(column + " deleted."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListColumns.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListColumns.java index e1d8eac2086..f638caf687f 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListColumns.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListColumns.java @@ -21,6 +21,7 @@ import com.google.common.base.Joiner; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.exceptions.NoSuchTableException; import org.apache.gravitino.rel.Column; @@ -34,21 +35,15 @@ public class ListColumns extends TableCommand { /** * Displays the details of a table's columns. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schenma. * @param table The name of the table. */ public ListColumns( - String url, - boolean ignoreVersions, - String metalake, - String catalog, - String schema, - String table) { - super(url, ignoreVersions, metalake, catalog); + CommandContext context, String metalake, String catalog, String schema, String table) { + super(context, metalake, catalog); this.schema = schema; this.table = table; } @@ -62,9 +57,8 @@ public void handle() { NameIdentifier name = NameIdentifier.of(schema, table); columns = tableCatalog().loadTable(name).columns(); } catch (NoSuchTableException noSuchTableException) { - System.err.println( + exitWithError( ErrorMessages.UNKNOWN_TABLE + Joiner.on(".").join(metalake, catalog, schema, table)); - return; } catch (Exception exp) { exitWithError(exp.getMessage()); } @@ -94,6 +88,6 @@ public void handle() { + System.lineSeparator()); } - System.out.print(all); + printResults(all.toString()); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnAutoIncrement.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnAutoIncrement.java index 99b27b200e8..44b114e0a27 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnAutoIncrement.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnAutoIncrement.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -42,8 +43,7 @@ public class UpdateColumnAutoIncrement extends Command { /** * Update the auto increment of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -52,15 +52,14 @@ public class UpdateColumnAutoIncrement extends Command { * @param autoincrement True if the column can be null, false if it must have non-null values. */ public UpdateColumnAutoIncrement( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, boolean autoincrement) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -94,6 +93,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " auto increment changed to " + autoincrement + "."); + printInformation(column + " auto increment changed to " + autoincrement + "."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnComment.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnComment.java index 2c7f05a8fd9..85413f1e8f8 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnComment.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnComment.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -42,8 +43,7 @@ public class UpdateColumnComment extends Command { /** * Update the comment of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -52,15 +52,14 @@ public class UpdateColumnComment extends Command { * @param comment New column comment. */ public UpdateColumnComment( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String comment) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -94,6 +93,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " comment changed."); + printInformation(column + " comment changed."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDatatype.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDatatype.java index 6eac9da7ef9..29e44b648d1 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDatatype.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDatatype.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.cli.ParseType; import org.apache.gravitino.client.GravitinoClient; @@ -44,8 +45,7 @@ public class UpdateColumnDatatype extends Command { /** * Update the data type of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -54,15 +54,14 @@ public class UpdateColumnDatatype extends Command { * @param datatype The new data type name. */ public UpdateColumnDatatype( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String datatype) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -97,6 +96,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " datatype changed to " + datatype + "."); + printInformation(column + " datatype changed to " + datatype + "."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDefault.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDefault.java index 976cf623054..cf8a49c47cd 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDefault.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnDefault.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.DefaultConverter; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; @@ -44,8 +45,7 @@ public class UpdateColumnDefault extends Command { /** * Update the default type of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -55,8 +55,7 @@ public class UpdateColumnDefault extends Command { * @param dataType The new default data type. */ public UpdateColumnDefault( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, @@ -64,7 +63,7 @@ public UpdateColumnDefault( String column, String defaultValue, String dataType) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -101,7 +100,7 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " default changed."); + printInformation(column + " default changed."); } @Override diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnName.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnName.java index 124223dbd29..74865f4c44d 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnName.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnName.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -42,8 +43,7 @@ public class UpdateColumnName extends Command { /** * Update the name of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -52,15 +52,14 @@ public class UpdateColumnName extends Command { * @param name The new column name. */ public UpdateColumnName( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String name) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -94,6 +93,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " name changed."); + printInformation(column + " name changed."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnNullability.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnNullability.java index 868b426868f..dce5c23aa20 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnNullability.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnNullability.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -42,8 +43,7 @@ public class UpdateColumnNullability extends Command { /** * Update the nullability of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -52,15 +52,14 @@ public class UpdateColumnNullability extends Command { * @param nullability True if the column can be null, false if it must have non-null values. */ public UpdateColumnNullability( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, boolean nullability) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -94,6 +93,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " nullability changed to " + nullability + "."); + printInformation(column + " nullability changed to " + nullability + "."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnPosition.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnPosition.java index 4bbe809d448..ba79aece7c8 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnPosition.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateColumnPosition.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.cli.PositionConverter; import org.apache.gravitino.client.GravitinoClient; @@ -43,8 +44,7 @@ public class UpdateColumnPosition extends Command { /** * Update the position of a column. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schema. @@ -53,15 +53,14 @@ public class UpdateColumnPosition extends Command { * @param position The new position of the column. */ public UpdateColumnPosition( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String schema, String table, String column, String position) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -97,6 +96,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(column + " position changed to " + position + "."); + printInformation(column + " position changed to " + position + "."); } } diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestColumnCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestColumnCommands.java index 31a3139482c..521b1e2b61d 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestColumnCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestColumnCommands.java @@ -21,6 +21,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -92,7 +96,11 @@ void testListColumnsCommand() { doReturn(mockList) .when(commandLine) .newListColumns( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema", "users"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users")); doReturn(mockList).when(mockList).validate(); commandLine.handleCommandLine(); verify(mockList).handle(); @@ -114,13 +122,12 @@ void testColumnAuditCommand() { doReturn(mockAudit) .when(commandLine) .newColumnAudit( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name")); doReturn(mockAudit).when(mockAudit).validate(); commandLine.handleCommandLine(); verify(mockAudit).handle(); @@ -142,14 +149,12 @@ void testColumnDetailsCommand() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newColumnAudit( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name"); - + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name")); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals(output, ErrorMessages.UNSUPPORTED_ACTION); } @@ -176,19 +181,18 @@ void testAddColumn() { doReturn(mockAddColumn) .when(commandLine) .newAddColumn( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "varchar(100)", - "comment", - null, - true, - false, - null); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("varchar(100)"), + eq("comment"), + isNull(), + anyBoolean(), + anyBoolean(), + isNull()); doReturn(mockAddColumn).when(mockAddColumn).validate(); commandLine.handleCommandLine(); verify(mockAddColumn).handle(); @@ -210,13 +214,12 @@ void testDeleteColumn() { doReturn(mockDelete) .when(commandLine) .newDeleteColumn( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name")); doReturn(mockDelete).when(mockDelete).validate(); commandLine.handleCommandLine(); verify(mockDelete).handle(); @@ -242,14 +245,13 @@ void testUpdateColumnComment() { doReturn(mockUpdateColumn) .when(commandLine) .newUpdateColumnComment( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "new comment"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("new comment")); doReturn(mockUpdateColumn).when(mockUpdateColumn).validate(); commandLine.handleCommandLine(); verify(mockUpdateColumn).handle(); @@ -275,14 +277,13 @@ void testUpdateColumnName() { doReturn(mockUpdateName) .when(commandLine) .newUpdateColumnName( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "renamed"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("renamed")); doReturn(mockUpdateName).when(mockUpdateName).validate(); commandLine.handleCommandLine(); verify(mockUpdateName).handle(); @@ -308,14 +309,13 @@ void testUpdateColumnDatatype() { doReturn(mockUpdateDatatype) .when(commandLine) .newUpdateColumnDatatype( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "varchar(250)"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("varchar(250)")); doReturn(mockUpdateDatatype).when(mockUpdateDatatype).validate(); commandLine.handleCommandLine(); verify(mockUpdateDatatype).handle(); @@ -341,14 +341,13 @@ void testUpdateColumnPosition() { doReturn(mockUpdatePosition) .when(commandLine) .newUpdateColumnPosition( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "first"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("first")); doReturn(mockUpdatePosition).when(mockUpdatePosition).validate(); commandLine.handleCommandLine(); verify(mockUpdatePosition).handle(); @@ -373,14 +372,13 @@ void testUpdateColumnNullability() { doReturn(mockUpdateNull) .when(commandLine) .newUpdateColumnNullability( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - true); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + anyBoolean()); doReturn(mockUpdateNull).when(mockUpdateNull).validate(); commandLine.handleCommandLine(); verify(mockUpdateNull).handle(); @@ -405,14 +403,13 @@ void testUpdateColumnAutoIncrement() { doReturn(mockUpdateAuto) .when(commandLine) .newUpdateColumnAutoIncrement( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - true); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + anyBoolean()); doReturn(mockUpdateAuto).when(mockUpdateAuto).validate(); commandLine.handleCommandLine(); verify(mockUpdateAuto).handle(); @@ -440,15 +437,14 @@ void testUpdateColumnDefault() { doReturn(mockUpdateDefault) .when(commandLine) .newUpdateColumnDefault( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - "name", - "Fred Smith", - "varchar(100)"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + eq("name"), + eq("Fred Smith"), + eq("varchar(100)")); doReturn(mockUpdateDefault).when(mockUpdateDefault).validate(); commandLine.handleCommandLine(); verify(mockUpdateDefault).handle(); @@ -457,18 +453,12 @@ void testUpdateColumnDefault() { @Test void testUpdateColumnDefaultWithoutDataType() { Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn(GravitinoCommandLine.DEFAULT_URL); UpdateColumnDefault spyUpdate = spy( new UpdateColumnDefault( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "user", - "name", - "", - null)); + mockContext, "metalake_demo", "catalog", "schema", "user", "name", "", null)); assertThrows(RuntimeException.class, spyUpdate::validate); verify(spyUpdate, never()).handle(); @@ -491,7 +481,7 @@ void testDeleteColumnCommandWithoutCatalog() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newDeleteColumn( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", null, null, null, null); + any(CommandContext.class), eq("metalake_demo"), isNull(), isNull(), isNull(), isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -523,7 +513,12 @@ void testDeleteColumnCommandWithoutSchema() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newDeleteColumn( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", null, null, null); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + isNull(), + isNull(), + isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -552,13 +547,12 @@ void testDeleteColumnCommandWithoutTable() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newDeleteColumn( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - null, - null); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + isNull(), + isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -584,13 +578,12 @@ void testDeleteColumnCommandWithoutColumn() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newDeleteColumn( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "schema", - "users", - null); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("schema"), + eq("users"), + isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -615,7 +608,7 @@ void testListColumnCommandWithoutCatalog() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newListColumns( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema", null); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("schema"), isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -644,7 +637,7 @@ void testListColumnCommandWithoutSchema() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newListColumns( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema", null); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("schema"), isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -670,7 +663,7 @@ void testListColumnCommandWithoutTable() { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newListColumns( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema", null); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("schema"), isNull()); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output,