Skip to content

Commit

Permalink
[apache#6424] improve(CLI): Refactor getURL in CLI and add context to…
Browse files Browse the repository at this point in the history
… simple commands. (apache#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: apache#6424 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test.
  • Loading branch information
Abyss-lord authored Feb 12, 2025
1 parent 423d311 commit 4b5d6a5
Show file tree
Hide file tree
Showing 23 changed files with 150 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public CatalogCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public ColumnCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down
122 changes: 88 additions & 34 deletions clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -66,15 +64,6 @@ 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.
*
Expand All @@ -101,4 +90,69 @@ public boolean force() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,9 @@

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public FilesetCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -60,29 +57,8 @@ public GravitinoCommandLine(CommandLine line, Options options, String entity, St

/** 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. */
Expand All @@ -91,7 +67,8 @@ public void handleSimpleLine() {
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();
}
}

Expand All @@ -106,11 +83,7 @@ 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);

private void executeCommand(CommandContext context) {
if (CommandActions.HELP.equals(command)) {
handleHelpCommand();
} else if (line.hasOption(GravitinoOptions.OWNER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public GroupCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public MetalakeCommandHandler(
this.line = line;
this.command = command;
this.context = context;
this.context.setUrl(getUrl(line));
}

/** Handles the command execution logic based on the provided command. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public ModelCommandHandler(
this.command = command;

this.context = context;
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public OwnerCommandHandler(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public RoleCommandHandler(
this.line = line;
this.command = command;
this.context = context;
this.context.setUrl(getUrl(line));
}

/** Handles the command execution logic based on the provided command. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public SchemaCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public TableCommandHandler(
this.command = command;
this.context = context;

this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public TagCommandHandler(
this.line = line;
this.command = command;
this.context = context;
this.context.setUrl(getUrl(line));
this.tags = line.getOptionValues(GravitinoOptions.TAG);

if (tags != null) {
Expand Down
Loading

0 comments on commit 4b5d6a5

Please sign in to comment.