Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6136] improvement(CLI): Move check for enable and disable command in Gravitino CLI to command #6535

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,9 @@ private void handlePropertiesCommand() {

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableMetalake = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newCatalogEnable(context, metalake, catalog, enableMetalake)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newCatalogDisable(context, metalake, catalog).validate().handle();

if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newManageCatalog(context, metalake, catalog).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public String auth() {
return auth;
}

/**
* Returns the command line.
*
* @return The command line.
*/
public CommandLine line() {
return line;
}
/**
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitino config file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ErrorMessages {
public static final String ENTITY_IN_USE = " in use, please disable it first.";

public static final String INVALID_ENABLE_DISABLE =
"Unable to us --enable and --disable at the same time";
"Unable to use --enable and --disable at the same time.";
public static final String INVALID_OWNER_COMMAND =
"Unsupported combination of options either use --user or --group.";
public static final String INVALID_REMOVE_COMMAND =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,8 @@ private void handlePropertiesCommand() {

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newMetalakeEnable(context, metalake, enableAllCatalogs)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newMetalakeDisable(context, metalake).validate().handle();
if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newManageMetalake(context, metalake).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.gravitino.cli.commands.AddRoleToUser;
import org.apache.gravitino.cli.commands.CatalogAudit;
import org.apache.gravitino.cli.commands.CatalogDetails;
import org.apache.gravitino.cli.commands.CatalogDisable;
import org.apache.gravitino.cli.commands.CatalogEnable;
import org.apache.gravitino.cli.commands.ClientVersion;
import org.apache.gravitino.cli.commands.ColumnAudit;
import org.apache.gravitino.cli.commands.CreateCatalog;
Expand Down Expand Up @@ -78,10 +76,10 @@
import org.apache.gravitino.cli.commands.ListTopicProperties;
import org.apache.gravitino.cli.commands.ListTopics;
import org.apache.gravitino.cli.commands.ListUsers;
import org.apache.gravitino.cli.commands.ManageCatalog;
import org.apache.gravitino.cli.commands.ManageMetalake;
import org.apache.gravitino.cli.commands.MetalakeAudit;
import org.apache.gravitino.cli.commands.MetalakeDetails;
import org.apache.gravitino.cli.commands.MetalakeDisable;
import org.apache.gravitino.cli.commands.MetalakeEnable;
import org.apache.gravitino.cli.commands.ModelAudit;
import org.apache.gravitino.cli.commands.ModelDetails;
import org.apache.gravitino.cli.commands.OwnerDetails;
Expand Down Expand Up @@ -839,23 +837,13 @@ protected RevokeAllPrivileges newRevokeAllPrivileges(
return new RevokeAllPrivileges(context, metalake, role, entity);
}

protected MetalakeEnable newMetalakeEnable(
CommandContext context, String metalake, boolean enableAllCatalogs) {
return new MetalakeEnable(context, metalake, enableAllCatalogs);
protected ManageMetalake newManageMetalake(CommandContext context, String metalake) {
return new ManageMetalake(context, metalake);
}

protected MetalakeDisable newMetalakeDisable(CommandContext context, String metalake) {
return new MetalakeDisable(context, metalake);
}

protected CatalogEnable newCatalogEnable(
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
return new CatalogEnable(context, metalake, catalog, enableMetalake);
}

protected CatalogDisable newCatalogDisable(
protected ManageCatalog newManageCatalog(
CommandContext context, String metalake, String catalog) {
return new CatalogDisable(context, metalake, catalog);
return new ManageCatalog(context, metalake, catalog);
}

protected ListModel newListModel(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,63 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli.commands;

import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.MetalakeNotInUseException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

/** Enable catalog. */
public class CatalogEnable extends Command {
/** Disable or enable a catalog. */
public class ManageCatalog extends Command {
private final String metalake;
private final String catalog;
private final CommandLine line;
private final boolean enableMetalake;

/**
* Enable catalog
* Constrcut a new instance of the {@code ManageCatalog}.
*
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param enableMetalake Whether to enable it's metalake
* @param context the command context.
* @param metalake the metalake name.
* @param catalog the catalog name.
*/
public CatalogEnable(
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
public ManageCatalog(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.enableMetalake = enableMetalake;
this.line = context.line();
this.enableMetalake = line.hasOption(GravitinoOptions.ALL);
}

/** Enable catalog. */
/** Disable or enable a catalog. */
@Override
public void handle() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
enableCatalog();
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
disableCatalog();
}
}

/** {@inheritDoc} */
@Override
public Command validate() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INVALID_ENABLE_DISABLE message text can be improved.

The --enable and the --disable options are mutual exclusive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tengqm Thank you , I just change the error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be "The --enable and the --disable options are mutually exclusive."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wondering if the reading level for that is a bit high "Unable to use --enable and --disable at the same time." is easier to understand. (Grade 9 vs Grade 2) The rest of the error messages are at a lower reading grade than Grade 9.

}

return super.validate();
}

/** Enable a catalog. */
private void enableCatalog() {
try {
if (enableMetalake) {
GravitinoAdminClient adminClient = buildAdminClient();
Expand All @@ -71,4 +93,20 @@ public void handle() {

printInformation(metalake + "." + catalog + " has been enabled.");
}

/** Disable a catalog. */
private void disableCatalog() {
try {
GravitinoClient client = buildClient(metalake);
client.disableCatalog(catalog);
} catch (NoSuchMetalakeException noSuchMetalakeException) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException noSuchCatalogException) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

printInformation(metalake + "." + catalog + " has been disabled.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,56 @@
package org.apache.gravitino.cli.commands;

import java.util.Arrays;
import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.client.GravitinoMetalake;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

/** Enable metalake. */
public class MetalakeEnable extends Command {

/** Disable or enable metalake. */
public class ManageMetalake extends Command {
private final String metalake;
private final CommandLine line;
private Boolean enableAllCatalogs;

/**
* Enable a metalake
* Construct a new instance of the {@code ManageMetalake}.
*
* @param context The command context.
* @param metalake The name of the metalake.
* @param enableAllCatalogs Whether to enable all catalogs.
* @param context the command context.
* @param metalake the name of the metalake.
*/
public MetalakeEnable(CommandContext context, String metalake, boolean enableAllCatalogs) {
public ManageMetalake(CommandContext context, String metalake) {
super(context);
this.metalake = metalake;
this.enableAllCatalogs = enableAllCatalogs;
this.line = context.line();

this.enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
}

/** Enable metalake. */
/** Disable or enable the metalake. */
@Override
public void handle() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
enableMetalake();
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
disableMetalake();
}
}

/** {@inheritDoc} */
@Override
public Command validate() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
}

return super.validate();
}

/** Enable the metalake. */
private void enableMetalake() {
StringBuilder msgBuilder = new StringBuilder(metalake);
try {
GravitinoAdminClient client = buildAdminClient();
Expand All @@ -68,4 +90,18 @@ public void handle() {

printInformation(msgBuilder.toString());
}

/** Disable the metalake. */
private void disableMetalake() {
try {
GravitinoAdminClient client = buildAdminClient();
client.disableMetalake(metalake);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

printInformation(metalake + " has been disabled.");
}
}
Loading
Loading