Skip to content

Commit

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

Add tags command context CLI

### Why are the changes needed?

Fix: apache#6423 

### 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 5d2a9dc commit 51c55d3
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.GROUP)) {
new GroupCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.TAG)) {
new TagCommandHandler(this, line, command, ignore).handle();
new TagCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.ROLE)) {
new RoleCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.MODEL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ public class TagCommandHandler 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 String[] tags;
private String metalake;

public TagCommandHandler(
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.tags = line.getOptionValues(GravitinoOptions.TAG);

if (tags != null) {
Expand Down Expand Up @@ -107,27 +109,26 @@ private boolean executeCommand() {
private void handleListCommand() {
FullName name = new FullName(line);
if (!name.hasCatalogName()) {
gravitinoCommandLine.newListTags(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newListTags(context, metalake).validate().handle();
} else {
gravitinoCommandLine.newListEntityTags(url, ignore, metalake, name).validate().handle();
gravitinoCommandLine.newListEntityTags(context, metalake, name).validate().handle();
}
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
gravitinoCommandLine.newTagDetails(url, ignore, metalake, getOneTag(tags)).validate().handle();
gravitinoCommandLine.newTagDetails(context, metalake, getOneTag(tags)).validate().handle();
}

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

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

/** Handles the "SET" command. */
Expand All @@ -136,12 +137,12 @@ private void handleSetCommand() {
String value = line.getOptionValue(GravitinoOptions.VALUE);
if (property == null && value == null) {
gravitinoCommandLine
.newTagEntity(url, ignore, metalake, new FullName(line), tags)
.newTagEntity(context, metalake, new FullName(line), tags)
.validate()
.handle();
} else {
gravitinoCommandLine
.newSetTagProperty(url, ignore, metalake, getOneTag(tags), property, value)
.newSetTagProperty(context, metalake, getOneTag(tags), property, value)
.validate()
.handle();
}
Expand All @@ -152,28 +153,24 @@ private void handleRemoveCommand() {
boolean isTag = line.hasOption(GravitinoOptions.TAG);
FullName name = new FullName(line);
if (!isTag) {
boolean forceRemove = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newRemoveAllTags(url, ignore, metalake, name, forceRemove)
.validate()
.handle();
gravitinoCommandLine.newRemoveAllTags(context, metalake, name).validate().handle();
} else {
String propertyRemove = line.getOptionValue(GravitinoOptions.PROPERTY);
if (propertyRemove != null) {
gravitinoCommandLine
.newRemoveTagProperty(url, ignore, metalake, getOneTag(tags), propertyRemove)
.newRemoveTagProperty(context, metalake, getOneTag(tags), propertyRemove)
.validate()
.handle();
} else {
gravitinoCommandLine.newUntagEntity(url, ignore, metalake, name, tags).validate().handle();
gravitinoCommandLine.newUntagEntity(context, metalake, name, tags).validate().handle();
}
}
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine
.newListTagProperties(url, ignore, metalake, getOneTag(tags))
.newListTagProperties(context, metalake, getOneTag(tags))
.validate()
.handle();
}
Expand All @@ -184,14 +181,14 @@ private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.COMMENT)) {
String updateComment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateTagComment(url, ignore, metalake, getOneTag(tags), updateComment)
.newUpdateTagComment(context, metalake, getOneTag(tags), updateComment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
gravitinoCommandLine
.newUpdateTagName(url, ignore, metalake, getOneTag(tags), newName)
.newUpdateTagName(context, metalake, getOneTag(tags), newName)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,67 +474,65 @@ protected DeleteRole newDeleteRole(
return new DeleteRole(url, ignore, force, metalake, roles);
}

protected TagDetails newTagDetails(String url, boolean ignore, String metalake, String tag) {
return new TagDetails(url, ignore, metalake, tag);
protected TagDetails newTagDetails(CommandContext context, String metalake, String tag) {
return new TagDetails(context, metalake, tag);
}

protected ListAllTags newListTags(String url, boolean ignore, String metalake) {
return new ListAllTags(url, ignore, metalake);
protected ListAllTags newListTags(CommandContext context, String metalake) {
return new ListAllTags(context, metalake);
}

protected CreateTag newCreateTags(
String url, boolean ignore, String metalake, String[] tags, String comment) {
return new CreateTag(url, ignore, metalake, tags, comment);
CommandContext context, String metalake, String[] tags, String comment) {
return new CreateTag(context, metalake, tags, comment);
}

protected DeleteTag newDeleteTag(
String url, boolean ignore, boolean force, String metalake, String[] tags) {
return new DeleteTag(url, ignore, force, metalake, tags);
protected DeleteTag newDeleteTag(CommandContext context, String metalake, String[] tags) {
return new DeleteTag(context, metalake, tags);
}

protected SetTagProperty newSetTagProperty(
String url, boolean ignore, String metalake, String tag, String property, String value) {
return new SetTagProperty(url, ignore, metalake, tag, property, value);
CommandContext context, String metalake, String tag, String property, String value) {
return new SetTagProperty(context, metalake, tag, property, value);
}

protected RemoveTagProperty newRemoveTagProperty(
String url, boolean ignore, String metalake, String tag, String property) {
return new RemoveTagProperty(url, ignore, metalake, tag, property);
CommandContext context, String metalake, String tag, String property) {
return new RemoveTagProperty(context, metalake, tag, property);
}

protected RemoveAllTags newRemoveAllTags(
String url, boolean ignore, String metalake, FullName name, boolean force) {
return new RemoveAllTags(url, ignore, metalake, name, force);
protected RemoveAllTags newRemoveAllTags(CommandContext context, String metalake, FullName name) {
return new RemoveAllTags(context, metalake, name);
}

protected ListTagProperties newListTagProperties(
String url, boolean ignore, String metalake, String tag) {
return new ListTagProperties(url, ignore, metalake, tag);
CommandContext context, String metalake, String tag) {
return new ListTagProperties(context, metalake, tag);
}

protected UpdateTagComment newUpdateTagComment(
String url, boolean ignore, String metalake, String tag, String comment) {
return new UpdateTagComment(url, ignore, metalake, tag, comment);
CommandContext context, String metalake, String tag, String comment) {
return new UpdateTagComment(context, metalake, tag, comment);
}

protected UpdateTagName newUpdateTagName(
String url, boolean ignore, String metalake, String tag, String newName) {
return new UpdateTagName(url, ignore, metalake, tag, newName);
CommandContext context, String metalake, String tag, String newName) {
return new UpdateTagName(context, metalake, tag, newName);
}

protected ListEntityTags newListEntityTags(
String url, boolean ignore, String metalake, FullName name) {
return new ListEntityTags(url, ignore, metalake, name);
CommandContext context, String metalake, FullName name) {
return new ListEntityTags(context, metalake, name);
}

protected TagEntity newTagEntity(
String url, boolean ignore, String metalake, FullName name, String[] tags) {
return new TagEntity(url, ignore, metalake, name, tags);
CommandContext context, String metalake, FullName name, String[] tags) {
return new TagEntity(context, metalake, name, tags);
}

protected UntagEntity newUntagEntity(
String url, boolean ignore, String metalake, FullName name, String[] tags) {
return new UntagEntity(url, ignore, metalake, name, tags);
CommandContext context, String metalake, FullName name, String[] tags) {
return new UntagEntity(context, metalake, name, tags);
}

protected ColumnAudit newColumnAudit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -35,15 +36,13 @@ public class CreateTag extends Command {
/**
* Create tags.
*
* @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 tags The names of the tags.
* @param comment The comment of the tag.
*/
public CreateTag(
String url, boolean ignoreVersions, String metalake, String[] tags, String comment) {
super(url, ignoreVersions);
public CreateTag(CommandContext context, String metalake, String[] tags, String comment) {
super(context);
this.metalake = metalake;
this.tags = tags;
this.comment = comment;
Expand Down Expand Up @@ -76,7 +75,7 @@ private void handleOnlyOneTag() {
exitWithError(exp.getMessage());
}

System.out.println("Tag " + tags[0] + " created");
printInformation("Tag " + tags[0] + " created");
}

private void handleMultipleTags() {
Expand All @@ -95,12 +94,12 @@ private void handleMultipleTags() {
exitWithError(exp.getMessage());
}
if (!created.isEmpty()) {
System.out.println("Tags " + String.join(",", created) + " created");
printInformation("Tags " + String.join(",", created) + " created");
}
if (created.size() < tags.length) {
List<String> remaining = Arrays.asList(tags);
remaining.removeAll(created);
System.out.println("Tags " + String.join(",", remaining) + " not created");
printInformation("Tags " + String.join(",", remaining) + " not created");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.List;
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.NoSuchMetalakeException;
Expand All @@ -37,16 +38,13 @@ public class DeleteTag extends Command {
/**
* Delete tags.
*
* @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 tags The names of the tags.
*/
public DeleteTag(
String url, boolean ignoreVersions, boolean force, String metalake, String[] tags) {
super(url, ignoreVersions);
this.force = force;
public DeleteTag(CommandContext context, String metalake, String[] tags) {
super(context);
this.force = context.force();
this.metalake = metalake;
this.tags = tags;
}
Expand All @@ -59,7 +57,7 @@ public void handle() {
}

if (tags == null || tags.length == 0) {
System.err.println(ErrorMessages.MISSING_TAG);
exitWithError(ErrorMessages.MISSING_TAG);
} else {
boolean hasOnlyOneTag = tags.length == 1;
if (hasOnlyOneTag) {
Expand Down Expand Up @@ -87,12 +85,12 @@ private void handleMultipleTags() {
exitWithError(exp.getMessage());
}
if (!deleted.isEmpty()) {
System.out.println("Tags " + String.join(",", deleted) + " deleted.");
printInformation("Tags " + String.join(",", deleted) + " deleted.");
}
if (deleted.size() < tags.length) {
List<String> remaining = Arrays.asList(tags);
remaining.removeAll(deleted);
System.out.println("Tags " + String.join(",", remaining) + " not deleted.");
printInformation("Tags " + String.join(",", remaining) + " not deleted.");
}
}

Expand All @@ -111,9 +109,9 @@ private void handleOnlyOneTag() {
}

if (deleted) {
System.out.println("Tag " + tags[0] + " deleted.");
printInformation("Tag " + tags[0] + " deleted.");
} else {
System.out.println("Tag " + tags[0] + " not deleted.");
printInformation("Tag " + tags[0] + " not deleted.");
}
}

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.NoSuchMetalakeException;
Expand All @@ -31,12 +32,11 @@ public class ListAllTags extends Command {
/**
* Lists all tags in a metalake.
*
* @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.
*/
public ListAllTags(String url, boolean ignoreVersions, String metalake) {
super(url, ignoreVersions);
public ListAllTags(CommandContext context, String metalake) {
super(context);
this.metalake = metalake;
}

Expand All @@ -53,8 +53,10 @@ public void handle() {
exitWithError(exp.getMessage());
}

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

System.out.println(all);
if (tags.length == 0) {
printInformation("No tags exist.");
} else {
printResults(String.join(",", tags));
}
}
}
Loading

0 comments on commit 51c55d3

Please sign in to comment.