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

jmclean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 9e3b98f12 [#6148] improve(CLI): Refactor model commands in Gavitino 
CLI (#6162)
9e3b98f12 is described below

commit 9e3b98f12adc8fa7fbda0ea0454e2325ae7334a6
Author: Lord of Abyss <[email protected]>
AuthorDate: Fri Jan 10 05:11:32 2025 +0800

    [#6148] improve(CLI): Refactor model commands in Gavitino CLI (#6162)
    
    ### What changes were proposed in this pull request?
    
    Refactor model commands and Base class in Gavitino CLI.
    
    ### Why are the changes needed?
    
    Fix: #6148
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    local test
---
 .../apache/gravitino/cli/GravitinoCommandLine.java |  82 +---------
 .../apache/gravitino/cli/ModelCommandHandler.java  | 181 +++++++++++++++++++++
 2 files changed, 182 insertions(+), 81 deletions(-)

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 950274f07..49c8b8e7c 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
@@ -151,7 +151,7 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     } else if (entity.equals(CommandEntities.ROLE)) {
       handleRoleCommand();
     } else if (entity.equals(CommandEntities.MODEL)) {
-      handleModelCommand();
+      new ModelCommandHandler(this, line, command, ignore).handle();
     }
   }
 
@@ -989,86 +989,6 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     }
   }
 
-  /**
-   * Handles the command execution for Models based on command type and the 
command line options.
-   */
-  private void handleModelCommand() {
-    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();
-    String schema = name.getSchemaName();
-
-    Command.setAuthenticationMode(auth, userName);
-
-    List<String> missingEntities = Lists.newArrayList();
-    if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
-    if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
-
-    // Handle CommandActions.LIST action separately as it doesn't require the 
`model`
-    if (CommandActions.LIST.equals(command)) {
-      checkEntities(missingEntities);
-      newListModel(url, ignore, metalake, catalog, schema).validate().handle();
-      return;
-    }
-
-    String model = name.getModelName();
-    if (model == null) missingEntities.add(CommandEntities.MODEL);
-    checkEntities(missingEntities);
-
-    switch (command) {
-      case CommandActions.DETAILS:
-        if (line.hasOption(GravitinoOptions.AUDIT)) {
-          newModelAudit(url, ignore, metalake, catalog, schema, 
model).validate().handle();
-        } else {
-          newModelDetails(url, ignore, metalake, catalog, schema, 
model).validate().handle();
-        }
-        break;
-
-      case CommandActions.DELETE:
-        boolean force = line.hasOption(GravitinoOptions.FORCE);
-        newDeleteModel(url, ignore, force, metalake, catalog, schema, 
model).validate().handle();
-        break;
-
-      case CommandActions.CREATE:
-        String createComment = line.getOptionValue(GravitinoOptions.COMMENT);
-        String[] createProperties = 
line.getOptionValues(GravitinoOptions.PROPERTIES);
-        Map<String, String> createPropertyMap = new 
Properties().parse(createProperties);
-        newCreateModel(
-                url, ignore, metalake, catalog, schema, model, createComment, 
createPropertyMap)
-            .validate()
-            .handle();
-        break;
-
-      case CommandActions.UPDATE:
-        String[] alias = line.getOptionValues(GravitinoOptions.ALIAS);
-        String uri = line.getOptionValue(GravitinoOptions.URI);
-        String linkComment = line.getOptionValue(GravitinoOptions.COMMENT);
-        String[] linkProperties = 
line.getOptionValues(CommandActions.PROPERTIES);
-        Map<String, String> linkPropertityMap = new 
Properties().parse(linkProperties);
-        newLinkModel(
-                url,
-                ignore,
-                metalake,
-                catalog,
-                schema,
-                model,
-                uri,
-                alias,
-                linkComment,
-                linkPropertityMap)
-            .validate()
-            .handle();
-        break;
-
-      default:
-        System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
-        break;
-    }
-  }
-
   /**
    * Retrieves the Gravitinno URL from the command line options or the 
GRAVITINO_URL environment
    * variable or the Gravitio config file.
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java
new file mode 100644
index 000000000..9e3c38575
--- /dev/null
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/ModelCommandHandler.java
@@ -0,0 +1,181 @@
+/*
+ * 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 java.util.Map;
+import org.apache.commons.cli.CommandLine;
+import org.apache.gravitino.cli.commands.Command;
+
+/** Handles the command execution for Models based on command type and the 
command line options. */
+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 FullName name;
+  private final String metalake;
+  private final String catalog;
+  private final String schema;
+  private String model;
+
+  /**
+   * Constructs a {@link ModelCommandHandler} 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 ModelCommandHandler(
+      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();
+    this.schema = name.getSchemaName();
+  }
+
+  /** Handles the command execution logic based on the provided command. */
+  @Override
+  protected void handle() {
+    String userName = line.getOptionValue(GravitinoOptions.LOGIN);
+    Command.setAuthenticationMode(getAuth(line), userName);
+
+    List<String> missingEntities = Lists.newArrayList();
+    if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
+    if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
+
+    // Handle CommandActions.LIST action separately as it doesn't require the 
`model`
+    if (CommandActions.LIST.equals(command)) {
+      checkEntities(missingEntities);
+      handleListCommand();
+      return;
+    }
+
+    this.model = name.getModelName();
+    if (model == null) missingEntities.add(CommandEntities.MODEL);
+    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.UPDATE:
+        handleUpdateCommand();
+        return true;
+
+      default:
+        return false;
+    }
+  }
+
+  /** Handles the "DETAILS" command. */
+  private void handleDetailsCommand() {
+    if (line.hasOption(GravitinoOptions.AUDIT)) {
+      gravitinoCommandLine
+          .newModelAudit(url, ignore, metalake, catalog, schema, model)
+          .validate()
+          .handle();
+    } else {
+      gravitinoCommandLine
+          .newModelDetails(url, ignore, metalake, catalog, schema, model)
+          .validate()
+          .handle();
+    }
+  }
+
+  /** Handles the "CREATE" command. */
+  private void handleCreateCommand() {
+    String createComment = line.getOptionValue(GravitinoOptions.COMMENT);
+    String[] createProperties = 
line.getOptionValues(GravitinoOptions.PROPERTIES);
+    Map<String, String> createPropertyMap = new 
Properties().parse(createProperties);
+    gravitinoCommandLine
+        .newCreateModel(
+            url, ignore, 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)
+        .validate()
+        .handle();
+  }
+
+  /** Handles the "UPDATE" command. */
+  private void handleUpdateCommand() {
+    String[] alias = line.getOptionValues(GravitinoOptions.ALIAS);
+    String uri = line.getOptionValue(GravitinoOptions.URI);
+    String linkComment = line.getOptionValue(GravitinoOptions.COMMENT);
+    String[] linkProperties = line.getOptionValues(CommandActions.PROPERTIES);
+    Map<String, String> linkPropertityMap = new 
Properties().parse(linkProperties);
+    gravitinoCommandLine
+        .newLinkModel(
+            url,
+            ignore,
+            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();
+  }
+}

Reply via email to