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

[#6416] improve(CLI): Add schema command context CLI #6427

Merged
merged 1 commit into from
Feb 11, 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 @@ -120,7 +120,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.TABLE)) {
new TableCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.SCHEMA)) {
new SchemaCommandHandler(this, line, command, ignore).handle();
new SchemaCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.CATALOG)) {
new CatalogCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.METALAKE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class SchemaCommandHandler 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 final String catalog;
Expand All @@ -42,16 +41,19 @@ public class SchemaCommandHandler extends CommandHandler {
* @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 SchemaCommandHandler(
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.catalog = name.getCatalogName();
Expand Down Expand Up @@ -120,48 +122,38 @@ private boolean executeCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListSchema(url, ignore, metalake, catalog).validate().handle();
gravitinoCommandLine.newListSchema(context, metalake, catalog).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine
.newSchemaAudit(url, ignore, metalake, catalog, schema)
.validate()
.handle();
gravitinoCommandLine.newSchemaAudit(context, metalake, catalog, schema).validate().handle();
} else {
gravitinoCommandLine
.newSchemaDetails(url, ignore, metalake, catalog, schema)
.validate()
.handle();
gravitinoCommandLine.newSchemaDetails(context, metalake, catalog, schema).validate().handle();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newCreateSchema(url, ignore, metalake, catalog, schema, comment)
.newCreateSchema(context, metalake, catalog, schema, comment)
.validate()
.handle();
}

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

/** Handles the "SET" command. */
private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetSchemaProperty(url, ignore, metalake, catalog, schema, property, value)
.newSetSchemaProperty(context, metalake, catalog, schema, property, value)
.validate()
.handle();
}
Expand All @@ -170,15 +162,15 @@ private void handleSetCommand() {
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveSchemaProperty(url, ignore, metalake, catalog, schema, property)
.newRemoveSchemaProperty(context, metalake, catalog, schema, property)
.validate()
.handle();
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine
.newListSchemaProperties(url, ignore, metalake, catalog, schema)
.newListSchemaProperties(context, metalake, catalog, schema)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,48 +260,47 @@ protected UpdateCatalogName newUpdateCatalogName(
}

protected SchemaAudit newSchemaAudit(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new SchemaAudit(url, ignore, metalake, catalog, schema);
CommandContext context, String metalake, String catalog, String schema) {
return new SchemaAudit(context, metalake, catalog, schema);
}

protected SchemaDetails newSchemaDetails(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new SchemaDetails(url, ignore, metalake, catalog, schema);
CommandContext context, String metalake, String catalog, String schema) {
return new SchemaDetails(context, metalake, catalog, schema);
}

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

protected CreateSchema newCreateSchema(
String url, boolean ignore, String metalake, String catalog, String schema, String comment) {
return new CreateSchema(url, ignore, metalake, catalog, schema, comment);
CommandContext context, String metalake, String catalog, String schema, String comment) {
return new CreateSchema(context, metalake, catalog, schema, comment);
}

protected DeleteSchema newDeleteSchema(
String url, boolean ignore, boolean force, String metalake, String catalog, String schema) {
return new DeleteSchema(url, ignore, force, metalake, catalog, schema);
CommandContext context, String metalake, String catalog, String schema) {
return new DeleteSchema(context, metalake, catalog, schema);
}

protected SetSchemaProperty newSetSchemaProperty(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String property,
String value) {
return new SetSchemaProperty(url, ignore, metalake, catalog, schema, property, value);
return new SetSchemaProperty(context, metalake, catalog, schema, property, value);
}

protected RemoveSchemaProperty newRemoveSchemaProperty(
String url, boolean ignore, String metalake, String catalog, String schema, String property) {
return new RemoveSchemaProperty(url, ignore, metalake, catalog, schema, property);
CommandContext context, String metalake, String catalog, String schema, String property) {
return new RemoveSchemaProperty(context, metalake, catalog, schema, property);
}

protected ListSchemaProperties newListSchemaProperties(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new ListSchemaProperties(url, ignore, metalake, catalog, schema);
CommandContext context, String metalake, String catalog, String schema) {
return new ListSchemaProperties(context, metalake, catalog, schema);
}

protected TableAudit newTableAudit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -34,21 +35,15 @@ public class CreateSchema extends Command {
/**
* Create a new schema.
*
* @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 schema The name of the schema.
* @param comment The schema's comment.
*/
public CreateSchema(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String comment) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, String schema, String comment) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand All @@ -71,6 +66,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(schema + " created");
printInformation(schema + " created");
}
}
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.cli.AreYouSure;
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 @@ -36,22 +37,14 @@ public class DeleteSchema extends Command {
/**
* Delete a schema.
*
* @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 context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schema.
*/
public DeleteSchema(
String url,
boolean ignoreVersions,
boolean force,
String metalake,
String catalog,
String schema) {
super(url, ignoreVersions);
this.force = force;
public DeleteSchema(CommandContext context, String metalake, String catalog, String schema) {
super(context);
this.force = context.force();
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -80,9 +73,9 @@ public void handle() {
}

if (deleted) {
System.out.println(schema + " deleted.");
printInformation(schema + " deleted.");
} else {
System.out.println(schema + " not deleted.");
printInformation(schema + " not deleted.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import com.google.common.base.Joiner;
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 @@ -34,13 +35,12 @@ public class ListSchema extends Command {
/**
* Lists all schemas in 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 ListSchema(String url, boolean ignoreVersions, String metalake, String catalog) {
super(url, ignoreVersions);
public ListSchema(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
}
Expand All @@ -62,6 +62,6 @@ public void handle() {

String all = schemas.length == 0 ? "No schemas exist." : Joiner.on(",").join(schemas);

System.out.println(all);
printInformation(all);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Map;
import org.apache.gravitino.Schema;
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 @@ -37,15 +38,14 @@ public class ListSchemaProperties extends ListProperties {
/**
* List the properties of a schema.
*
* @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 schema The name of the schema.
*/
public ListSchemaProperties(
String url, boolean ignoreVersions, String metalake, String catalog, String schema) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, String schema) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
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.SchemaChange;
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 @@ -37,21 +38,15 @@ public class RemoveSchemaProperty extends Command {
/**
* Remove a property of a schema.
*
* @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 schema The name of the schema.
* @param property The name of the property.
*/
public RemoveSchemaProperty(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String property) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, String schema, String property) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand All @@ -75,7 +70,7 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(property + " property removed.");
printInformation(property + " property removed.");
}

@Override
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.Schema;
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 @@ -36,15 +37,13 @@ public class SchemaAudit extends AuditCommand {
/**
* Displays the audit information of a schema.
*
* @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 schema The name of the schenma.
*/
public SchemaAudit(
String url, boolean ignoreVersions, String metalake, String catalog, String schema) {
super(url, ignoreVersions);
public SchemaAudit(CommandContext context, String metalake, String catalog, String schema) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down
Loading
Loading