Skip to content

Commit

Permalink
[#6326] improve(CLI): Make CLI more extendable and maintainable.
Browse files Browse the repository at this point in the history
Add config class.
  • Loading branch information
Abyss-lord committed Jan 20, 2025
1 parent 8c2a5ec commit ced3ab8
Show file tree
Hide file tree
Showing 26 changed files with 813 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.gravitino.cli.config.GenericConfig;

/* Gravitino Command line */
public class GravitinoCommandLine extends TestableCommandLine {
Expand Down Expand Up @@ -107,7 +108,7 @@ public static void displayHelp(Options options) {

/** Executes the appropriate command based on the command type. */
private void executeCommand() {

GenericConfig genericConfig = GenericConfig.fromCommandLine(line);
if (CommandActions.HELP.equals(command)) {
handleHelpCommand();
} else if (line.hasOption(GravitinoOptions.OWNER)) {
Expand All @@ -121,7 +122,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, command, genericConfig).handle();
} else if (entity.equals(CommandEntities.TOPIC)) {
new TopicCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.FILESET)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,42 @@

import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.commands.Command;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.cli.config.CreateConfig;
import org.apache.gravitino.cli.config.DeleteConfig;
import org.apache.gravitino.cli.config.GenericConfig;
import org.apache.gravitino.cli.config.RemoveConfig;
import org.apache.gravitino.cli.config.SetConfig;
import org.apache.gravitino.cli.config.UpdateConfig;

/**
* Handles the command execution for Metalakes based on command type and the command line options.
*/
public class MetalakeCommandHandler extends CommandHandler {
private String command;
private GravitinoCommandLine gravitinoCommandLine;
private CommandLine line;
private GenericConfig genericConfig;
private FullName name;

private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private String metalake;

/**
* Constructs a MetalakeCommandHandler instance.
*
* @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.
*/
public MetalakeCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
GravitinoCommandLine gravitinoCommandLine, String command, GenericConfig config) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.url = getUrl(line);
this.genericConfig = config;
this.line = config.getCommandLine();
this.name = new FullName(line);
}

/** Handles the command execution logic based on the provided command. */
public void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
Command.setAuthenticationMode(getAuth(line), userName);

if (CommandActions.LIST.equals(command)) {
handleListCommand();
return;
}

metalake = name.getMetalakeName();

if (!executeCommand()) {
System.err.println(ErrorMessages.UNSUPPORTED_COMMAND);
Main.exit(-1);
Expand Down Expand Up @@ -113,56 +105,45 @@ private boolean executeCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
// TODO: move this to GravitinoCommandLine class
OutputProperty property = OutputProperty.fromLine(line);
gravitinoCommandLine.newListMetalakes(url, ignore, property).validate().handle();
gravitinoCommandLine.newListMetalakes(genericConfig).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
OutputProperty property = OutputProperty.fromLine(line);
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newMetalakeAudit(genericConfig, name).validate().handle();
} else {
// TODO: move this to GravitinoCommandLine class
gravitinoCommandLine.newMetalakeDetails(url, ignore, property, metalake).validate().handle();
gravitinoCommandLine.newMetalakeDetails(genericConfig, name).validate().handle();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine.newCreateMetalake(url, ignore, metalake, comment).validate().handle();
CreateConfig createConfig = new CreateConfig(genericConfig);
gravitinoCommandLine.newCreateMetalake(createConfig, name).validate().handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine.newDeleteMetalake(url, ignore, force, metalake).validate().handle();
DeleteConfig deleteConfig = new DeleteConfig(genericConfig);
gravitinoCommandLine.newDeleteMetalake(deleteConfig, name).validate().handle();
}

/** Handles the "SET" command. */
private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetMetalakeProperty(url, ignore, metalake, property, value)
.validate()
.handle();
SetConfig setConfig = new SetConfig(genericConfig);
gravitinoCommandLine.newSetMetalakeProperty(setConfig, name).validate().handle();
}

/** Handles the "REMOVE" command. */
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveMetalakeProperty(url, ignore, metalake, property)
.validate()
.handle();
RemoveConfig removeConfig = new RemoveConfig(genericConfig);
gravitinoCommandLine.newRemoveMetalakeProperty(removeConfig, name).validate().handle();
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine.newListMetalakeProperties(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newListMetalakeProperties(genericConfig, name).validate().handle();
}

/** Handles the "UPDATE" command. */
Expand All @@ -171,31 +152,20 @@ private void handleUpdateCommand() {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}

UpdateConfig updateConfig = new UpdateConfig(genericConfig);
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newMetalakeEnable(url, ignore, metalake, enableAllCatalogs)
.validate()
.handle();
gravitinoCommandLine.newMetalakeEnable(updateConfig, name).validate().handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newMetalakeDisable(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newMetalakeDisable(updateConfig, name).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateMetalakeComment(url, ignore, metalake, comment)
.validate()
.handle();
gravitinoCommandLine.newUpdateMetalakeComment(updateConfig, name).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(updateConfig, name).validate().handle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
import org.apache.gravitino.cli.commands.UpdateTopicComment;
import org.apache.gravitino.cli.commands.UserAudit;
import org.apache.gravitino.cli.commands.UserDetails;
import org.apache.gravitino.cli.config.CreateConfig;
import org.apache.gravitino.cli.config.DeleteConfig;
import org.apache.gravitino.cli.config.GenericConfig;
import org.apache.gravitino.cli.config.RemoveConfig;
import org.apache.gravitino.cli.config.SetConfig;
import org.apache.gravitino.cli.config.UpdateConfig;
import org.apache.gravitino.cli.outputs.OutputProperty;

/*
Expand All @@ -158,52 +164,46 @@ 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(GenericConfig config, FullName name) {
return new MetalakeAudit(config, name);
}

protected MetalakeDetails newMetalakeDetails(
String url, boolean ignore, OutputProperty property, String metalake) {
return new MetalakeDetails(url, ignore, property, metalake);
protected MetalakeDetails newMetalakeDetails(GenericConfig config, FullName name) {
return new MetalakeDetails(config, name);
}

protected ListMetalakes newListMetalakes(String url, boolean ignore, OutputProperty property) {
return new ListMetalakes(url, ignore, property);
protected ListMetalakes newListMetalakes(GenericConfig config) {
return new ListMetalakes(config);
}

protected CreateMetalake newCreateMetalake(
String url, boolean ignore, String metalake, String comment) {
return new CreateMetalake(url, ignore, metalake, comment);
protected CreateMetalake newCreateMetalake(CreateConfig config, FullName name) {
return new CreateMetalake(config, name);
}

protected DeleteMetalake newDeleteMetalake(
String url, boolean ignore, boolean force, String metalake) {
return new DeleteMetalake(url, ignore, force, metalake);
protected DeleteMetalake newDeleteMetalake(DeleteConfig deleteConfig, FullName name) {
return new DeleteMetalake(deleteConfig, name);
}

protected SetMetalakeProperty newSetMetalakeProperty(
String url, boolean ignore, String metalake, String property, String value) {
return new SetMetalakeProperty(url, ignore, metalake, property, value);
protected SetMetalakeProperty newSetMetalakeProperty(SetConfig setConfig, FullName name) {
return new SetMetalakeProperty(setConfig, name);
}

protected RemoveMetalakeProperty newRemoveMetalakeProperty(
String url, boolean ignore, String metalake, String property) {
return new RemoveMetalakeProperty(url, ignore, metalake, property);
RemoveConfig removeConfig, FullName name) {
return new RemoveMetalakeProperty(removeConfig, name);
}

protected ListMetalakeProperties newListMetalakeProperties(
String url, boolean ignore, String metalake) {
return new ListMetalakeProperties(url, ignore, metalake);
protected ListMetalakeProperties newListMetalakeProperties(GenericConfig config, FullName name) {
return new ListMetalakeProperties(config, name);
}

protected UpdateMetalakeComment newUpdateMetalakeComment(
String url, boolean ignore, String metalake, String comment) {
return new UpdateMetalakeComment(url, ignore, metalake, comment);
UpdateConfig updateConfig, FullName name) {
return new UpdateMetalakeComment(updateConfig, name);
}

protected UpdateMetalakeName newUpdateMetalakeName(
String url, boolean ignore, boolean force, String metalake, String newName) {
return new UpdateMetalakeName(url, ignore, force, metalake, newName);
protected UpdateMetalakeName newUpdateMetalakeName(UpdateConfig updateConfig, FullName name) {
return new UpdateMetalakeName(updateConfig, name);
}

protected CatalogAudit newCatalogAudit(
Expand Down Expand Up @@ -908,13 +908,12 @@ protected RevokeAllPrivileges newRevokeAllPrivileges(
return new RevokeAllPrivileges(url, ignore, metalake, role, entity);
}

protected MetalakeEnable newMetalakeEnable(
String url, boolean ignore, String metalake, boolean enableAllCatalogs) {
return new MetalakeEnable(url, ignore, metalake, enableAllCatalogs);
protected MetalakeEnable newMetalakeEnable(UpdateConfig updateConfig, FullName name) {
return new MetalakeEnable(updateConfig, name);
}

protected MetalakeDisable newMetalakeDisable(String url, boolean ignore, String metalake) {
return new MetalakeDisable(url, ignore, metalake);
protected MetalakeDisable newMetalakeDisable(UpdateConfig updateConfig, FullName name) {
return new MetalakeDisable(updateConfig, name);
}

protected CatalogEnable newCatalogEnable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.apache.gravitino.cli.commands;

import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.FullName;
import org.apache.gravitino.cli.config.CreateConfig;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.exceptions.MetalakeAlreadyExistsException;

Expand All @@ -30,15 +32,13 @@ 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 metalake The name of the metalake.
* @param comment The metalake's comment.
* @param config the {@link CreateConfig} instance containing the configuration for the command.
* @param name the {@link FullName} instance containing the full name of the metalake.
*/
public CreateMetalake(String url, boolean ignoreVersions, String metalake, String comment) {
super(url, ignoreVersions);
this.metalake = metalake;
this.comment = comment;
public CreateMetalake(CreateConfig config, FullName name) {
super(config.getUrl(), config.isIgnoreVersion());
this.metalake = name.getMetalakeName();
this.comment = config.getComment();
}

/** Create a new metalake. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.apache.gravitino.cli.AreYouSure;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.FullName;
import org.apache.gravitino.cli.config.DeleteConfig;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.exceptions.MetalakeInUseException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -32,15 +34,13 @@ 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 metalake The name of the metalake.
* @param config the {@link DeleteConfig} instance containing the configuration for the command.
* @param name the {@link FullName} instance containing the full name of the metalake.
*/
public DeleteMetalake(String url, boolean ignoreVersions, boolean force, String metalake) {
super(url, ignoreVersions);
this.force = force;
this.metalake = metalake;
public DeleteMetalake(DeleteConfig config, FullName name) {
super(config.getUrl(), config.isIgnoreVersion());
this.force = config.isForce();
this.metalake = name.getMetalakeName();
}

/** Delete a metalake. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.FullName;
import org.apache.gravitino.cli.config.GenericConfig;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

Expand All @@ -33,13 +35,13 @@ 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 metalake The name of the metalake.
* @param config the {@link GenericConfig} instance containing the URL and other configuration
* parameters.
* @param name the {@link FullName} instance containing the full name of the metalake.
*/
public ListMetalakeProperties(String url, boolean ignoreVersions, String metalake) {
super(url, ignoreVersions);
this.metalake = metalake;
public ListMetalakeProperties(GenericConfig config, FullName name) {
super(config.getUrl(), config.isIgnoreVersion());
this.metalake = name.getMetalakeName();
}

/** List the properties of a metalake. */
Expand Down
Loading

0 comments on commit ced3ab8

Please sign in to comment.