Skip to content

Commit

Permalink
[#6414] improve(CLI): Add catalog command context CLI (#6425)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add catalog command context CLI

### Why are the changes needed?

Fix: #6414 

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

No

### How was this patch tested?

local test.
  • Loading branch information
Abyss-lord authored Feb 11, 2025
1 parent 086278e commit 1e85c3d
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,32 @@ public class CatalogCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private final CommandContext context;
private final FullName name;
private final String metalake;
private String catalog;
private final String outputFormat;

/**
* Constructs a {@link CatalogCommandHandler} 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.
* @param context The command context.
*/
public CatalogCommandHandler(
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.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
}

/** Handles the command execution logic based on the provided command. */
Expand Down Expand Up @@ -126,12 +126,9 @@ private boolean executeCommand() {
/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newCatalogAudit(url, ignore, metalake, catalog).validate().handle();
gravitinoCommandLine.newCatalogAudit(context, metalake, catalog).validate().handle();
} else {
gravitinoCommandLine
.newCatalogDetails(url, ignore, outputFormat, metalake, catalog)
.validate()
.handle();
gravitinoCommandLine.newCatalogDetails(context, metalake, catalog).validate().handle();
}
}

Expand All @@ -143,26 +140,22 @@ private void handleCreateCommand() {

Map<String, String> propertyMap = new Properties().parse(properties);
gravitinoCommandLine
.newCreateCatalog(url, ignore, metalake, catalog, provider, comment, propertyMap)
.newCreateCatalog(context, metalake, catalog, provider, comment, propertyMap)
.validate()
.handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newDeleteCatalog(url, ignore, force, metalake, catalog)
.validate()
.handle();
gravitinoCommandLine.newDeleteCatalog(context, metalake, catalog).validate().handle();
}

/** Handles the "SET" command. */
private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetCatalogProperty(url, ignore, metalake, catalog, property, value)
.newSetCatalogProperty(context, metalake, catalog, property, value)
.validate()
.handle();
}
Expand All @@ -171,17 +164,14 @@ private void handleSetCommand() {
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveCatalogProperty(url, ignore, metalake, catalog, property)
.newRemoveCatalogProperty(context, metalake, catalog, property)
.validate()
.handle();
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine
.newListCatalogProperties(url, ignore, metalake, catalog)
.validate()
.handle();
gravitinoCommandLine.newListCatalogProperties(context, metalake, catalog).validate().handle();
}

/** Handles the "UPDATE" command. */
Expand All @@ -193,32 +183,32 @@ private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableMetalake = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newCatalogEnable(url, ignore, metalake, catalog, enableMetalake)
.newCatalogEnable(context, metalake, catalog, enableMetalake)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newCatalogDisable(url, ignore, metalake, catalog).validate().handle();
gravitinoCommandLine.newCatalogDisable(context, metalake, catalog).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
String updateComment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateCatalogComment(url, ignore, metalake, catalog, updateComment)
.newUpdateCatalogComment(context, metalake, catalog, updateComment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
gravitinoCommandLine
.newUpdateCatalogName(url, ignore, metalake, catalog, newName)
.newUpdateCatalogName(context, metalake, catalog, newName)
.validate()
.handle();
}
}

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListCatalogs(url, ignore, outputFormat, metalake).validate().handle();
gravitinoCommandLine.newListCatalogs(context, metalake).validate().handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.SCHEMA)) {
new SchemaCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.CATALOG)) {
new CatalogCommandHandler(this, line, command, ignore).handle();
new CatalogCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.METALAKE)) {
new MetalakeCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.TOPIC)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,60 +203,57 @@ protected UpdateMetalakeName newUpdateMetalakeName(
return new UpdateMetalakeName(context, metalake, newName);
}

