Skip to content

Commit

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

Add model command context CLI

### Why are the changes needed?

Fix: apache#6417 

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

No

### How was this patch tested?

local test
  • Loading branch information
Abyss-lord authored and youngyjd committed Feb 14, 2025
1 parent 86bc865 commit 8651fa0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.ROLE)) {
new RoleCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.MODEL)) {
new ModelCommandHandler(this, line, command, ignore).handle();
new ModelCommandHandler(this, line, command, context).handle();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class ModelCommandHandler 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 @@ -44,16 +43,19 @@ public class ModelCommandHandler 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 ModelCommandHandler(
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.url = getUrl(line);
this.context = context;
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down Expand Up @@ -119,12 +121,12 @@ private boolean executeCommand() {
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine
.newModelAudit(url, ignore, metalake, catalog, schema, model)
.newModelAudit(context, metalake, catalog, schema, model)
.validate()
.handle();
} else {
gravitinoCommandLine
.newModelDetails(url, ignore, metalake, catalog, schema, model)
.newModelDetails(context, metalake, catalog, schema, model)
.validate()
.handle();
}
Expand All @@ -136,17 +138,15 @@ private void handleCreateCommand() {
String[] createProperties = line.getOptionValues(GravitinoOptions.PROPERTIES);
Map<String, String> createPropertyMap = new Properties().parse(createProperties);
gravitinoCommandLine
.newCreateModel(
url, ignore, metalake, catalog, schema, model, createComment, createPropertyMap)
.newCreateModel(context, metalake, catalog, schema, model, createComment, createPropertyMap)
.validate()
.handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newDeleteModel(url, ignore, force, metalake, catalog, schema, model)
.newDeleteModel(context, metalake, catalog, schema, model)
.validate()
.handle();
}
Expand All @@ -160,22 +160,13 @@ private void handleUpdateCommand() {
Map<String, String> linkPropertityMap = new Properties().parse(linkProperties);
gravitinoCommandLine
.newLinkModel(
url,
ignore,
metalake,
catalog,
schema,
model,
uri,
alias,
linkComment,
linkPropertityMap)
context, metalake, catalog, schema, model, uri, alias, linkComment, linkPropertityMap)
.validate()
.handle();
}

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListModel(url, ignore, metalake, catalog, schema).validate().handle();
gravitinoCommandLine.newListModel(context, metalake, catalog, schema).validate().handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -903,46 +903,38 @@ protected CatalogDisable newCatalogDisable(
}

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

protected ModelAudit newModelAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelAudit(url, ignore, metalake, catalog, schema, model);
CommandContext context, String metalake, String catalog, String schema, String model) {
return new ModelAudit(context, metalake, catalog, schema, model);
}

protected ModelDetails newModelDetails(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelDetails(url, ignore, metalake, catalog, schema, model);
CommandContext context, String metalake, String catalog, String schema, String model) {
return new ModelDetails(context, metalake, catalog, schema, model);
}

protected RegisterModel newCreateModel(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String model,
String comment,
Map<String, String> properties) {
return new RegisterModel(url, ignore, metalake, catalog, schema, model, comment, properties);
return new RegisterModel(context, metalake, catalog, schema, model, comment, properties);
}

protected DeleteModel newDeleteModel(
String url,
boolean ignore,
boolean force,
String metalake,
String catalog,
String schema,
String model) {
return new DeleteModel(url, ignore, force, metalake, catalog, schema, model);
CommandContext context, String metalake, String catalog, String schema, String model) {
return new DeleteModel(context, metalake, catalog, schema, model);
}

protected LinkModel newLinkModel(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
Expand All @@ -952,6 +944,6 @@ protected LinkModel newLinkModel(
String comment,
Map<String, String> properties) {
return new LinkModel(
url, ignore, metalake, catalog, schema, model, uri, alias, comment, properties);
context, metalake, catalog, schema, model, uri, alias, comment, properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.gravitino.NameIdentifier;
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 @@ -40,24 +41,16 @@ public class DeleteModel extends Command {
/**
* Deletes an existing model.
*
* @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.
* @param model The name of the model.
*/
public DeleteModel(
String url,
boolean ignoreVersions,
boolean force,
String metalake,
String catalog,
String schema,
String model) {
super(url, ignoreVersions);
this.force = force;
CommandContext context, String metalake, String catalog, String schema, String model) {
super(context);
this.force = context.force();
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -88,9 +81,9 @@ public void handle() {
}

if (deleted) {
System.out.println(model + " deleted.");
printInformation(model + " deleted.");
} else {
System.out.println(model + " not deleted.");
printInformation(model + " not deleted.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Map;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
Expand All @@ -44,8 +45,7 @@ public class LinkModel extends Command {
/**
* Link a new model version to the registered model.
*
* @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 schema.
Expand All @@ -56,8 +56,7 @@ public class LinkModel extends Command {
* @param properties The properties of the model version.
*/
public LinkModel(
String url,
boolean ignoreVersions,
CommandContext context,
String metalake,
String catalog,
String schema,
Expand All @@ -66,7 +65,7 @@ public LinkModel(
String[] alias,
String comment,
Map<String, String> properties) {
super(url, ignoreVersions);
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -100,7 +99,7 @@ public void handle() {
exitWithError(err.getMessage());
}

System.out.println(
printResults(
"Linked model " + model + " to " + uri + " with aliases " + Arrays.toString(alias));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
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,13 @@ public class ListModel extends Command {
/**
* List the names of all models in 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 schema.
*/
public ListModel(
String url, boolean ignoreVersions, String metalake, String catalog, String schema) {
super(url, ignoreVersions);
public ListModel(CommandContext context, String metalake, String catalog, String schema) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -75,6 +74,6 @@ public void handle() {
? "No models exist."
: Joiner.on(",").join(Arrays.stream(models).map(model -> model.name()).iterator());

System.out.println(output);
printResults(output);
}
}
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.NameIdentifier;
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 @@ -39,21 +40,15 @@ public class ModelAudit extends AuditCommand {
/**
* Displays the audit information of a model.
*
* @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 model The name of the model.
*/
public ModelAudit(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String model) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, String schema, String model) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand All @@ -70,16 +65,16 @@ public void handle() {
ModelCatalog modelCatalog = client.loadCatalog(catalog).asModelCatalog();
result = modelCatalog.getModel(name);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
System.err.println(ErrorMessages.UNKNOWN_CATALOG);
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchModelException err) {
System.err.println(ErrorMessages.UNKNOWN_MODEL);
exitWithError(ErrorMessages.UNKNOWN_MODEL);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
exitWithError(exp.getMessage());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Arrays;
import org.apache.gravitino.NameIdentifier;
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 @@ -40,21 +41,15 @@ public class ModelDetails extends Command {
/**
* Displays the details of a model.
*
* @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 schema.
* @param model The name of model.
*/
public ModelDetails(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String model) {
super(url, ignoreVersions);
CommandContext context, String metalake, String catalog, String schema, String model) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -87,6 +82,6 @@ public void handle() {
String basicInfo =
String.format("Model name %s, latest version: %s%n", gModel.name(), gModel.latestVersion());
String versionInfo = Arrays.toString(versions);
System.out.printf(basicInfo + "versions: " + versionInfo);
printResults(basicInfo + "versions: " + versionInfo);
}
}
Loading

0 comments on commit 8651fa0

Please sign in to comment.