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 b438ef48f [#6153]refactor: Break up role commands in Gravitino CLI 
(#6170)
b438ef48f is described below

commit b438ef48f426c7d260299acbec99af79c20ac194
Author: Cheng-Yi Shih <48374270+cool9850...@users.noreply.github.com>
AuthorDate: Sat Jan 11 06:18:42 2025 +0800

    [#6153]refactor: Break up role commands in Gravitino CLI (#6170)
    
    ### What changes were proposed in this pull request?
    Break up role commands in Gravitino command line class
    
    ### Why are the changes needed?
    For readability and maintainability.
    
    ### Fix: #6153
    
    ### Does this PR introduce any user-facing change?
    None.
    
    ### How was this patch tested?
    Tested locally.
---
 .../apache/gravitino/cli/GravitinoCommandLine.java |  73 +---------
 .../apache/gravitino/cli/RoleCommandHandler.java   | 155 +++++++++++++++++++++
 2 files changed, 156 insertions(+), 72 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 74fadcb54..675a96d36 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
@@ -149,7 +149,7 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     } else if (entity.equals(CommandEntities.TAG)) {
       handleTagCommand();
     } else if (entity.equals(CommandEntities.ROLE)) {
-      handleRoleCommand();
+      new RoleCommandHandler(this, line, command, ignore).handle();
     } else if (entity.equals(CommandEntities.MODEL)) {
       new ModelCommandHandler(this, line, command, ignore).handle();
     }
@@ -401,77 +401,6 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
     return tags[0];
   }
 
-  /** Handles the command execution for Roles based on command type and the 
command line options. */
-  protected void handleRoleCommand() {
-    String url = getUrl();
-    String auth = getAuth();
-    String userName = line.getOptionValue(GravitinoOptions.LOGIN);
-    FullName name = new FullName(line);
-    String metalake = name.getMetalakeName();
-    String[] privileges = line.getOptionValues(GravitinoOptions.PRIVILEGE);
-
-    Command.setAuthenticationMode(auth, userName);
-
-    String[] roles = line.getOptionValues(GravitinoOptions.ROLE);
-    if (roles == null && !CommandActions.LIST.equals(command)) {
-      System.err.println(ErrorMessages.MISSING_ROLE);
-      Main.exit(-1);
-    }
-
-    if (roles != null) {
-      roles = Arrays.stream(roles).distinct().toArray(String[]::new);
-    }
-
-    switch (command) {
-      case CommandActions.DETAILS:
-        if (line.hasOption(GravitinoOptions.AUDIT)) {
-          newRoleAudit(url, ignore, metalake, 
getOneRole(roles)).validate().handle();
-        } else {
-          newRoleDetails(url, ignore, metalake, 
getOneRole(roles)).validate().handle();
-        }
-        break;
-
-      case CommandActions.LIST:
-        newListRoles(url, ignore, metalake).validate().handle();
-        break;
-
-      case CommandActions.CREATE:
-        newCreateRole(url, ignore, metalake, roles).validate().handle();
-        break;
-
-      case CommandActions.DELETE:
-        boolean forceDelete = line.hasOption(GravitinoOptions.FORCE);
-        newDeleteRole(url, ignore, forceDelete, metalake, 
roles).validate().handle();
-        break;
-
-      case CommandActions.GRANT:
-        newGrantPrivilegesToRole(url, ignore, metalake, getOneRole(roles), 
name, privileges)
-            .validate()
-            .handle();
-        break;
-
-      case CommandActions.REVOKE:
-        newRevokePrivilegesFromRole(url, ignore, metalake, getOneRole(roles), 
name, privileges)
-            .validate()
-            .handle();
-        break;
-
-      default:
-        System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
-        Main.exit(-1);
-        break;
-    }
-  }
-
-  private String getOneRole(String[] roles) {
-    if (roles == null || roles.length != 1) {
-      System.err.println(ErrorMessages.MULTIPLE_ROLE_COMMAND_ERROR);
-      Main.exit(-1);
-    }
-
-    return roles[0];
-  }
-
   /**
    * Handles the command execution for Columns based on command type and the 
command line options.
    */
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java
new file mode 100644
index 000000000..cf48783b8
--- /dev/null
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java
@@ -0,0 +1,155 @@
+/*
+ * 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 RoleCommandHandler extends CommandHandler {
+
+  private final GravitinoCommandLine gravitinoCommandLine;
+  private final CommandLine line;
+  private final String command;
+  private final boolean ignore;
+  private final String url;
+  private String metalake;
+  private String[] roles;
+  private String[] privileges;
+
+  public RoleCommandHandler(
+      GravitinoCommandLine gravitinoCommandLine, CommandLine line, String 
command, boolean ignore) {
+    this.gravitinoCommandLine = gravitinoCommandLine;
+    this.line = line;
+    this.command = command;
+    this.ignore = ignore;
+    this.url = getUrl(line);
+  }
+
+  /** Handles the command execution logic based on the provided command. */
+  public void handle() {
+    String auth = getAuth(line);
+    String userName = line.getOptionValue(GravitinoOptions.LOGIN);
+    Command.setAuthenticationMode(auth, userName);
+
+    metalake = new FullName(line).getMetalakeName();
+
+    roles = line.getOptionValues(GravitinoOptions.ROLE);
+    if (roles == null && !CommandActions.LIST.equals(command)) {
+      System.err.println(ErrorMessages.MISSING_ROLE);
+      Main.exit(-1);
+    }
+    if (roles != null) {
+      roles = Arrays.stream(roles).distinct().toArray(String[]::new);
+    }
+
+    privileges = line.getOptionValues(GravitinoOptions.PRIVILEGE);
+
+    if (!executeCommand()) {
+      System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
+      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.GRANT:
+        handleGrantCommand();
+        return true;
+
+      case CommandActions.REVOKE:
+        handleRevokeCommand();
+        return true;
+
+      default:
+        return false;
+    }
+  }
+
+  private void handleDetailsCommand() {
+    if (line.hasOption(GravitinoOptions.AUDIT)) {
+      gravitinoCommandLine.newRoleAudit(url, ignore, metalake, 
getOneRole()).validate().handle();
+    } else {
+      gravitinoCommandLine.newRoleDetails(url, ignore, metalake, 
getOneRole()).validate().handle();
+    }
+  }
+
+  private void handleListCommand() {
+    gravitinoCommandLine.newListRoles(url, ignore, 
metalake).validate().handle();
+  }
+
+  private void handleCreateCommand() {
+    gravitinoCommandLine.newCreateRole(url, ignore, metalake, 
roles).validate().handle();
+  }
+
+  private void handleDeleteCommand() {
+    boolean forceDelete = line.hasOption(GravitinoOptions.FORCE);
+    gravitinoCommandLine
+        .newDeleteRole(url, ignore, forceDelete, metalake, roles)
+        .validate()
+        .handle();
+  }
+
+  private void handleGrantCommand() {
+    gravitinoCommandLine
+        .newGrantPrivilegesToRole(
+            url, ignore, metalake, getOneRole(), new FullName(line), 
privileges)
+        .validate()
+        .handle();
+  }
+
+  private void handleRevokeCommand() {
+    gravitinoCommandLine
+        .newRevokePrivilegesFromRole(
+            url, ignore, metalake, getOneRole(), new FullName(line), 
privileges)
+        .validate()
+        .handle();
+  }
+
+  private String getOneRole() {
+    if (roles == null || roles.length != 1) {
+      System.err.println(ErrorMessages.MULTIPLE_ROLE_COMMAND_ERROR);
+      Main.exit(-1);
+    }
+
+    return roles[0];
+  }
+}

Reply via email to