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 4b5d6a5b0c [#6424] improve(CLI): Refactor getURL in CLI and add context to simple commands. (#6440) 4b5d6a5b0c is described below commit 4b5d6a5b0c6a6ce1469353ddd681d56647699a1e Author: Lord of Abyss <103809695+abyss-l...@users.noreply.github.com> AuthorDate: Wed Feb 12 16:49:22 2025 +0800 [#6424] improve(CLI): Refactor getURL in CLI and add context to simple commands. (#6440) ### What changes were proposed in this pull request? Refactor getURL in CLI and add context to simple commands. ### Why are the changes needed? Fix: #6424 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? local test. --- .../gravitino/cli/CatalogCommandHandler.java | 1 - .../apache/gravitino/cli/ColumnCommandHandler.java | 1 - .../org/apache/gravitino/cli/CommandContext.java | 122 +++++++++++++++------ .../org/apache/gravitino/cli/CommandHandler.java | 44 -------- .../gravitino/cli/FilesetCommandHandler.java | 1 - .../apache/gravitino/cli/GravitinoCommandLine.java | 37 +------ .../apache/gravitino/cli/GroupCommandHandler.java | 1 - .../gravitino/cli/MetalakeCommandHandler.java | 1 - .../apache/gravitino/cli/ModelCommandHandler.java | 1 - .../apache/gravitino/cli/OwnerCommandHandler.java | 1 - .../apache/gravitino/cli/RoleCommandHandler.java | 1 - .../apache/gravitino/cli/SchemaCommandHandler.java | 1 - .../apache/gravitino/cli/SimpleCommandHandler.java | 12 +- .../apache/gravitino/cli/TableCommandHandler.java | 1 - .../apache/gravitino/cli/TagCommandHandler.java | 1 - .../apache/gravitino/cli/TestableCommandLine.java | 8 +- .../apache/gravitino/cli/TopicCommandHandler.java | 1 - .../apache/gravitino/cli/UserCommandHandler.java | 1 - .../gravitino/cli/commands/ClientVersion.java | 10 +- .../gravitino/cli/commands/ServerVersion.java | 10 +- .../apache/gravitino/cli/TestCommandContext.java | 35 +++++- .../apache/gravitino/cli/TestMetalakeCommands.java | 8 +- .../apache/gravitino/cli/TestSimpleCommands.java | 9 +- 23 files changed, 150 insertions(+), 158 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 a702856f65..edef9138e3 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 @@ -56,7 +56,6 @@ public class CatalogCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java index 58398585c3..01de9ca2a9 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/ColumnCommandHandler.java @@ -55,7 +55,6 @@ public class ColumnCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); 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 index 994b97226e..fc713ba577 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java @@ -19,42 +19,40 @@ package org.apache.gravitino.cli; +import com.google.common.base.Preconditions; +import org.apache.commons.cli.CommandLine; 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; + private final boolean force; + private final boolean ignoreVersions; + private final String outputFormat; + private final String url; + private final CommandLine line; + + private String ignoreEnv; + private boolean ignoreSet = false; + private String urlEnv; + private boolean urlSet = false; // 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. + * @param line The command line. */ - public CommandContext(String url, boolean ignoreVersions) { - this.url = url; - this.ignoreVersions = ignoreVersions; - this.force = false; - this.outputFormat = Command.OUTPUT_FORMAT_PLAIN; - } + public CommandContext(CommandLine line) { + Preconditions.checkNotNull(line); + this.line = line; + this.force = line.hasOption(GravitinoOptions.FORCE); + this.outputFormat = + line.hasOption(GravitinoOptions.OUTPUT) + ? line.getOptionValue(GravitinoOptions.OUTPUT) + : 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; + this.url = getUrl(); + this.ignoreVersions = getIgnore(); } /** @@ -66,15 +64,6 @@ public class CommandContext { 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. * @@ -101,4 +90,69 @@ public class CommandContext { public String outputFormat() { return outputFormat; } + + /** + * Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment + * variable or the Gravitino config file. + * + * @return The Gravitino URL, or null if not found. + */ + private String getUrl() { + GravitinoConfig config = new GravitinoConfig(null); + + // If specified on the command line use that + if (line.hasOption(GravitinoOptions.URL)) { + return line.getOptionValue(GravitinoOptions.URL); + } + + // Cache the Gravitino URL environment variable + if (urlEnv == null && !urlSet) { + urlEnv = System.getenv("GRAVITINO_URL"); + urlSet = true; + } + + // If set return the Gravitino URL environment variable + if (urlEnv != null) { + return urlEnv; + } + + // Check if the Gravitino URL is specified in the configuration file + if (config.fileExists()) { + config.read(); + String configURL = config.getGravitinoURL(); + if (configURL != null) { + return configURL; + } + } + + // Return the default localhost URL + return GravitinoCommandLine.DEFAULT_URL; + } + + private boolean getIgnore() { + GravitinoConfig config = new GravitinoConfig(null); + boolean ignore = false; + + /* Check if you should ignore client/version versions */ + if (line.hasOption(GravitinoOptions.IGNORE)) { + ignore = true; + } else { + // Cache the ignore environment variable + if (ignoreEnv == null && !ignoreSet) { + ignoreEnv = System.getenv("GRAVITINO_IGNORE"); + ignore = ignoreEnv != null && ignoreEnv.equals("true"); + ignoreSet = true; + } + + // Check if the ignore name is specified in the configuration file + if (ignoreEnv == null) { + if (config.fileExists()) { + config.read(); + ignore = config.getIgnore(); + } + } + } + + return ignore; + } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java index 2af2487cc8..79462e8102 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java @@ -25,53 +25,9 @@ import org.apache.commons.cli.CommandLine; public abstract class CommandHandler { public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls(); - - public static final String DEFAULT_URL = "http://localhost:8090"; - - private String urlEnv; - private boolean urlSet = false; private String authEnv; private boolean authSet = false; - /** - * Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment - * variable or the Gravitino config file. - * - * @param line The command line instance. - * @return The Gravitino URL, or null if not found. - */ - public String getUrl(CommandLine line) { - GravitinoConfig config = new GravitinoConfig(null); - - // If specified on the command line use that - if (line.hasOption(GravitinoOptions.URL)) { - return line.getOptionValue(GravitinoOptions.URL); - } - - // Cache the Gravitino URL environment variable - if (urlEnv == null && !urlSet) { - urlEnv = System.getenv("GRAVITINO_URL"); - urlSet = true; - } - - // If set return the Gravitino URL environment variable - if (urlEnv != null) { - return urlEnv; - } - - // Check if the Gravitino URL is specified in the configuration file - if (config.fileExists()) { - config.read(); - String configURL = config.getGravitinoURL(); - if (configURL != null) { - return configURL; - } - } - - // Return the default localhost URL - return DEFAULT_URL; - } - /** * Retrieves the Gravitino authentication from the command line options or the GRAVITINO_AUTH * environment variable or the Gravitino config file. diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java index 54db3ea6bd..9eec933ddf 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java @@ -56,7 +56,6 @@ public class FilesetCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); 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 3a7c656691..de9ff12d66 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 @@ -35,9 +35,6 @@ public class GravitinoCommandLine extends TestableCommandLine { private final Options options; private final String entity; private final String command; - private boolean ignore = false; - private String ignoreEnv; - private boolean ignoreSet = false; public static final String CMD = "gcli"; // recommended name public static final String DEFAULT_URL = "http://localhost:8090"; @@ -60,29 +57,8 @@ public class GravitinoCommandLine extends TestableCommandLine { /** Handles the parsed command line arguments and executes the corresponding actions. */ public void handleCommandLine() { - GravitinoConfig config = new GravitinoConfig(null); - - /* Check if you should ignore client/version versions */ - if (line.hasOption(GravitinoOptions.IGNORE)) { - ignore = true; - } else { - // Cache the ignore environment variable - if (ignoreEnv == null && !ignoreSet) { - ignoreEnv = System.getenv("GRAVITINO_IGNORE"); - ignore = ignoreEnv != null && ignoreEnv.equals("true"); - ignoreSet = true; - } - - // Check if the ignore name is specified in the configuration file - if (ignoreEnv == null) { - if (config.fileExists()) { - config.read(); - ignore = config.getIgnore(); - } - } - } - - executeCommand(); + CommandContext context = new CommandContext(line); + executeCommand(context); } /** Handles the parsed command line arguments and executes the corresponding actions. */ @@ -91,7 +67,8 @@ public class GravitinoCommandLine extends TestableCommandLine { if (line.hasOption(GravitinoOptions.HELP)) { displayHelp(options); } else { - new SimpleCommandHandler(this, line, ignore).handle(); + CommandContext context = new CommandContext(line); + new SimpleCommandHandler(this, line, context).handle(); } } @@ -106,11 +83,7 @@ public class GravitinoCommandLine extends TestableCommandLine { } /** 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); - + private void executeCommand(CommandContext context) { if (CommandActions.HELP.equals(command)) { handleHelpCommand(); } else if (line.hasOption(GravitinoOptions.OWNER)) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GroupCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GroupCommandHandler.java index 6fcec1447b..deaad3df3e 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/GroupCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GroupCommandHandler.java @@ -50,7 +50,6 @@ public class GroupCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); } 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 7d39683842..1fe32f3fea 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 @@ -50,7 +50,6 @@ public class MetalakeCommandHandler extends CommandHandler { this.line = line; this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); } /** Handles the command execution logic based on the provided command. */ diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java index 0a0c8e8885..2a0dee0bf6 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java @@ -55,7 +55,6 @@ public class ModelCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/OwnerCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/OwnerCommandHandler.java index 28fb7fb380..cef7a9822f 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/OwnerCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/OwnerCommandHandler.java @@ -55,7 +55,6 @@ public class OwnerCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.owner = line.getOptionValue(GravitinoOptions.USER); this.group = line.getOptionValue(GravitinoOptions.GROUP); this.name = new FullName(line); diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java index ef21ec5c41..a02d7f0ca8 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java @@ -42,7 +42,6 @@ public class RoleCommandHandler extends CommandHandler { this.line = line; this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); } /** Handles the command execution logic based on the provided command. */ diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java index 4b6a11496e..30c051ccf2 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java @@ -53,7 +53,6 @@ public class SchemaCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.java index 48aca9f956..113be4f20b 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/SimpleCommandHandler.java @@ -25,29 +25,29 @@ import org.apache.commons.cli.CommandLine; public class SimpleCommandHandler extends CommandHandler { private final GravitinoCommandLine gravitinoCommandLine; private final CommandLine line; - private final boolean ignore; + private final CommandContext context; /** * Constructs a {@link SimpleCommandHandler} instance. * * @param gravitinoCommandLine The Gravitino command line instance. * @param line The command line arguments. - * @param ignore Ignore server version mismatch. + * @param context The command context. */ public SimpleCommandHandler( - GravitinoCommandLine gravitinoCommandLine, CommandLine line, boolean ignore) { + GravitinoCommandLine gravitinoCommandLine, CommandLine line, CommandContext context) { this.gravitinoCommandLine = gravitinoCommandLine; this.line = line; - this.ignore = ignore; + this.context = context; } /** Handles the command execution logic based on the provided command. */ @Override protected void handle() { if (line.hasOption(GravitinoOptions.VERSION)) { - gravitinoCommandLine.newClientVersion(getUrl(line), ignore).validate().handle(); + gravitinoCommandLine.newClientVersion(context).validate().handle(); } else if (line.hasOption(GravitinoOptions.SERVER)) { - gravitinoCommandLine.newServerVersion(getUrl(line), ignore).validate().handle(); + gravitinoCommandLine.newServerVersion(context).validate().handle(); } } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TableCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TableCommandHandler.java index 520b655c25..8287691704 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TableCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TableCommandHandler.java @@ -54,7 +54,6 @@ public class TableCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java index 13567ac064..2c8612a33c 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java @@ -39,7 +39,6 @@ public class TagCommandHandler extends CommandHandler { this.line = line; this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.tags = line.getOptionValues(GravitinoOptions.TAG); if (tags != null) { 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 22425b621b..346e2691f4 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 @@ -149,12 +149,12 @@ import org.apache.gravitino.cli.commands.UserDetails; */ public class TestableCommandLine { - protected ClientVersion newClientVersion(String url, boolean ignore) { - return new ClientVersion(url, ignore); + protected ClientVersion newClientVersion(CommandContext context) { + return new ClientVersion(context); } - protected ServerVersion newServerVersion(String url, boolean ignore) { - return new ServerVersion(url, ignore); + protected ServerVersion newServerVersion(CommandContext context) { + return new ServerVersion(context); } protected MetalakeAudit newMetalakeAudit(CommandContext context, String metalake) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java index f67d003b28..34315fa971 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java @@ -54,7 +54,6 @@ public class TopicCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/UserCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/UserCommandHandler.java index 964d2ba6d9..5fd7d906b9 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/UserCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/UserCommandHandler.java @@ -50,7 +50,6 @@ public class UserCommandHandler extends CommandHandler { this.command = command; this.context = context; - this.context.setUrl(getUrl(line)); this.name = new FullName(line); this.metalake = name.getMetalakeName(); } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ClientVersion.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ClientVersion.java index 6bc2200b7b..28c702e39f 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ClientVersion.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ClientVersion.java @@ -19,6 +19,7 @@ package org.apache.gravitino.cli.commands; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.client.GravitinoAdminClient; /** Displays the Gravitino client version. */ @@ -27,11 +28,10 @@ public class ClientVersion extends Command { /** * Displays the client version. * - * @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 ClientVersion(String url, boolean ignoreVersions) { - super(url, ignoreVersions); + public ClientVersion(CommandContext context) { + super(context); } /** Displays the client version. */ @@ -44,6 +44,6 @@ public class ClientVersion extends Command { } catch (Exception exp) { exitWithError(exp.getMessage()); } - System.out.println("Apache Gravitino " + version); + printResults("Apache Gravitino " + version); } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ServerVersion.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ServerVersion.java index 218fa71bb8..b94076db69 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ServerVersion.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/ServerVersion.java @@ -19,6 +19,7 @@ package org.apache.gravitino.cli.commands; +import org.apache.gravitino.cli.CommandContext; import org.apache.gravitino.client.GravitinoAdminClient; /** Displays the Gravitino server version. */ @@ -27,11 +28,10 @@ public class ServerVersion extends Command { /** * Displays the server version. * - * @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 ServerVersion(String url, boolean ignoreVersions) { - super(url, ignoreVersions); + public ServerVersion(CommandContext context) { + super(context); } /** Displays the server version. */ @@ -45,6 +45,6 @@ public class ServerVersion extends Command { exitWithError(exp.getMessage()); } - System.out.println("Apache Gravitino " + version); + printResults("Apache Gravitino " + version); } } 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 index 8919446826..5bdd822b33 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCommandContext.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCommandContext.java @@ -19,16 +19,29 @@ package org.apache.gravitino.cli; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.commons.cli.CommandLine; import org.apache.gravitino.cli.commands.Command; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class TestCommandContext { + private CommandLine mockCommandLine; + + @BeforeEach + void setUp() { + mockCommandLine = mock(CommandLine.class); + } + @Test public void testCreateCommandContextWithDefaults() { - CommandContext commandContext = new CommandContext("http://localhost:8080", false); - Assertions.assertEquals("http://localhost:8080", commandContext.url()); + CommandContext commandContext = new CommandContext(mockCommandLine); + + Assertions.assertEquals(GravitinoCommandLine.DEFAULT_URL, commandContext.url()); Assertions.assertFalse(commandContext.ignoreVersions()); Assertions.assertFalse(commandContext.force()); Assertions.assertEquals(Command.OUTPUT_FORMAT_PLAIN, commandContext.outputFormat()); @@ -36,11 +49,23 @@ public class TestCommandContext { @Test public void testCreateCommandContextWithCustomValues() { - CommandContext commandContext = - new CommandContext("http://localhost:8080", true, true, Command.OUTPUT_FORMAT_TABLE); - Assertions.assertEquals("http://localhost:8080", commandContext.url()); + when(mockCommandLine.hasOption(GravitinoOptions.URL)).thenReturn(true); + when(mockCommandLine.getOptionValue(GravitinoOptions.URL)).thenReturn("http://localhost:8090"); + when(mockCommandLine.hasOption(GravitinoOptions.IGNORE)).thenReturn(true); + when(mockCommandLine.hasOption(GravitinoOptions.FORCE)).thenReturn(true); + when(mockCommandLine.hasOption(GravitinoOptions.OUTPUT)).thenReturn(true); + when(mockCommandLine.getOptionValue(GravitinoOptions.OUTPUT)) + .thenReturn(Command.OUTPUT_FORMAT_TABLE); + + CommandContext commandContext = new CommandContext(mockCommandLine); + Assertions.assertEquals("http://localhost:8090", commandContext.url()); Assertions.assertTrue(commandContext.ignoreVersions()); Assertions.assertTrue(commandContext.force()); Assertions.assertEquals(Command.OUTPUT_FORMAT_TABLE, commandContext.outputFormat()); } + + @Test + public void testCreateCommandContextWithNull() { + Assertions.assertThrows(NullPointerException.class, () -> new CommandContext(null)); + } } 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 37e3acd289..e01b652c64 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 @@ -227,7 +227,7 @@ class TestMetalakeCommands { @Test void testSetMetalakePropertyCommandWithoutPropertyAndValue() { - CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + CommandContext context = new CommandContext(mockCommandLine); Main.useExit = false; SetMetalakeProperty metalakeProperty = @@ -240,7 +240,7 @@ class TestMetalakeCommands { @Test void testSetMetalakePropertyCommandWithoutProperty() { - CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + CommandContext context = new CommandContext(mockCommandLine); Main.useExit = false; SetMetalakeProperty metalakeProperty = @@ -253,7 +253,7 @@ class TestMetalakeCommands { @Test void testSetMetalakePropertyCommandWithoutValue() { - CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + CommandContext context = new CommandContext(mockCommandLine); Main.useExit = false; SetMetalakeProperty metalakeProperty = @@ -285,7 +285,7 @@ class TestMetalakeCommands { @Test void testRemoveMetalakePropertyCommandWithoutProperty() { - CommandContext context = new CommandContext(GravitinoCommandLine.DEFAULT_URL, false); + CommandContext context = new CommandContext(mockCommandLine); Main.useExit = false; RemoveMetalakeProperty mockRemoveProperty = diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestSimpleCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestSimpleCommands.java index 044e06c58f..8024189112 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestSimpleCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestSimpleCommands.java @@ -19,6 +19,7 @@ package org.apache.gravitino.cli; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -50,9 +51,7 @@ public class TestSimpleCommands { GravitinoCommandLine commandLine = spy(new GravitinoCommandLine(mockCommandLine, mockOptions, null, null)); - doReturn(mockServerVersion) - .when(commandLine) - .newServerVersion(GravitinoCommandLine.DEFAULT_URL, false); + doReturn(mockServerVersion).when(commandLine).newServerVersion(any(CommandContext.class)); doReturn(mockServerVersion).when(mockServerVersion).validate(); commandLine.handleSimpleLine(); verify(mockServerVersion).handle(); @@ -65,9 +64,7 @@ public class TestSimpleCommands { GravitinoCommandLine commandLine = spy(new GravitinoCommandLine(mockCommandLine, mockOptions, null, null)); - doReturn(mockClientVersion) - .when(commandLine) - .newClientVersion(GravitinoCommandLine.DEFAULT_URL, false); + doReturn(mockClientVersion).when(commandLine).newClientVersion(any(CommandContext.class)); doReturn(mockClientVersion).when(mockClientVersion).validate(); commandLine.handleSimpleLine(); verify(mockClientVersion).handle();