This is an automated email from the ASF dual-hosted git repository.

jshao pushed a commit to branch branch-0.8
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/branch-0.8 by this push:
     new e9b3946b7 [#6144] improve(CLI): Refactor schema commands in Gravitino 
CLI (#6181)
e9b3946b7 is described below

commit e9b3946b7593eb43c2948477c3bea16d7dec5557
Author: luoshipeng <[email protected]>
AuthorDate: Fri Jan 10 17:54:32 2025 +0800

    [#6144] improve(CLI): Refactor schema commands in Gravitino CLI (#6181)
    
    ### What changes were proposed in this pull request?
    
    Refactor schema commands in Gravitino CLI.
    
    ### Why are the changes needed?
    
    Fix: #6144
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    Local test
---
 .../org/apache/gravitino/cli/CommandHandler.java   |  12 +-
 .../apache/gravitino/cli/GravitinoCommandLine.java |  75 +--------
 .../apache/gravitino/cli/SchemaCommandHandler.java | 185 +++++++++++++++++++++
 3 files changed, 192 insertions(+), 80 deletions(-)

diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java
index 2b058b07a..2af2487cc 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java
@@ -34,11 +34,11 @@ public abstract class CommandHandler {
   private boolean authSet = false;
 
   /**
-   * Retrieves the Gravitinno URL from the command line options or the 
GRAVITINO_URL environment
-   * variable or the Gravitio config file.
+   * Retrieves the Gravitino URL from the command line options or the 
GRAVITINO_URL environment
+   * variable or the Gravitino config file.
    *
    * @param line The command line instance.
-   * @return The Gravitinno URL, or null if not found.
+   * @return The Gravitino URL, or null if not found.
    */
   public String getUrl(CommandLine line) {
     GravitinoConfig config = new GravitinoConfig(null);
@@ -73,11 +73,11 @@ public abstract class CommandHandler {
   }
 
   /**
-   * Retrieves the Gravitinno authentication from the command line options or 
the GRAVITINO_AUTH
-   * environment variable or the Gravitio config file.
+   * Retrieves the Gravitino authentication from the command line options or 
the GRAVITINO_AUTH
+   * environment variable or the Gravitino config file.
    *
    * @param line The command line instance.
-   * @return The Gravitinno authentication, or null if not found.
+   * @return The Gravitino authentication, or null if not found.
    */
   public String getAuth(CommandLine line) {
     // If specified on the command line use that
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 2af8a2973..74fadcb54 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
@@ -133,7 +133,7 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     } else if (entity.equals(CommandEntities.TABLE)) {
       new TableCommandHandler(this, line, command, ignore).handle();
     } else if (entity.equals(CommandEntities.SCHEMA)) {
-      handleSchemaCommand();
+      new SchemaCommandHandler(this, line, command, ignore).handle();
     } else if (entity.equals(CommandEntities.CATALOG)) {
       new CatalogCommandHandler(this, line, command, ignore).handle();
     } else if (entity.equals(CommandEntities.METALAKE)) {
@@ -240,79 +240,6 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     }
   }
 
-  /**
-   * Handles the command execution for Schemas based on command type and the 
command line options.
-   */
-  private void handleSchemaCommand() {
-    String url = getUrl();
-    String auth = getAuth();
-    String userName = line.getOptionValue(GravitinoOptions.LOGIN);
-    FullName name = new FullName(line);
-    String metalake = name.getMetalakeName();
-    String catalog = name.getCatalogName();
-
-    Command.setAuthenticationMode(auth, userName);
-
-    List<String> missingEntities = Lists.newArrayList();
-    if (metalake == null) missingEntities.add(CommandEntities.METALAKE);
-    if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
-
-    // Handle the CommandActions.LIST action separately as it doesn't use 
`schema`
-    if (CommandActions.LIST.equals(command)) {
-      checkEntities(missingEntities);
-      newListSchema(url, ignore, metalake, catalog).validate().handle();
-      return;
-    }
-
-    String schema = name.getSchemaName();
-    if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
-    checkEntities(missingEntities);
-
-    switch (command) {
-      case CommandActions.DETAILS:
-        if (line.hasOption(GravitinoOptions.AUDIT)) {
-          newSchemaAudit(url, ignore, metalake, catalog, 
schema).validate().handle();
-        } else {
-          newSchemaDetails(url, ignore, metalake, catalog, 
schema).validate().handle();
-        }
-        break;
-
-      case CommandActions.CREATE:
-        String comment = line.getOptionValue(GravitinoOptions.COMMENT);
-        newCreateSchema(url, ignore, metalake, catalog, schema, 
comment).validate().handle();
-        break;
-
-      case CommandActions.DELETE:
-        boolean force = line.hasOption(GravitinoOptions.FORCE);
-        newDeleteSchema(url, ignore, force, metalake, catalog, 
schema).validate().handle();
-        break;
-
-      case CommandActions.SET:
-        String property = line.getOptionValue(GravitinoOptions.PROPERTY);
-        String value = line.getOptionValue(GravitinoOptions.VALUE);
-        newSetSchemaProperty(url, ignore, metalake, catalog, schema, property, 
value)
-            .validate()
-            .handle();
-        break;
-
-      case CommandActions.REMOVE:
-        property = line.getOptionValue(GravitinoOptions.PROPERTY);
-        newRemoveSchemaProperty(url, ignore, metalake, catalog, schema, 
property)
-            .validate()
-            .handle();
-        break;
-
-      case CommandActions.PROPERTIES:
-        newListSchemaProperties(url, ignore, metalake, catalog, 
schema).validate().handle();
-        break;
-
-      default:
-        System.err.println(ErrorMessages.UNSUPPORTED_COMMAND);
-        Main.exit(-1);
-        break;
-    }
-  }
-
   /** Handles the command execution for Users based on command type and the 
command line options. */
   protected void handleUserCommand() {
     String url = getUrl();
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java
new file mode 100644
index 000000000..4a0cf919f
--- /dev/null
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java
@@ -0,0 +1,185 @@
+/*
+ * 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 com.google.common.collect.Lists;
+import java.util.List;
+import org.apache.commons.cli.CommandLine;
+import org.apache.gravitino.cli.commands.Command;
+
+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 FullName name;
+  private final String metalake;
+  private final String catalog;
+  private String schema;
+
+  /**
+   * Constructs a {@link SchemaCommandHandler} 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.
+   */
+  public SchemaCommandHandler(
+      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.name = new FullName(line);
+    this.metalake = name.getMetalakeName();
+    this.catalog = name.getCatalogName();
+  }
+
+  @Override
+  protected void handle() {
+    String userName = line.getOptionValue(GravitinoOptions.LOGIN);
+    Command.setAuthenticationMode(getAuth(line), userName);
+
+    List<String> missingEntities = Lists.newArrayList();
+    if (metalake == null) missingEntities.add(CommandEntities.METALAKE);
+    if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
+
+    if (CommandActions.LIST.equals(command)) {
+      checkEntities(missingEntities);
+      handleListCommand();
+      return;
+    }
+
+    this.schema = name.getSchemaName();
+    if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
+    checkEntities(missingEntities);
+
+    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.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;
+
+      default:
+        return false;
+    }
+  }
+
+  /** Handles the "LIST" command. */
+  private void handleListCommand() {
+    gravitinoCommandLine.newListSchema(url, ignore, 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();
+    } else {
+      gravitinoCommandLine
+          .newSchemaDetails(url, ignore, 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)
+        .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();
+  }
+
+  /** 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)
+        .validate()
+        .handle();
+  }
+
+  /** Handles the "REMOVE" command. */
+  private void handleRemoveCommand() {
+    String property = line.getOptionValue(GravitinoOptions.PROPERTY);
+    gravitinoCommandLine
+        .newRemoveSchemaProperty(url, ignore, metalake, catalog, schema, 
property)
+        .validate()
+        .handle();
+  }
+
+  /** Handles the "PROPERTIES" command. */
+  private void handlePropertiesCommand() {
+    gravitinoCommandLine
+        .newListSchemaProperties(url, ignore, metalake, catalog, schema)
+        .validate()
+        .handle();
+  }
+}

Reply via email to