From 00e24f7d23fa53cf006510fe5417581a45ed146f Mon Sep 17 00:00:00 2001 From: frankvicky Date: Sun, 12 Jan 2025 15:48:41 +0800 Subject: [PATCH] [#6152] refactor: Refactor tag commands in Gavitino --- .../gravitino/cli/GravitinoCommandLine.java | 103 +-------- .../gravitino/cli/TagCommandHandler.java | 207 ++++++++++++++++++ 2 files changed, 208 insertions(+), 102 deletions(-) create mode 100644 clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java index 21d3ed176cb..a4f2f3e1ed1 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java @@ -26,7 +26,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.Arrays; import java.util.List; import java.util.Map; import org.apache.commons.cli.CommandLine; @@ -147,7 +146,7 @@ private void executeCommand() { } else if (entity.equals(CommandEntities.GROUP)) { new GroupCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.TAG)) { - handleTagCommand(); + new TagCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.ROLE)) { new RoleCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.MODEL)) { @@ -240,106 +239,6 @@ private void handleMetalakeCommand() { } } - /** Handles the command execution for Tags based on command type and the command line options. */ - protected void handleTagCommand() { - String url = getUrl(); - String auth = getAuth(); - String userName = line.getOptionValue(GravitinoOptions.LOGIN); - FullName name = new FullName(line); - String metalake = name.getMetalakeName(); - - Command.setAuthenticationMode(auth, userName); - - String[] tags = line.getOptionValues(GravitinoOptions.TAG); - - if (tags != null) { - tags = Arrays.stream(tags).distinct().toArray(String[]::new); - } - - switch (command) { - case CommandActions.DETAILS: - newTagDetails(url, ignore, metalake, getOneTag(tags)).validate().handle(); - break; - - case CommandActions.LIST: - if (!name.hasCatalogName()) { - newListTags(url, ignore, metalake).validate().handle(); - } else { - newListEntityTags(url, ignore, metalake, name).validate().handle(); - } - break; - - case CommandActions.CREATE: - String comment = line.getOptionValue(GravitinoOptions.COMMENT); - newCreateTags(url, ignore, metalake, tags, comment).validate().handle(); - break; - - case CommandActions.DELETE: - boolean forceDelete = line.hasOption(GravitinoOptions.FORCE); - newDeleteTag(url, ignore, forceDelete, metalake, tags).validate().handle(); - break; - - case CommandActions.SET: - String propertySet = line.getOptionValue(GravitinoOptions.PROPERTY); - String valueSet = line.getOptionValue(GravitinoOptions.VALUE); - if (propertySet == null && valueSet == null) { - newTagEntity(url, ignore, metalake, name, tags).validate().handle(); - } else { - newSetTagProperty(url, ignore, metalake, getOneTag(tags), propertySet, valueSet) - .validate() - .handle(); - } - break; - - case CommandActions.REMOVE: - boolean isTag = line.hasOption(GravitinoOptions.TAG); - if (!isTag) { - boolean forceRemove = line.hasOption(GravitinoOptions.FORCE); - newRemoveAllTags(url, ignore, metalake, name, forceRemove).validate().handle(); - } else { - String propertyRemove = line.getOptionValue(GravitinoOptions.PROPERTY); - if (propertyRemove != null) { - newRemoveTagProperty(url, ignore, metalake, getOneTag(tags), propertyRemove) - .validate() - .handle(); - } else { - newUntagEntity(url, ignore, metalake, name, tags).validate().handle(); - } - } - break; - - case CommandActions.PROPERTIES: - newListTagProperties(url, ignore, metalake, getOneTag(tags)).validate().handle(); - break; - - case CommandActions.UPDATE: - if (line.hasOption(GravitinoOptions.COMMENT)) { - String updateComment = line.getOptionValue(GravitinoOptions.COMMENT); - newUpdateTagComment(url, ignore, metalake, getOneTag(tags), updateComment) - .validate() - .handle(); - } - if (line.hasOption(GravitinoOptions.RENAME)) { - String newName = line.getOptionValue(GravitinoOptions.RENAME); - newUpdateTagName(url, ignore, metalake, getOneTag(tags), newName).validate().handle(); - } - break; - - default: - System.err.println(ErrorMessages.UNSUPPORTED_ACTION); - Main.exit(-1); - break; - } - } - - private String getOneTag(String[] tags) { - if (tags == null || tags.length > 1) { - System.err.println(ErrorMessages.MULTIPLE_TAG_COMMAND_ERROR); - Main.exit(-1); - } - return tags[0]; - } - private void handleHelpCommand() { String helpFile = entity.toLowerCase() + "_help.txt"; diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java new file mode 100644 index 00000000000..e274c271f9c --- /dev/null +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TagCommandHandler.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.cli; + +import java.util.Arrays; +import org.apache.commons.cli.CommandLine; +import org.apache.gravitino.cli.commands.Command; + +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 String[] tags; + private String metalake; + + public TagCommandHandler( + GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) { + this.gravitinoCommandLine = gravitinoCommandLine; + this.line = line; + this.command = command; + this.ignore = ignore; + this.url = getUrl(line); + this.tags = line.getOptionValues(GravitinoOptions.TAG); + + if (tags != null) { + tags = Arrays.stream(tags).distinct().toArray(String[]::new); + } + } + + @Override + public void handle() { + String userName = line.getOptionValue(GravitinoOptions.LOGIN); + FullName name = new FullName(line); + Command.setAuthenticationMode(getAuth(line), userName); + + metalake = name.getMetalakeName(); + + if (!executeCommand()) { + System.err.println(ErrorMessages.UNSUPPORTED_COMMAND); + Main.exit(-1); + } + } + + /** + * Executes the specific command based on the command type. + * + * @return true if the command is supported, false otherwise + */ + private boolean executeCommand() { + switch (command) { + case CommandActions.DETAILS: + handleDetailsCommand(); + return true; + + case CommandActions.LIST: + handleListCommand(); + return true; + + case CommandActions.CREATE: + handleCreateCommand(); + return true; + + case CommandActions.DELETE: + handleDeleteCommand(); + return true; + + case CommandActions.SET: + handleSetCommand(); + return true; + + case CommandActions.REMOVE: + handleRemoveCommand(); + return true; + + case CommandActions.PROPERTIES: + handlePropertiesCommand(); + return true; + + case CommandActions.UPDATE: + handleUpdateCommand(); + return true; + + default: + return false; + } + } + + /** Handles the "LIST" command. */ + private void handleListCommand() { + FullName name = new FullName(line); + if (!name.hasCatalogName()) { + gravitinoCommandLine.newListTags(url, ignore, metalake).validate().handle(); + } else { + gravitinoCommandLine.newListEntityTags(url, ignore, metalake, name).validate().handle(); + } + } + + /** Handles the "DETAILS" command. */ + private void handleDetailsCommand() { + gravitinoCommandLine.newTagDetails(url, ignore, 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(); + } + + /** Handles the "DELETE" command. */ + private void handleDeleteCommand() { + boolean forceDelete = line.hasOption(GravitinoOptions.FORCE); + gravitinoCommandLine.newDeleteTag(url, ignore, forceDelete, metalake, tags).validate().handle(); + } + + /** Handles the "SET" command. */ + private void handleSetCommand() { + String property = line.getOptionValue(GravitinoOptions.PROPERTY); + String value = line.getOptionValue(GravitinoOptions.VALUE); + if (property == null && value == null) { + gravitinoCommandLine + .newTagEntity(url, ignore, metalake, new FullName(line), tags) + .validate() + .handle(); + } else { + gravitinoCommandLine + .newSetTagProperty(url, ignore, metalake, getOneTag(tags), property, value) + .validate() + .handle(); + } + } + + /** Handles the "REMOVE" command. */ + 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(); + } else { + String propertyRemove = line.getOptionValue(GravitinoOptions.PROPERTY); + if (propertyRemove != null) { + gravitinoCommandLine + .newRemoveTagProperty(url, ignore, metalake, getOneTag(tags), propertyRemove) + .validate() + .handle(); + } else { + gravitinoCommandLine.newUntagEntity(url, ignore, metalake, name, tags).validate().handle(); + } + } + } + + /** Handles the "PROPERTIES" command. */ + private void handlePropertiesCommand() { + gravitinoCommandLine + .newListTagProperties(url, ignore, metalake, getOneTag(tags)) + .validate() + .handle(); + } + + /** Handles the "UPDATE" command. */ + private void handleUpdateCommand() { + + if (line.hasOption(GravitinoOptions.COMMENT)) { + String updateComment = line.getOptionValue(GravitinoOptions.COMMENT); + gravitinoCommandLine + .newUpdateTagComment(url, ignore, 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) + .validate() + .handle(); + } + } + + private String getOneTag(String[] tags) { + if (tags == null || tags.length > 1) { + System.err.println(ErrorMessages.MULTIPLE_TAG_COMMAND_ERROR); + Main.exit(-1); + } + return tags[0]; + } +}