This is an automated email from the ASF dual-hosted git repository. jmclean pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push: new 1e85c3d333 [#6414] improve(CLI): Add catalog command context CLI (#6425) 1e85c3d333 is described below commit 1e85c3d3334f52646ee8b71512fb148743bed52f Author: Lord of Abyss <103809695+abyss-l...@users.noreply.github.com> AuthorDate: Tue Feb 11 12:40:57 2025 +0800 [#6414] improve(CLI): Add catalog command context CLI (#6425) ### What changes were proposed in this pull request? Add catalog command context CLI ### Why are the changes needed? Fix: #6414 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? local test. --- .../gravitino/cli/CatalogCommandHandler.java | 50 ++++---- .../apache/gravitino/cli/GravitinoCommandLine.java | 2 +- .../apache/gravitino/cli/TestableCommandLine.java | 51 ++++----- .../gravitino/cli/commands/CatalogAudit.java | 8 +- .../gravitino/cli/commands/CatalogDetails.java | 11 +- .../gravitino/cli/commands/CatalogDisable.java | 10 +- .../gravitino/cli/commands/CatalogEnable.java | 10 +- .../gravitino/cli/commands/CreateCatalog.java | 11 +- .../gravitino/cli/commands/DeleteCatalog.java | 16 ++- .../cli/commands/ListCatalogProperties.java | 9 +- .../gravitino/cli/commands/ListCatalogs.java | 9 +- .../cli/commands/RemoveCatalogProperty.java | 10 +- .../gravitino/cli/commands/SetCatalogProperty.java | 15 +-- .../cli/commands/UpdateCatalogComment.java | 10 +- .../gravitino/cli/commands/UpdateCatalogName.java | 11 +- .../apache/gravitino/cli/TestCatalogCommands.java | 126 ++++++++------------- .../apache/gravitino/cli/TestCommandContext.java | 46 ++++++++ 17 files changed, 199 insertions(+), 206 deletions(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java index 8e23840685..a702856f65 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java @@ -33,12 +33,10 @@ public class CatalogCommandHandler 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 String catalog; - private final String outputFormat; /** * Constructs a {@link CatalogCommandHandler} instance. @@ -46,19 +44,21 @@ public class CatalogCommandHandler 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 CatalogCommandHandler( - 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.outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); } /** Handles the command execution logic based on the provided command. */ @@ -126,12 +126,9 @@ public class CatalogCommandHandler extends CommandHandler { /** Handles the "DETAILS" command. */ private void handleDetailsCommand() { if (line.hasOption(GravitinoOptions.AUDIT)) { - gravitinoCommandLine.newCatalogAudit(url, ignore, metalake, catalog).validate().handle(); + gravitinoCommandLine.newCatalogAudit(context, metalake, catalog).validate().handle(); } else { - gravitinoCommandLine - .newCatalogDetails(url, ignore, outputFormat, metalake, catalog) - .validate() - .handle(); + gravitinoCommandLine.newCatalogDetails(context, metalake, catalog).validate().handle(); } } @@ -143,18 +140,14 @@ public class CatalogCommandHandler extends CommandHandler { Map<String, String> propertyMap = new Properties().parse(properties); gravitinoCommandLine - .newCreateCatalog(url, ignore, metalake, catalog, provider, comment, propertyMap) + .newCreateCatalog(context, metalake, catalog, provider, comment, propertyMap) .validate() .handle(); } /** Handles the "DELETE" command. */ private void handleDeleteCommand() { - boolean force = line.hasOption(GravitinoOptions.FORCE); - gravitinoCommandLine - .newDeleteCatalog(url, ignore, force, metalake, catalog) - .validate() - .handle(); + gravitinoCommandLine.newDeleteCatalog(context, metalake, catalog).validate().handle(); } /** Handles the "SET" command. */ @@ -162,7 +155,7 @@ public class CatalogCommandHandler extends CommandHandler { String property = line.getOptionValue(GravitinoOptions.PROPERTY); String value = line.getOptionValue(GravitinoOptions.VALUE); gravitinoCommandLine - .newSetCatalogProperty(url, ignore, metalake, catalog, property, value) + .newSetCatalogProperty(context, metalake, catalog, property, value) .validate() .handle(); } @@ -171,17 +164,14 @@ public class CatalogCommandHandler extends CommandHandler { private void handleRemoveCommand() { String property = line.getOptionValue(GravitinoOptions.PROPERTY); gravitinoCommandLine - .newRemoveCatalogProperty(url, ignore, metalake, catalog, property) + .newRemoveCatalogProperty(context, metalake, catalog, property) .validate() .handle(); } /** Handles the "PROPERTIES" command. */ private void handlePropertiesCommand() { - gravitinoCommandLine - .newListCatalogProperties(url, ignore, metalake, catalog) - .validate() - .handle(); + gravitinoCommandLine.newListCatalogProperties(context, metalake, catalog).validate().handle(); } /** Handles the "UPDATE" command. */ @@ -193,25 +183,25 @@ public class CatalogCommandHandler extends CommandHandler { if (line.hasOption(GravitinoOptions.ENABLE)) { boolean enableMetalake = line.hasOption(GravitinoOptions.ALL); gravitinoCommandLine - .newCatalogEnable(url, ignore, metalake, catalog, enableMetalake) + .newCatalogEnable(context, metalake, catalog, enableMetalake) .validate() .handle(); } if (line.hasOption(GravitinoOptions.DISABLE)) { - gravitinoCommandLine.newCatalogDisable(url, ignore, metalake, catalog).validate().handle(); + gravitinoCommandLine.newCatalogDisable(context, metalake, catalog).validate().handle(); } if (line.hasOption(GravitinoOptions.COMMENT)) { String updateComment = line.getOptionValue(GravitinoOptions.COMMENT); gravitinoCommandLine - .newUpdateCatalogComment(url, ignore, metalake, catalog, updateComment) + .newUpdateCatalogComment(context, metalake, catalog, updateComment) .validate() .handle(); } if (line.hasOption(GravitinoOptions.RENAME)) { String newName = line.getOptionValue(GravitinoOptions.RENAME); gravitinoCommandLine - .newUpdateCatalogName(url, ignore, metalake, catalog, newName) + .newUpdateCatalogName(context, metalake, catalog, newName) .validate() .handle(); } @@ -219,6 +209,6 @@ public class CatalogCommandHandler extends CommandHandler { /** Handles the "LIST" command. */ private void handleListCommand() { - gravitinoCommandLine.newListCatalogs(url, ignore, outputFormat, metalake).validate().handle(); + gravitinoCommandLine.newListCatalogs(context, metalake).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 5ab3ed5c93..97fca069a9 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 @@ -122,7 +122,7 @@ public class GravitinoCommandLine extends TestableCommandLine { } else if (entity.equals(CommandEntities.SCHEMA)) { new SchemaCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.CATALOG)) { - new CatalogCommandHandler(this, line, command, ignore).handle(); + new CatalogCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.METALAKE)) { new MetalakeCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.TOPIC)) { 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 de2583f698..d1c757345a 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 @@ -203,60 +203,57 @@ public class TestableCommandLine { return new UpdateMetalakeName(context, metalake, newName); } - protected CatalogAudit newCatalogAudit( - String url, boolean ignore, String metalake, String catalog) { - return new CatalogAudit(url, ignore, metalake, catalog); + protected CatalogAudit newCatalogAudit(CommandContext context, String metalake, String catalog) { + return new CatalogAudit(context, metalake, catalog); } protected CatalogDetails newCatalogDetails( - String url, boolean ignore, String outputFormat, String metalake, String catalog) { - return new CatalogDetails(url, ignore, outputFormat, metalake, catalog); + CommandContext context, String metalake, String catalog) { + return new CatalogDetails(context, metalake, catalog); } - protected ListCatalogs newListCatalogs( - String url, boolean ignore, String outputFormat, String metalake) { - return new ListCatalogs(url, ignore, outputFormat, metalake); + protected ListCatalogs newListCatalogs(CommandContext context, String metalake) { + return new ListCatalogs(context, metalake); } protected CreateCatalog newCreateCatalog( - String url, - boolean ignore, + CommandContext context, String metalake, String catalog, String provider, String comment, Map<String, String> properties) { - return new CreateCatalog(url, ignore, metalake, catalog, provider, comment, properties); + return new CreateCatalog(context, metalake, catalog, provider, comment, properties); } protected DeleteCatalog newDeleteCatalog( - String url, boolean ignore, boolean force, String metalake, String catalog) { - return new DeleteCatalog(url, ignore, force, metalake, catalog); + CommandContext context, String metalake, String catalog) { + return new DeleteCatalog(context, metalake, catalog); } protected SetCatalogProperty newSetCatalogProperty( - String url, boolean ignore, String metalake, String catalog, String property, String value) { - return new SetCatalogProperty(url, ignore, metalake, catalog, property, value); + CommandContext context, String metalake, String catalog, String property, String value) { + return new SetCatalogProperty(context, metalake, catalog, property, value); } protected RemoveCatalogProperty newRemoveCatalogProperty( - String url, boolean ignore, String metalake, String catalog, String property) { - return new RemoveCatalogProperty(url, ignore, metalake, catalog, property); + CommandContext context, String metalake, String catalog, String property) { + return new RemoveCatalogProperty(context, metalake, catalog, property); } protected ListCatalogProperties newListCatalogProperties( - String url, boolean ignore, String metalake, String catalog) { - return new ListCatalogProperties(url, ignore, metalake, catalog); + CommandContext context, String metalake, String catalog) { + return new ListCatalogProperties(context, metalake, catalog); } protected UpdateCatalogComment newUpdateCatalogComment( - String url, boolean ignore, String metalake, String catalog, String comment) { - return new UpdateCatalogComment(url, ignore, metalake, catalog, comment); + CommandContext context, String metalake, String catalog, String comment) { + return new UpdateCatalogComment(context, metalake, catalog, comment); } protected UpdateCatalogName newUpdateCatalogName( - String url, boolean ignore, String metalake, String catalog, String newName) { - return new UpdateCatalogName(url, ignore, metalake, catalog, newName); + CommandContext context, String metalake, String catalog, String newName) { + return new UpdateCatalogName(context, metalake, catalog, newName); } protected SchemaAudit newSchemaAudit( @@ -892,13 +889,13 @@ public class TestableCommandLine { } protected CatalogEnable newCatalogEnable( - String url, boolean ignore, String metalake, String catalog, boolean enableMetalake) { - return new CatalogEnable(url, ignore, metalake, catalog, enableMetalake); + CommandContext context, String metalake, String catalog, boolean enableMetalake) { + return new CatalogEnable(context, metalake, catalog, enableMetalake); } protected CatalogDisable newCatalogDisable( - String url, boolean ignore, String metalake, String catalog) { - return new CatalogDisable(url, ignore, metalake, catalog); + CommandContext context, String metalake, String catalog) { + return new CatalogDisable(context, metalake, catalog); } protected ListModel newListModel( diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java index f1ea8ac7b5..6600e1d429 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.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; @@ -33,13 +34,12 @@ public class CatalogAudit extends AuditCommand { /** * Displays the audit information of a catalog. * - * @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. */ - public CatalogAudit(String url, boolean ignoreVersions, String metalake, String catalog) { - super(url, ignoreVersions); + public CatalogAudit(CommandContext context, String metalake, String catalog) { + super(context); this.metalake = metalake; this.catalog = catalog; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDetails.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDetails.java index a204f560d0..fac504a008 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDetails.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDetails.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; @@ -33,16 +34,12 @@ public class CatalogDetails extends Command { /** * Displays the name and comment of a catalog. * - * @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 context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. */ - public CatalogDetails( - String url, boolean ignoreVersions, String outputFormat, String metalake, String catalog) { - - super(url, ignoreVersions, outputFormat); + public CatalogDetails(CommandContext context, String metalake, String catalog) { + super(context); this.metalake = metalake; this.catalog = catalog; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java index 620a4291ee..7a9954b1ee 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java @@ -18,6 +18,7 @@ */ package org.apache.gravitino.cli.commands; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -32,13 +33,12 @@ public class CatalogDisable extends Command { /** * Disable catalog * - * @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. */ - public CatalogDisable(String url, boolean ignoreVersions, String metalake, String catalog) { - super(url, ignoreVersions); + public CatalogDisable(CommandContext context, String metalake, String catalog) { + super(context); this.metalake = metalake; this.catalog = catalog; } @@ -57,6 +57,6 @@ public class CatalogDisable extends Command { exitWithError(exp.getMessage()); } - System.out.println(metalake + "." + catalog + " has been disabled."); + printInformation(metalake + "." + catalog + " has been disabled."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java index 8646baee29..8c5ca51354 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java @@ -18,6 +18,7 @@ */ package org.apache.gravitino.cli.commands; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.client.GravitinoClient; @@ -34,15 +35,14 @@ public class CatalogEnable extends Command { /** * Enable catalog * - * @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 enableMetalake Whether to enable it's metalake */ public CatalogEnable( - String url, boolean ignoreVersions, String metalake, String catalog, boolean enableMetalake) { - super(url, ignoreVersions); + CommandContext context, String metalake, String catalog, boolean enableMetalake) { + super(context); this.metalake = metalake; this.catalog = catalog; this.enableMetalake = enableMetalake; @@ -69,6 +69,6 @@ public class CatalogEnable extends Command { exitWithError(exp.getMessage()); } - System.out.println(metalake + "." + catalog + " has been enabled."); + printInformation(metalake + "." + catalog + " has been enabled."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateCatalog.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateCatalog.java index 2870dd7103..af90cda73b 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateCatalog.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateCatalog.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import java.util.Map; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.cli.Providers; import org.apache.gravitino.client.GravitinoClient; @@ -36,8 +37,7 @@ public class CreateCatalog extends Command { /** * Create a new catalog. * - * @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 provider The provider/type of catalog. @@ -45,14 +45,13 @@ public class CreateCatalog extends Command { * @param properties The catalog's properties. */ public CreateCatalog( - String url, - boolean ignoreVersions, + CommandContext context, String metalake, String catalog, String provider, String comment, Map<String, String> properties) { - super(url, ignoreVersions); + super(context); this.metalake = metalake; this.catalog = catalog; this.provider = provider; @@ -79,7 +78,7 @@ public class CreateCatalog extends Command { exitWithError(exp.getMessage()); } - System.out.println(catalog + " catalog created"); + printInformation(catalog + " catalog created"); } @Override diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java index 7cb9bf7d9c..60d49772ca 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.cli.AreYouSure; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.CatalogInUseException; @@ -35,16 +36,13 @@ public class DeleteCatalog extends Command { /** * Delete a catalog. * - * @param url The URL of the Gravitino server. - * @param ignoreVersions If true don't check the client/server versions match. - * @param force Force operation. + * @param context The command context. * @param metalake The name of the metalake. * @param catalog The name of the catalog. */ - public DeleteCatalog( - String url, boolean ignoreVersions, boolean force, String metalake, String catalog) { - super(url, ignoreVersions); - this.force = force; + public DeleteCatalog(CommandContext context, String metalake, String catalog) { + super(context); + this.force = context.force(); this.metalake = metalake; this.catalog = catalog; } @@ -72,9 +70,9 @@ public class DeleteCatalog extends Command { } if (deleted) { - System.out.println(catalog + " deleted."); + printInformation(catalog + " deleted."); } else { - System.out.println(catalog + " not deleted."); + printInformation(catalog + " not deleted."); } } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogProperties.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogProperties.java index f94213eef4..54312b8e43 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogProperties.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogProperties.java @@ -21,6 +21,7 @@ package org.apache.gravitino.cli.commands; import java.util.Map; 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; @@ -35,14 +36,12 @@ public class ListCatalogProperties extends ListProperties { /** * List the properties of a catalog. * - * @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. */ - public ListCatalogProperties( - String url, boolean ignoreVersions, String metalake, String catalog) { - super(url, ignoreVersions); + public ListCatalogProperties(CommandContext context, String metalake, String catalog) { + super(context); this.metalake = metalake; this.catalog = catalog; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogs.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogs.java index e6aaf811ec..ad8d171fec 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogs.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogs.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.NoSuchMetalakeException; @@ -32,13 +33,11 @@ public class ListCatalogs extends Command { /** * Lists all catalogs in a metalake. * - * @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 context The command context. * @param metalake The name of the metalake. */ - public ListCatalogs(String url, boolean ignoreVersions, String outputFormat, String metalake) { - super(url, ignoreVersions, outputFormat); + public ListCatalogs(CommandContext context, String metalake) { + super(context); this.metalake = metalake; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveCatalogProperty.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveCatalogProperty.java index dc1a76765b..dd22b583e2 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveCatalogProperty.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveCatalogProperty.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.CatalogChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -35,15 +36,14 @@ public class RemoveCatalogProperty extends Command { /** * Remove a property of a catalog. * - * @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 property The name of the property. */ public RemoveCatalogProperty( - String url, boolean ignoreVersions, String metalake, String catalog, String property) { - super(url, ignoreVersions); + CommandContext context, String metalake, String catalog, String property) { + super(context); this.metalake = metalake; this.catalog = catalog; this.property = property; @@ -64,7 +64,7 @@ public class RemoveCatalogProperty extends Command { exitWithError(exp.getMessage()); } - System.out.println(property + " property removed."); + printInformation(property + " property removed."); } @Override diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetCatalogProperty.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetCatalogProperty.java index 034b1b8e2a..a255835f43 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetCatalogProperty.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetCatalogProperty.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.CatalogChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -36,21 +37,15 @@ public class SetCatalogProperty extends Command { /** * Set a property of a catalog. * - * @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 property The name of the property. * @param value The value of the property. */ public SetCatalogProperty( - String url, - boolean ignoreVersions, - String metalake, - String catalog, - String property, - String value) { - super(url, ignoreVersions); + CommandContext context, String metalake, String catalog, String property, String value) { + super(context); this.metalake = metalake; this.catalog = catalog; this.property = property; @@ -72,7 +67,7 @@ public class SetCatalogProperty extends Command { exitWithError(exp.getMessage()); } - System.out.println(catalog + " property set."); + printInformation(catalog + " property set."); } @Override diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogComment.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogComment.java index ed12dbc7ca..b058bf32f5 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogComment.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogComment.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.CatalogChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -35,15 +36,14 @@ public class UpdateCatalogComment extends Command { /** * Update the comment of a catalog. * - * @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 comment New metalake comment. */ public UpdateCatalogComment( - String url, boolean ignoreVersions, String metalake, String catalog, String comment) { - super(url, ignoreVersions); + CommandContext context, String metalake, String catalog, String comment) { + super(context); this.metalake = metalake; this.catalog = catalog; this.comment = comment; @@ -64,6 +64,6 @@ public class UpdateCatalogComment extends Command { exitWithError(exp.getMessage()); } - System.out.println(catalog + " comment changed."); + printInformation(catalog + " comment changed."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogName.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogName.java index 8d4fcb60b9..ab7a1db4e5 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogName.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateCatalogName.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.CatalogChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchCatalogException; @@ -35,15 +36,13 @@ public class UpdateCatalogName extends Command { /** * Update the name of a catalog. * - * @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 name The new catalog name. */ - public UpdateCatalogName( - String url, boolean ignoreVersions, String metalake, String catalog, String name) { - super(url, ignoreVersions); + public UpdateCatalogName(CommandContext context, String metalake, String catalog, String name) { + super(context); this.metalake = metalake; this.catalog = catalog; this.name = name; @@ -64,6 +63,6 @@ public class UpdateCatalogName extends Command { exitWithError(exp.getMessage()); } - System.out.println(catalog + " name changed."); + printInformation(catalog + " name changed."); } } diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java index afa19b94c5..26639368a6 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java @@ -22,6 +22,10 @@ package org.apache.gravitino.cli; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -39,7 +43,6 @@ import org.apache.gravitino.cli.commands.CatalogAudit; import org.apache.gravitino.cli.commands.CatalogDetails; import org.apache.gravitino.cli.commands.CatalogDisable; import org.apache.gravitino.cli.commands.CatalogEnable; -import org.apache.gravitino.cli.commands.Command; import org.apache.gravitino.cli.commands.CreateCatalog; import org.apache.gravitino.cli.commands.DeleteCatalog; import org.apache.gravitino.cli.commands.ListCatalogProperties; @@ -69,11 +72,6 @@ class TestCatalogCommands { System.setErr(new PrintStream(errContent)); } - @AfterEach - void restoreExitFlg() { - Main.useExit = true; - } - @AfterEach public void restoreStreams() { System.setOut(originalOut); @@ -91,7 +89,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.LIST)); doReturn(mockList) .when(commandLine) - .newListCatalogs(GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo"); + .newListCatalogs(any(CommandContext.class), eq("metalake_demo")); doReturn(mockList).when(mockList).validate(); commandLine.handleCommandLine(); verify(mockList).handle(); @@ -111,8 +109,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.DETAILS)); doReturn(mockDetails) .when(commandLine) - .newCatalogDetails( - GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo", "catalog"); + .newCatalogDetails(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockDetails).when(mockDetails).validate(); commandLine.handleCommandLine(); verify(mockDetails).handle(); @@ -132,7 +129,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.DETAILS)); doReturn(mockAudit) .when(commandLine) - .newCatalogAudit(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog"); + .newCatalogAudit(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockAudit).when(mockAudit).validate(); commandLine.handleCommandLine(); verify(mockAudit).handle(); @@ -163,13 +160,17 @@ class TestCatalogCommands { doReturn(mockCreate) .when(commandLine) .newCreateCatalog( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "postgres", - "comment", - map); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("postgres"), + eq("comment"), + argThat( + stringStringMap -> + stringStringMap.containsKey("key1") + && stringStringMap.get("key1").equals(map.get("key1")) + && stringStringMap.containsKey("key2") + && stringStringMap.get("key2").equals("value2"))); doReturn(mockCreate).when(mockCreate).validate(); commandLine.handleCommandLine(); verify(mockCreate).handle(); @@ -178,16 +179,10 @@ class TestCatalogCommands { @Test void testCreateCatalogCommandWithoutProvider() { Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn("http://localhost:8080"); CreateCatalog mockCreateCatalog = - spy( - new CreateCatalog( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - null, - "comment", - null)); + spy(new CreateCatalog(mockContext, "metalake_demo", "catalog", null, "comment", null)); assertThrows(RuntimeException.class, mockCreateCatalog::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -207,8 +202,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteCatalog( - GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo", "catalog"); + .newDeleteCatalog(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockDelete).when(mockDelete).validate(); commandLine.handleCommandLine(); verify(mockDelete).handle(); @@ -228,8 +222,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteCatalog( - GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo", "catalog"); + .newDeleteCatalog(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockDelete).when(mockDelete).validate(); commandLine.handleCommandLine(); verify(mockDelete).handle(); @@ -253,12 +246,11 @@ class TestCatalogCommands { doReturn(mockSetProperty) .when(commandLine) .newSetCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "property", - "value"); + any(CommandContext.class), + eq("metalake_demo"), + eq("catalog"), + eq("property"), + eq("value")); doReturn(mockSetProperty).when(mockSetProperty).validate(); commandLine.handleCommandLine(); verify(mockSetProperty).handle(); @@ -267,10 +259,10 @@ class TestCatalogCommands { @Test void testSetCatalogPropertyCommandWithoutPropertyAndValue() { Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn("http://localhost:8080"); SetCatalogProperty mockSetProperty = - spy( - new SetCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", null, null)); + spy(new SetCatalogProperty(mockContext, "metalake_demo", "catalog", null, null)); assertThrows(RuntimeException.class, mockSetProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -280,15 +272,10 @@ class TestCatalogCommands { @Test void testSetCatalogPropertyCommandWithoutProperty() { Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn("http://localhost:8080"); SetCatalogProperty mockSetProperty = - spy( - new SetCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - null, - "value")); + spy(new SetCatalogProperty(mockContext, "metalake_demo", "catalog", null, "value")); assertThrows(RuntimeException.class, mockSetProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -297,16 +284,10 @@ class TestCatalogCommands { @Test void testSetCatalogPropertyCommandWithoutValue() { - Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn("http://localhost:8080"); SetCatalogProperty mockSetProperty = - spy( - new SetCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, - false, - "metalake_demo", - "catalog", - "property", - null)); + spy(new SetCatalogProperty(mockContext, "metalake_demo", "catalog", "property", null)); assertThrows(RuntimeException.class, mockSetProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -329,7 +310,7 @@ class TestCatalogCommands { doReturn(mockRemoveProperty) .when(commandLine) .newRemoveCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "property"); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("property")); doReturn(mockRemoveProperty).when(mockRemoveProperty).validate(); commandLine.handleCommandLine(); verify(mockRemoveProperty).handle(); @@ -337,11 +318,10 @@ class TestCatalogCommands { @Test void testRemoveCatalogPropertyCommandWithoutProperty() { - Main.useExit = false; + CommandContext mockContext = mock(CommandContext.class); + when(mockContext.url()).thenReturn("http://localhost:8080"); RemoveCatalogProperty mockRemoveProperty = - spy( - new RemoveCatalogProperty( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", null)); + spy(new RemoveCatalogProperty(mockContext, "metalake_demo", "catalog", null)); assertThrows(RuntimeException.class, mockRemoveProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -361,8 +341,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.PROPERTIES)); doReturn(mockListProperties) .when(commandLine) - .newListCatalogProperties( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog"); + .newListCatalogProperties(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockListProperties).when(mockListProperties).validate(); commandLine.handleCommandLine(); verify(mockListProperties).handle(); @@ -384,7 +363,7 @@ class TestCatalogCommands { doReturn(mockUpdateComment) .when(commandLine) .newUpdateCatalogComment( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "new comment"); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("new comment")); doReturn(mockUpdateComment).when(mockUpdateComment).validate(); commandLine.handleCommandLine(); verify(mockUpdateComment).handle(); @@ -407,7 +386,7 @@ class TestCatalogCommands { doReturn(mockUpdateName) .when(commandLine) .newUpdateCatalogName( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "new_name"); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), eq("new_name")); doReturn(mockUpdateName).when(mockUpdateName).validate(); commandLine.handleCommandLine(); verify(mockUpdateName).handle(); @@ -428,12 +407,7 @@ class TestCatalogCommands { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) - .newCatalogDetails( - GravitinoCommandLine.DEFAULT_URL, - false, - Command.OUTPUT_FORMAT_TABLE, - "metalake_demo", - "catalog"); + .newCatalogDetails(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); assertEquals( output, @@ -459,7 +433,7 @@ class TestCatalogCommands { doReturn(mockEnable) .when(commandLine) .newCatalogEnable( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), anyBoolean()); doReturn(mockEnable).when(mockEnable).validate(); commandLine.handleCommandLine(); verify(mockEnable).handle(); @@ -482,7 +456,7 @@ class TestCatalogCommands { doReturn(mockEnable) .when(commandLine) .newCatalogEnable( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", true); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), anyBoolean()); doReturn(mockEnable).when(mockEnable).validate(); commandLine.handleCommandLine(); verify(mockEnable).handle(); @@ -503,7 +477,7 @@ class TestCatalogCommands { mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE)); doReturn(mockDisable) .when(commandLine) - .newCatalogDisable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog"); + .newCatalogDisable(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); doReturn(mockDisable).when(mockDisable).validate(); commandLine.handleCommandLine(); verify(mockDisable).handle(); @@ -528,9 +502,9 @@ class TestCatalogCommands { assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) .newCatalogEnable( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false); + any(CommandContext.class), eq("metalake_demo"), eq("catalog"), anyBoolean()); verify(commandLine, never()) - .newCatalogDisable(GravitinoCommandLine.DEFAULT_URL, false, "melake_demo", "catalog"); + .newCatalogDisable(any(CommandContext.class), eq("metalake_demo"), eq("catalog")); assertTrue(errContent.toString().contains(ErrorMessages.INVALID_ENABLE_DISABLE)); } } diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCommandContext.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCommandContext.java new file mode 100644 index 0000000000..8919446826 --- /dev/null +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCommandContext.java @@ -0,0 +1,46 @@ +/* + * 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; + +import org.apache.gravitino.cli.commands.Command; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestCommandContext { + + @Test + public void testCreateCommandContextWithDefaults() { + CommandContext commandContext = new CommandContext("http://localhost:8080", false); + Assertions.assertEquals("http://localhost:8080", commandContext.url()); + Assertions.assertFalse(commandContext.ignoreVersions()); + Assertions.assertFalse(commandContext.force()); + Assertions.assertEquals(Command.OUTPUT_FORMAT_PLAIN, commandContext.outputFormat()); + } + + @Test + public void testCreateCommandContextWithCustomValues() { + CommandContext commandContext = + new CommandContext("http://localhost:8080", true, true, Command.OUTPUT_FORMAT_TABLE); + Assertions.assertEquals("http://localhost:8080", commandContext.url()); + Assertions.assertTrue(commandContext.ignoreVersions()); + Assertions.assertTrue(commandContext.force()); + Assertions.assertEquals(Command.OUTPUT_FORMAT_TABLE, commandContext.outputFormat()); + } +}