From 71540e358ca2dcd910617164d127940bdfd76e9d Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Wed, 22 Jan 2025 14:19:00 +1100 Subject: [PATCH 1/2] refactor to add a command context --- .../gravitino/cli/GravitinoCommandLine.java | 6 +- .../gravitino/cli/MetalakeCommandHandler.java | 54 ++++++-------- .../gravitino/cli/TestableCommandLine.java | 50 +++++++------ .../cli/commands/AllMetalakeDetails.java | 10 +-- .../gravitino/cli/commands/AuditCommand.java | 8 ++- .../gravitino/cli/commands/Command.java | 35 ++++++++++ .../cli/commands/CreateMetalake.java | 10 +-- .../cli/commands/DeleteMetalake.java | 15 ++-- .../gravitino/cli/commands/DeleteRole.java | 4 +- .../cli/commands/ListMetalakeProperties.java | 8 +-- .../gravitino/cli/commands/ListMetalakes.java | 9 ++- .../cli/commands/ListProperties.java | 10 +++ .../cli/commands/MetadataCommand.java | 10 +++ .../gravitino/cli/commands/MetalakeAudit.java | 8 +-- .../cli/commands/MetalakeDetails.java | 9 ++- .../cli/commands/MetalakeDisable.java | 10 +-- .../cli/commands/MetalakeEnable.java | 11 ++- .../cli/commands/RemoveMetalakeProperty.java | 11 ++- .../cli/commands/SetMetalakeProperty.java | 10 +-- .../cli/commands/UpdateMetalakeComment.java | 11 ++- .../cli/commands/UpdateMetalakeName.java | 14 ++-- .../gravitino/cli/TestMetalakeCommands.java | 70 +++++++++---------- 22 files changed, 211 insertions(+), 172 deletions(-) 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 d7e257a8a81..1923562e1f4 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 @@ -107,6 +107,10 @@ public static void displayHelp(Options options) { /** Executes the appropriate command based on the command type. */ private void executeCommand() { + boolean force = line.hasOption(GravitinoOptions.FORCE); + String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); + CommandContext context = new CommandContext(null, ignore, force, outputFormat); + if (CommandActions.HELP.equals(command)) { handleHelpCommand(); } else if (line.hasOption(GravitinoOptions.OWNER)) { @@ -120,7 +124,7 @@ private void executeCommand() { } else if (entity.equals(CommandEntities.CATALOG)) { new CatalogCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.METALAKE)) { - new MetalakeCommandHandler(this, line, command, ignore).handle(); + new MetalakeCommandHandler(this, line, command, context).handle(); } else if (entity.equals(CommandEntities.TOPIC)) { new TopicCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.FILESET)) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java index 993116f19f5..7d396838421 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java @@ -29,9 +29,8 @@ public class MetalakeCommandHandler extends CommandHandler { private final GravitinoCommandLine gravitinoCommandLine; private final CommandLine line; + private final CommandContext context; private final String command; - private final boolean ignore; - private final String url; private String metalake; /** @@ -40,15 +39,18 @@ public class MetalakeCommandHandler 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 MetalakeCommandHandler( - 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.url = getUrl(line); + this.context = context; + this.context.setUrl(getUrl(line)); } /** Handles the command execution logic based on the provided command. */ @@ -112,33 +114,27 @@ private boolean executeCommand() { /** Handles the "LIST" command. */ private void handleListCommand() { - String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); - gravitinoCommandLine.newListMetalakes(url, ignore, outputFormat).validate().handle(); + gravitinoCommandLine.newListMetalakes(context).validate().handle(); } /** Handles the "DETAILS" command. */ private void handleDetailsCommand() { if (line.hasOption(GravitinoOptions.AUDIT)) { - gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle(); + gravitinoCommandLine.newMetalakeAudit(context, metalake).validate().handle(); } else { - String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); - gravitinoCommandLine - .newMetalakeDetails(url, ignore, outputFormat, metalake) - .validate() - .handle(); + gravitinoCommandLine.newMetalakeDetails(context, metalake).validate().handle(); } } /** Handles the "CREATE" command. */ private void handleCreateCommand() { String comment = line.getOptionValue(GravitinoOptions.COMMENT); - gravitinoCommandLine.newCreateMetalake(url, ignore, metalake, comment).validate().handle(); + gravitinoCommandLine.newCreateMetalake(context, metalake, comment).validate().handle(); } /** Handles the "DELETE" command. */ private void handleDeleteCommand() { - boolean force = line.hasOption(GravitinoOptions.FORCE); - gravitinoCommandLine.newDeleteMetalake(url, ignore, force, metalake).validate().handle(); + gravitinoCommandLine.newDeleteMetalake(context, metalake).validate().handle(); } /** Handles the "SET" command. */ @@ -146,7 +142,7 @@ private void handleSetCommand() { String property = line.getOptionValue(GravitinoOptions.PROPERTY); String value = line.getOptionValue(GravitinoOptions.VALUE); gravitinoCommandLine - .newSetMetalakeProperty(url, ignore, metalake, property, value) + .newSetMetalakeProperty(context, metalake, property, value) .validate() .handle(); } @@ -154,15 +150,12 @@ private void handleSetCommand() { /** Handles the "REMOVE" command. */ private void handleRemoveCommand() { String property = line.getOptionValue(GravitinoOptions.PROPERTY); - gravitinoCommandLine - .newRemoveMetalakeProperty(url, ignore, metalake, property) - .validate() - .handle(); + gravitinoCommandLine.newRemoveMetalakeProperty(context, metalake, property).validate().handle(); } /** Handles the "PROPERTIES" command. */ private void handlePropertiesCommand() { - gravitinoCommandLine.newListMetalakeProperties(url, ignore, metalake).validate().handle(); + gravitinoCommandLine.newListMetalakeProperties(context, metalake).validate().handle(); } /** Handles the "UPDATE" command. */ @@ -174,28 +167,21 @@ private void handleUpdateCommand() { if (line.hasOption(GravitinoOptions.ENABLE)) { boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL); gravitinoCommandLine - .newMetalakeEnable(url, ignore, metalake, enableAllCatalogs) + .newMetalakeEnable(context, metalake, enableAllCatalogs) .validate() .handle(); } if (line.hasOption(GravitinoOptions.DISABLE)) { - gravitinoCommandLine.newMetalakeDisable(url, ignore, metalake).validate().handle(); + gravitinoCommandLine.newMetalakeDisable(context, metalake).validate().handle(); } if (line.hasOption(GravitinoOptions.COMMENT)) { String comment = line.getOptionValue(GravitinoOptions.COMMENT); - gravitinoCommandLine - .newUpdateMetalakeComment(url, ignore, metalake, comment) - .validate() - .handle(); + gravitinoCommandLine.newUpdateMetalakeComment(context, metalake, comment).validate().handle(); } if (line.hasOption(GravitinoOptions.RENAME)) { String newName = line.getOptionValue(GravitinoOptions.RENAME); - boolean force = line.hasOption(GravitinoOptions.FORCE); - gravitinoCommandLine - .newUpdateMetalakeName(url, ignore, force, metalake, newName) - .validate() - .handle(); + gravitinoCommandLine.newUpdateMetalakeName(context, metalake, newName).validate().handle(); } } } 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 498a0060cbd..de924e162f5 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 @@ -156,52 +156,50 @@ protected ServerVersion newServerVersion(String url, boolean ignore) { return new ServerVersion(url, ignore); } - protected MetalakeAudit newMetalakeAudit(String url, boolean ignore, String metalake) { - return new MetalakeAudit(url, ignore, metalake); + protected MetalakeAudit newMetalakeAudit(CommandContext context, String metalake) { + return new MetalakeAudit(context, metalake); } - protected MetalakeDetails newMetalakeDetails( - String url, boolean ignore, String outputFormat, String metalake) { - return new MetalakeDetails(url, ignore, outputFormat, metalake); + protected MetalakeDetails newMetalakeDetails(CommandContext context, String metalake) { + return new MetalakeDetails(context, metalake); } - protected ListMetalakes newListMetalakes(String url, boolean ignore, String outputFormat) { - return new ListMetalakes(url, ignore, outputFormat); + protected ListMetalakes newListMetalakes(CommandContext context) { + return new ListMetalakes(context); } protected CreateMetalake newCreateMetalake( - String url, boolean ignore, String metalake, String comment) { - return new CreateMetalake(url, ignore, metalake, comment); + CommandContext context, String metalake, String comment) { + return new CreateMetalake(context, metalake, comment); } - protected DeleteMetalake newDeleteMetalake( - String url, boolean ignore, boolean force, String metalake) { - return new DeleteMetalake(url, ignore, force, metalake); + protected DeleteMetalake newDeleteMetalake(CommandContext context, String metalake) { + return new DeleteMetalake(context, metalake); } protected SetMetalakeProperty newSetMetalakeProperty( - String url, boolean ignore, String metalake, String property, String value) { - return new SetMetalakeProperty(url, ignore, metalake, property, value); + CommandContext context, String metalake, String property, String value) { + return new SetMetalakeProperty(context, metalake, property, value); } protected RemoveMetalakeProperty newRemoveMetalakeProperty( - String url, boolean ignore, String metalake, String property) { - return new RemoveMetalakeProperty(url, ignore, metalake, property); + CommandContext context, String metalake, String property) { + return new RemoveMetalakeProperty(context, metalake, property); } protected ListMetalakeProperties newListMetalakeProperties( - String url, boolean ignore, String metalake) { - return new ListMetalakeProperties(url, ignore, metalake); + CommandContext context, String metalake) { + return new ListMetalakeProperties(context, metalake); } protected UpdateMetalakeComment newUpdateMetalakeComment( - String url, boolean ignore, String metalake, String comment) { - return new UpdateMetalakeComment(url, ignore, metalake, comment); + CommandContext context, String metalake, String comment) { + return new UpdateMetalakeComment(context, metalake, comment); } protected UpdateMetalakeName newUpdateMetalakeName( - String url, boolean ignore, boolean force, String metalake, String newName) { - return new UpdateMetalakeName(url, ignore, force, metalake, newName); + CommandContext context, String metalake, String newName) { + return new UpdateMetalakeName(context, metalake, newName); } protected CatalogAudit newCatalogAudit( @@ -902,12 +900,12 @@ protected RevokePrivilegesFromRole newRevokePrivilegesFromRole( } protected MetalakeEnable newMetalakeEnable( - String url, boolean ignore, String metalake, boolean enableAllCatalogs) { - return new MetalakeEnable(url, ignore, metalake, enableAllCatalogs); + CommandContext context, String metalake, boolean enableAllCatalogs) { + return new MetalakeEnable(context, metalake, enableAllCatalogs); } - protected MetalakeDisable newMetalakeDisable(String url, boolean ignore, String metalake) { - return new MetalakeDisable(url, ignore, metalake); + protected MetalakeDisable newMetalakeDisable(CommandContext context, String metalake) { + return new MetalakeDisable(context, metalake); } protected CatalogEnable newCatalogEnable( diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AllMetalakeDetails.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AllMetalakeDetails.java index b76138cb5c9..cd8deb9c13d 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AllMetalakeDetails.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AllMetalakeDetails.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.gravitino.Metalake; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.client.GravitinoAdminClient; public class AllMetalakeDetails extends Command { @@ -30,11 +31,10 @@ public class AllMetalakeDetails extends Command { /** * Parameters needed to list all metalakes. * - * @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. */ - public AllMetalakeDetails(String url, boolean ignoreVersions) { - super(url, ignoreVersions); + public AllMetalakeDetails(CommandContext context) { + super(context); } /** Displays the name and comment of all metalakes. */ @@ -55,6 +55,6 @@ public void handle() { String all = Joiner.on(System.lineSeparator()).join(metalakeDetails); - System.out.print(all); + printResults(all); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java index 09eee347821..9c27295bddb 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.Audit; +import org.apache.gravitino.cli.CommandContext; public abstract class AuditCommand extends Command { /** @@ -30,6 +31,11 @@ public AuditCommand(String url, boolean ignoreVersions) { super(url, ignoreVersions); } + /** @param context The command context. */ + public AuditCommand(CommandContext context) { + super(context); + } + /* Overridden in parent - do nothing */ @Override public void handle() {} @@ -51,6 +57,6 @@ public void displayAuditInfo(Audit audit) { + "," + audit.lastModifiedTime(); - System.out.println(auditInfo); + printResults(auditInfo); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java index ea6abdd6393..7d259ed18ba 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java @@ -23,6 +23,7 @@ import com.google.common.base.Joiner; import java.io.File; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.cli.GravitinoConfig; import org.apache.gravitino.cli.KerberosData; @@ -49,10 +50,25 @@ public abstract class Command { private static final String SIMPLE_AUTH = "simple"; private static final String OAUTH_AUTH = "oauth"; private static final String KERBEROS_AUTH = "kerberos"; + private final String url; private final boolean ignoreVersions; private final String outputFormat; + protected CommandContext context = null; // TODO make final + + /** + * Command constructor. + * + * @param context The command context. + */ + public Command(CommandContext context) { + this.context = context; + this.url = context.url(); + this.ignoreVersions = context.ignoreVersions(); + this.outputFormat = context.outputFormat(); + } + /** * Command constructor. * @@ -88,6 +104,25 @@ public void exitWithError(String error) { Main.exit(-1); } + /** + * Prints out an informational message, often to indicate a command has finished. + * + * @param message The message to display. + */ + public void printInformation(String message) { + // so that future outoput could be suppressed + System.out.print(message); + } + + /** + * Prints out an a results of a command. + * + * @param results The results to display. + */ + public void printResults(String results) { + System.out.print(results); + } + /** * Sets the authentication mode and user credentials for the command. * diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateMetalake.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateMetalake.java index 9a3c033f028..cdb9472f20a 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateMetalake.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateMetalake.java @@ -19,6 +19,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.exceptions.MetalakeAlreadyExistsException; @@ -30,13 +31,12 @@ public class CreateMetalake extends Command { /** * Create a new metalake. * - * @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 comment The metalake's comment. */ - public CreateMetalake(String url, boolean ignoreVersions, String metalake, String comment) { - super(url, ignoreVersions); + public CreateMetalake(CommandContext context, String metalake, String comment) { + super(context); this.metalake = metalake; this.comment = comment; } @@ -53,6 +53,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(metalake + " created"); + printInformation(metalake + " created"); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java index 3bad108a9ec..4f72058cd14 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.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.GravitinoAdminClient; import org.apache.gravitino.exceptions.MetalakeInUseException; @@ -32,14 +33,12 @@ public class DeleteMetalake extends Command { /** * Delete a metalake. * - * @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. */ - public DeleteMetalake(String url, boolean ignoreVersions, boolean force, String metalake) { - super(url, ignoreVersions); - this.force = force; + public DeleteMetalake(CommandContext context, String metalake) { + super(context); + this.force = context.force(); this.metalake = metalake; } @@ -64,9 +63,9 @@ public void handle() { } if (deleted) { - System.out.println(metalake + " deleted."); + printInformation(metalake + " deleted."); } else { - System.out.println(metalake + " not deleted."); + printInformation(metalake + " not deleted."); } } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteRole.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteRole.java index fa7c8cacc2c..4e0bb508d19 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteRole.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteRole.java @@ -74,9 +74,9 @@ public void handle() { } if (failedRoles.isEmpty()) { - System.out.println(COMMA_JOINER.join(successRoles) + " deleted."); + printInformation(COMMA_JOINER.join(successRoles) + " deleted."); } else { - System.err.println( + printInformation( COMMA_JOINER.join(successRoles) + " deleted, " + "but " diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakeProperties.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakeProperties.java index b7d794d4455..2e85058d123 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakeProperties.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakeProperties.java @@ -21,6 +21,7 @@ import java.util.Map; import org.apache.gravitino.Metalake; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -33,12 +34,11 @@ public class ListMetalakeProperties extends ListProperties { /** * List the properties of a metalake. * - * @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. */ - public ListMetalakeProperties(String url, boolean ignoreVersions, String metalake) { - super(url, ignoreVersions); + public ListMetalakeProperties(CommandContext context, String metalake) { + super(context); this.metalake = metalake; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakes.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakes.java index ee5ac81d646..a97e89bca2e 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakes.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListMetalakes.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.Metalake; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.client.GravitinoAdminClient; /** Lists all metalakes. */ @@ -28,12 +29,10 @@ public class ListMetalakes extends Command { /** * List all metalakes. * - * @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. */ - public ListMetalakes(String url, boolean ignoreVersions, String outputFormat) { - super(url, ignoreVersions, outputFormat); + public ListMetalakes(CommandContext context) { + super(context); } /** Lists all metalakes. */ diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListProperties.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListProperties.java index a7d08ba36e2..55c750aa712 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListProperties.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListProperties.java @@ -20,10 +20,20 @@ package org.apache.gravitino.cli.commands; import java.util.Map; +import org.apache.gravitino.cli.CommandContext; /** List the properties of a metalake. */ public class ListProperties extends Command { + /** + * List the properties of an entity. + * + * @param context The command context. + */ + public ListProperties(CommandContext context) { + super(context); + } + /** * List the properties of an entity. * diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetadataCommand.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetadataCommand.java index 3f1e347c1fd..0ff7bce294f 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetadataCommand.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetadataCommand.java @@ -22,6 +22,7 @@ import org.apache.gravitino.Catalog; import org.apache.gravitino.MetadataObject; import org.apache.gravitino.MetadataObjects; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.FullName; import org.apache.gravitino.client.GravitinoClient; @@ -37,6 +38,15 @@ public MetadataCommand(String url, boolean ignoreVersions) { super(url, ignoreVersions); } + /** + * MetadataCommand constructor. + * + * @param context The command context. + */ + public MetadataCommand(CommandContext context) { + super(context); + } + /** * Constructs a {@link MetadataObject} based on the provided client, existing metadata object, and * entity name. diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeAudit.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeAudit.java index b966a1ae291..76d953e5b8e 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeAudit.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeAudit.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.Audit; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -31,12 +32,11 @@ public class MetalakeAudit extends AuditCommand { /** * Displays metalake audit information. * - * @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. */ - public MetalakeAudit(String url, boolean ignoreVersions, String metalake) { - super(url, ignoreVersions); + public MetalakeAudit(CommandContext context, String metalake) { + super(context); this.metalake = metalake; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDetails.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDetails.java index ea503710d42..cc2bf6ce4b4 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDetails.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDetails.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.Metalake; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -31,13 +32,11 @@ public class MetalakeDetails extends Command { /** * Displays metalake details. * - * @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 MetalakeDetails(String url, boolean ignoreVersions, String outputFormat, String metalake) { - super(url, ignoreVersions, outputFormat); + public MetalakeDetails(CommandContext context, String metalake) { + super(context); this.metalake = metalake; } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java index 02e33a45d45..ad05d293433 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java @@ -19,6 +19,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.exceptions.NoSuchMetalakeException; @@ -30,12 +31,11 @@ public class MetalakeDisable extends Command { /** * Disable metalake * - * @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. */ - public MetalakeDisable(String url, boolean ignoreVersions, String metalake) { - super(url, ignoreVersions); + public MetalakeDisable(CommandContext context, String metalake) { + super(context); this.metalake = metalake; } @@ -51,6 +51,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(metalake + " has been disabled."); + printInformation(metalake + " has been disabled."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java index 34ba23a61bb..f402c376081 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import java.util.Arrays; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.client.GravitinoMetalake; @@ -34,14 +35,12 @@ public class MetalakeEnable extends Command { /** * Enable a metalake * - * @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 enableAllCatalogs Whether to enable all catalogs. */ - public MetalakeEnable( - String url, boolean ignoreVersions, String metalake, boolean enableAllCatalogs) { - super(url, ignoreVersions); + public MetalakeEnable(CommandContext context, String metalake, boolean enableAllCatalogs) { + super(context); this.metalake = metalake; this.enableAllCatalogs = enableAllCatalogs; } @@ -67,6 +66,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(msgBuilder); + printInformation(msgBuilder.toString()); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveMetalakeProperty.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveMetalakeProperty.java index ce3a50fee16..ad00aa2fd21 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveMetalakeProperty.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/RemoveMetalakeProperty.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.MetalakeChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -33,14 +34,12 @@ public class RemoveMetalakeProperty extends Command { /** * Remove a property of a metalake. * - * @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 property The name of the property. */ - public RemoveMetalakeProperty( - String url, boolean ignoreVersions, String metalake, String property) { - super(url, ignoreVersions); + public RemoveMetalakeProperty(CommandContext context, String metalake, String property) { + super(context); this.metalake = metalake; this.property = property; } @@ -58,7 +57,7 @@ public void handle() { 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/SetMetalakeProperty.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetMetalakeProperty.java index ef67d008bc8..330ebb6398d 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetMetalakeProperty.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SetMetalakeProperty.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.MetalakeChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -34,15 +35,14 @@ public class SetMetalakeProperty extends Command { /** * Set a property of a metalake. * - * @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 property The name of the property. * @param value The value of the property. */ public SetMetalakeProperty( - String url, boolean ignoreVersions, String metalake, String property, String value) { - super(url, ignoreVersions); + CommandContext context, String metalake, String property, String value) { + super(context); this.metalake = metalake; this.property = property; this.value = value; @@ -61,7 +61,7 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(metalake + " property set."); + printInformation(metalake + " property set."); } @Override diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeComment.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeComment.java index 9ca63084e75..96a5a927418 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeComment.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeComment.java @@ -20,6 +20,7 @@ package org.apache.gravitino.cli.commands; import org.apache.gravitino.MetalakeChange; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -33,14 +34,12 @@ public class UpdateMetalakeComment extends Command { /** * Update the comment of a metalake. * - * @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 comment New metalake comment. */ - public UpdateMetalakeComment( - String url, boolean ignoreVersions, String metalake, String comment) { - super(url, ignoreVersions); + public UpdateMetalakeComment(CommandContext context, String metalake, String comment) { + super(context); this.metalake = metalake; this.comment = comment; } @@ -58,6 +57,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(metalake + " comment changed."); + printInformation(metalake + " comment changed."); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeName.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeName.java index 275ba3165df..60497d034df 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeName.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateMetalakeName.java @@ -21,6 +21,7 @@ import org.apache.gravitino.MetalakeChange; import org.apache.gravitino.cli.AreYouSure; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.cli.ErrorMessages; import org.apache.gravitino.client.GravitinoAdminClient; import org.apache.gravitino.exceptions.NoSuchMetalakeException; @@ -35,16 +36,13 @@ public class UpdateMetalakeName extends Command { /** * Update the name of a metalake. * - * @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 name The new metalake name. */ - public UpdateMetalakeName( - String url, boolean ignoreVersions, boolean force, String metalake, String name) { - super(url, ignoreVersions); - this.force = force; + public UpdateMetalakeName(CommandContext context, String metalake, String name) { + super(context); + this.force = context.force(); this.metalake = metalake; this.name = name; } @@ -67,6 +65,6 @@ public void handle() { exitWithError(exp.getMessage()); } - System.out.println(metalake + " name changed."); + printInformation(metalake + " name changed."); } } diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java index dae2fe63400..37e3acd2896 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java @@ -22,7 +22,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.any; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -65,6 +68,7 @@ void setUp() { mockOptions = mock(Options.class); System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); + Main.useExit = false; } @AfterEach @@ -87,9 +91,7 @@ void testListMetalakesCommand() { spy( new GravitinoCommandLine( mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.LIST)); - doReturn(mockList) - .when(commandLine) - .newListMetalakes(GravitinoCommandLine.DEFAULT_URL, false, null); + doReturn(mockList).when(commandLine).newListMetalakes(any(CommandContext.class)); doReturn(mockList).when(mockList).validate(); commandLine.handleCommandLine(); verify(mockList).handle(); @@ -107,7 +109,7 @@ void testMetalakeDetailsCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.DETAILS)); doReturn(mockDetails) .when(commandLine) - .newMetalakeDetails(GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo"); + .newMetalakeDetails(any(CommandContext.class), eq("metalake_demo")); doReturn(mockDetails).when(mockDetails).validate(); commandLine.handleCommandLine(); verify(mockDetails).handle(); @@ -125,7 +127,7 @@ void testMetalakeAuditCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.DETAILS)); doReturn(mockAudit) .when(commandLine) - .newMetalakeAudit(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo"); + .newMetalakeAudit(any(CommandContext.class), eq("metalake_demo")); doReturn(mockAudit).when(mockAudit).validate(); commandLine.handleCommandLine(); verify(mockAudit).handle(); @@ -144,7 +146,7 @@ void testCreateMetalakeCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.CREATE)); doReturn(mockCreate) .when(commandLine) - .newCreateMetalake(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "comment"); + .newCreateMetalake(any(CommandContext.class), eq("metalake_demo"), eq("comment")); doReturn(mockCreate).when(mockCreate).validate(); commandLine.handleCommandLine(); verify(mockCreate).handle(); @@ -161,7 +163,7 @@ void testCreateMetalakeCommandNoComment() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.CREATE)); doReturn(mockCreate) .when(commandLine) - .newCreateMetalake(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", null); + .newCreateMetalake(any(CommandContext.class), eq("metalake_demo"), isNull()); doReturn(mockCreate).when(mockCreate).validate(); commandLine.handleCommandLine(); verify(mockCreate).handle(); @@ -178,7 +180,7 @@ void testDeleteMetalakeCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteMetalake(GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo"); + .newDeleteMetalake(any(CommandContext.class), eq("metalake_demo")); doReturn(mockDelete).when(mockDelete).validate(); commandLine.handleCommandLine(); verify(mockDelete).handle(); @@ -189,14 +191,13 @@ void testDeleteMetalakeForceCommand() { DeleteMetalake mockDelete = mock(DeleteMetalake.class); when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true); when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo"); - when(mockCommandLine.hasOption(GravitinoOptions.FORCE)).thenReturn(true); GravitinoCommandLine commandLine = spy( new GravitinoCommandLine( mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.DELETE)); doReturn(mockDelete) .when(commandLine) - .newDeleteMetalake(GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo"); + .newDeleteMetalake(any(CommandContext.class), eq("metalake_demo")); doReturn(mockDelete).when(mockDelete).validate(); commandLine.handleCommandLine(); verify(mockDelete).handle(); @@ -218,7 +219,7 @@ void testSetMetalakePropertyCommand() { doReturn(mockSetProperty) .when(commandLine) .newSetMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "property", "value"); + any(CommandContext.class), eq("metalake_demo"), eq("property"), eq("value")); doReturn(mockSetProperty).when(mockSetProperty).validate(); commandLine.handleCommandLine(); verify(mockSetProperty).handle(); @@ -226,11 +227,11 @@ void testSetMetalakePropertyCommand() { @Test void testSetMetalakePropertyCommandWithoutPropertyAndValue() { + CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + Main.useExit = false; SetMetalakeProperty metalakeProperty = - spy( - new SetMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null, null)); + spy(new SetMetalakeProperty(context, "demo_metalake", null, null)); assertThrows(RuntimeException.class, metalakeProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -239,11 +240,11 @@ void testSetMetalakePropertyCommandWithoutPropertyAndValue() { @Test void testSetMetalakePropertyCommandWithoutProperty() { + CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + Main.useExit = false; SetMetalakeProperty metalakeProperty = - spy( - new SetMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null, "val1")); + spy(new SetMetalakeProperty(context, "demo_metalake", null, "val1")); assertThrows(RuntimeException.class, metalakeProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -252,11 +253,11 @@ void testSetMetalakePropertyCommandWithoutProperty() { @Test void testSetMetalakePropertyCommandWithoutValue() { + CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + Main.useExit = false; SetMetalakeProperty metalakeProperty = - spy( - new SetMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", "property1", null)); + spy(new SetMetalakeProperty(context, "demo_metalake", "property1", null)); assertThrows(RuntimeException.class, metalakeProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -276,8 +277,7 @@ void testRemoveMetalakePropertyCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.REMOVE)); doReturn(mockRemoveProperty) .when(commandLine) - .newRemoveMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "property"); + .newRemoveMetalakeProperty(any(CommandContext.class), eq("metalake_demo"), eq("property")); doReturn(mockRemoveProperty).when(mockRemoveProperty).validate(); commandLine.handleCommandLine(); verify(mockRemoveProperty).handle(); @@ -285,11 +285,11 @@ void testRemoveMetalakePropertyCommand() { @Test void testRemoveMetalakePropertyCommandWithoutProperty() { + CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + Main.useExit = false; RemoveMetalakeProperty mockRemoveProperty = - spy( - new RemoveMetalakeProperty( - GravitinoCommandLine.DEFAULT_URL, false, "demo_metalake", null)); + spy(new RemoveMetalakeProperty(context, "demo_metalake", null)); assertThrows(RuntimeException.class, mockRemoveProperty::validate); String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim(); @@ -307,7 +307,7 @@ void testListMetalakePropertiesCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.PROPERTIES)); doReturn(mockListProperties) .when(commandLine) - .newListMetalakeProperties(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo"); + .newListMetalakeProperties(any(CommandContext.class), eq("metalake_demo")); doReturn(mockListProperties).when(mockListProperties).validate(); commandLine.handleCommandLine(); verify(mockListProperties).handle(); @@ -327,7 +327,7 @@ void testUpdateMetalakeCommentCommand() { doReturn(mockUpdateComment) .when(commandLine) .newUpdateMetalakeComment( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "new comment"); + any(CommandContext.class), eq("metalake_demo"), eq("new comment")); doReturn(mockUpdateComment).when(mockUpdateComment).validate(); commandLine.handleCommandLine(); verify(mockUpdateComment).handle(); @@ -346,8 +346,7 @@ void testUpdateMetalakeNameCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE)); doReturn(mockUpdateName) .when(commandLine) - .newUpdateMetalakeName( - GravitinoCommandLine.DEFAULT_URL, false, false, "metalake_demo", "new_name"); + .newUpdateMetalakeName(any(CommandContext.class), eq("metalake_demo"), eq("new_name")); doReturn(mockUpdateName).when(mockUpdateName).validate(); commandLine.handleCommandLine(); verify(mockUpdateName).handle(); @@ -367,8 +366,7 @@ void testUpdateMetalakeNameForceCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE)); doReturn(mockUpdateName) .when(commandLine) - .newUpdateMetalakeName( - GravitinoCommandLine.DEFAULT_URL, false, true, "metalake_demo", "new_name"); + .newUpdateMetalakeName(any(CommandContext.class), eq("metalake_demo"), eq("new_name")); doReturn(mockUpdateName).when(mockUpdateName).validate(); commandLine.handleCommandLine(); verify(mockUpdateName).handle(); @@ -386,7 +384,7 @@ void testEnableMetalakeCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE)); doReturn(mockEnable) .when(commandLine) - .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false); + .newMetalakeEnable(any(CommandContext.class), eq("metalake_demo"), eq(false)); doReturn(mockEnable).when(mockEnable).validate(); commandLine.handleCommandLine(); verify(mockEnable).handle(); @@ -405,7 +403,7 @@ void testEnableMetalakeCommandWithRecursive() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE)); doReturn(mockEnable) .when(commandLine) - .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", true); + .newMetalakeEnable(any(CommandContext.class), eq("metalake_demo"), eq(true)); doReturn(mockEnable).when(mockEnable).validate(); commandLine.handleCommandLine(); verify(mockEnable).handle(); @@ -424,7 +422,7 @@ void testDisableMetalakeCommand() { mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE)); doReturn(mockDisable) .when(commandLine) - .newMetalakeDisable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo"); + .newMetalakeDisable(any(CommandContext.class), eq("metalake_demo")); doReturn(mockDisable).when(mockDisable).validate(); commandLine.handleCommandLine(); verify(mockDisable).handle(); @@ -446,9 +444,9 @@ void testMetalakeWithDisableAndEnableOptions() { Assert.assertThrows(RuntimeException.class, commandLine::handleCommandLine); verify(commandLine, never()) - .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false); + .newMetalakeEnable(any(CommandContext.class), eq("metalake_demo"), eq(false)); verify(commandLine, never()) - .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false); + .newMetalakeEnable(any(CommandContext.class), eq("metalake_demo"), eq(false)); assertTrue(errContent.toString().contains(ErrorMessages.INVALID_ENABLE_DISABLE)); } } From 7d934604ee74d3bfd1f8a66942d57c673d5cbc7f Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Wed, 22 Jan 2025 15:24:29 +1100 Subject: [PATCH 2/2] helps if I add the context class --- .../apache/gravitino/cli/CommandContext.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java new file mode 100644 index 00000000000..994b97226e9 --- /dev/null +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java @@ -0,0 +1,104 @@ +/* + * 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; + +/* Context for a command */ +public class CommandContext { + private String url; + private boolean ignoreVersions; + private boolean force; + private String outputFormat; + // Can add more "global" command flags here without any major changes e.g. a guiet flag + + /** + * Command constructor. + * + * @param url The URL of the Gravitino server. + * @param ignoreVersions If true don't check the client/server versions match. + */ + public CommandContext(String url, boolean ignoreVersions) { + this.url = url; + this.ignoreVersions = ignoreVersions; + this.force = false; + this.outputFormat = Command.OUTPUT_FORMAT_PLAIN; + } + + /** + * Command constructor. + * + * @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 outputFormat Display output format. + */ + public CommandContext(String url, boolean ignoreVersions, boolean force, String outputFormat) { + this.url = url; + this.ignoreVersions = ignoreVersions; + this.force = force; + this.outputFormat = outputFormat; + } + + /** + * Returns the URL. + * + * @return The URL. + */ + public String url() { + return url; + } + + /** + * Sets the URL. + * + * @param url The URL to be set. + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * Indicates whether versions should be ignored. + * + * @return False if versions should be ignored. + */ + public boolean ignoreVersions() { + return ignoreVersions; + } + + /** + * Indicates whether the operation should be forced. + * + * @return True if the operation should be forced. + */ + public boolean force() { + return force; + } + + /** + * Returns the output format. + * + * @return The output format. + */ + public String outputFormat() { + return outputFormat; + } +}