protected CatalogAudit newCatalogAudit(
String url, boolean ignore, String metalake, String catalog) {
return new CatalogAudit(url, ignore, metalake, catalog);
protected CatalogAudit newCatalogAudit(CommandContext context, String metalake, String catalog) {
return new CatalogAudit(context, metalake, catalog);
}

protected CatalogDetails newCatalogDetails(
String url, boolean ignore, String outputFormat, String metalake, String catalog) {
return new CatalogDetails(url, ignore, outputFormat, metalake, catalog);
CommandContext context, String metalake, String catalog) {
return new CatalogDetails(context, metalake, catalog);
}

protected ListCatalogs newListCatalogs(
String url, boolean ignore, String outputFormat, String metalake) {
return new ListCatalogs(url, ignore, outputFormat, metalake);
protected ListCatalogs newListCatalogs(CommandContext context, String metalake) {
return new ListCatalogs(context, metalake);
}

protected CreateCatalog newCreateCatalog(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String provider,
String comment,
Map<String, String> properties) {
return new CreateCatalog(url, ignore, metalake, catalog, provider, comment, properties);
return new CreateCatalog(context, metalake, catalog, provider, comment, properties);
}

protected DeleteCatalog newDeleteCatalog(
String url, boolean ignore, boolean force, String metalake, String catalog) {
return new DeleteCatalog(url, ignore, force, metalake, catalog);
CommandContext context, String metalake, String catalog) {
return new DeleteCatalog(context, metalake, catalog);
}

protected SetCatalogProperty newSetCatalogProperty(
String url, boolean ignore, String metalake, String catalog, String property, String value) {
return new SetCatalogProperty(url, ignore, metalake, catalog, property, value);
CommandContext context, String metalake, String catalog, String property, String value) {
return new SetCatalogProperty(context, metalake, catalog, property, value);
}

protected RemoveCatalogProperty newRemoveCatalogProperty(
String url, boolean ignore, String metalake, String catalog, String property) {
return new RemoveCatalogProperty(url, ignore, metalake, catalog, property);
CommandContext context, String metalake, String catalog, String property) {
return new RemoveCatalogProperty(context, metalake, catalog, property);
}

protected ListCatalogProperties newListCatalogProperties(
String url, boolean ignore, String metalake, String catalog) {
return new ListCatalogProperties(url, ignore, metalake, catalog);
CommandContext context, String metalake, String catalog) {
return new ListCatalogProperties(context, metalake, catalog);
}

protected UpdateCatalogComment newUpdateCatalogComment(
String url, boolean ignore, String metalake, String catalog, String comment) {
return new UpdateCatalogComment(url, ignore, metalake, catalog, comment);
CommandContext context, String metalake, String catalog, String comment) {
return new UpdateCatalogComment(context, metalake, catalog, comment);
}

protected UpdateCatalogName newUpdateCatalogName(
String url, boolean ignore, String metalake, String catalog, String newName) {
return new UpdateCatalogName(url, ignore, metalake, catalog, newName);
CommandContext context, String metalake, String catalog, String newName) {
return new UpdateCatalogName(context, metalake, catalog, newName);
}

protected SchemaAudit newSchemaAudit(
Expand Down Expand Up @@ -892,13 +889,13 @@ protected MetalakeDisable newMetalakeDisable(CommandContext context, String meta
}

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

protected CatalogDisable newCatalogDisable(
String url, boolean ignore, String metalake, String catalog) {
return new CatalogDisable(url, ignore, metalake, catalog);
CommandContext context, String metalake, String catalog) {
return new CatalogDisable(context, metalake, catalog);
}

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

import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
Expand All @@ -33,13 +34,12 @@ public class CatalogAudit extends AuditCommand {
/**
* Displays the audit information of a catalog.
*
* @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 catalog The name of the catalog.
*/
public CatalogAudit(String url, boolean ignoreVersions, String metalake, String catalog) {
super(url, ignoreVersions);
public CatalogAudit(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
Expand All @@ -33,16 +34,12 @@ public class CatalogDetails extends Command {
/**
* Displays the name and comment of a catalog.
*
* @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.
* @param catalog The name of the catalog.
*/
public CatalogDetails(
String url, boolean ignoreVersions, String outputFormat, String metalake, String catalog) {

super(url, ignoreVersions, outputFormat);
public CatalogDetails(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.gravitino.cli.commands;

import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
Expand All @@ -32,13 +33,12 @@ public class CatalogDisable extends Command {
/**
* Disable catalog
*
* @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 catalog The name of the catalog.
*/
public CatalogDisable(String url, boolean ignoreVersions, String metalake, String catalog) {
super(url, ignoreVersions);
public CatalogDisable(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
}
Expand All @@ -57,6 +57,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(metalake + "." + catalog + " has been disabled.");
printInformation(metalake + "." + catalog + " has been disabled.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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.client.GravitinoClient;
Expand All @@ -34,15 +35,14 @@ public class CatalogEnable extends Command {
/**
* Enable catalog
*
* @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 catalog The name of the catalog.
* @param enableMetalake Whether to enable it's metalake
*/
public CatalogEnable(
String url, boolean ignoreVersions, String metalake, String catalog, boolean enableMetalake) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.enableMetalake = enableMetalake;
Expand All @@ -69,6 +69,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(metalake + "." + catalog + " has been enabled.");
printInformation(metalake + "." + catalog + " has been enabled.");
}
}
Loading

0 comments on commit 1e85c3d

Please sign in to comment.