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 3590393dbe [#5746] feat(CLI): Support table format output for Audit 
command (#6503)
3590393dbe is described below

commit 3590393dbe3dae90a45c7506537d9fb3f92f8ff3
Author: Lord of Abyss <103809695+abyss-l...@users.noreply.github.com>
AuthorDate: Tue Feb 25 13:31:46 2025 +0800

    [#5746] feat(CLI): Support table format output for Audit command (#6503)
    
    ### What changes were proposed in this pull request?
    
    Support table format output for Audit command.
    
    ### Why are the changes needed?
    
    Fix: #5746
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    local test.
    ```bash
    gcli metalake  details   --output table  -m demo_metalake --audit  -i
    
+-----------+--------------------------+-----------+--------------------------+
    |  Creator  |       Creation at        | Modifier  |       Modified at      
  |
    
+-----------+--------------------------+-----------+--------------------------+
    | anonymous | 2024-12-04T07:41:18.512Z | anonymous | 
2025-01-14T07:56:25.496Z |
    
+-----------+--------------------------+-----------+--------------------------+
    
    gcli catalog  details --name Hive_catalog  -i --output table  -m 
demo_metalake --audit
    
+-----------+--------------------------+-----------+-----------------------------+
    |  Creator  |       Creation at        | Modifier  |         Modified at    
     |
    
+-----------+--------------------------+-----------+-----------------------------+
    | anonymous | 2024-12-05T01:20:40.512Z | anonymous | 
2025-02-21T08:36:50.613886Z |
    
+-----------+--------------------------+-----------+-----------------------------+
    
    gcli schema  details --name Hive_catalog.default --output table  -m 
demo_metalake --audit -i
    +---------+-------------+----------+-------------+
    | Creator | Creation at | Modifier | Modified at |
    +---------+-------------+----------+-------------+
    | public  | N/A         | N/A      | N/A         |
    +---------+-------------+----------+-------------+
    
    gcli table  details --name Hive_catalog.default.test_dates --output table  
-m demo_metalake --audit -i
    +-----------+----------------------+----------+-------------+
    |  Creator  |     Creation at      | Modifier | Modified at |
    +-----------+----------------------+----------+-------------+
    | panchenxi | 2024-07-24T07:20:52Z | N/A      | N/A         |
    +-----------+----------------------+----------+-------------+
    ```
---
 .../gravitino/cli/commands/AuditCommand.java       | 15 ++------
 .../gravitino/cli/commands/CatalogAudit.java       |  4 +-
 .../apache/gravitino/cli/commands/SchemaAudit.java |  4 +-
 .../apache/gravitino/cli/outputs/TableFormat.java  | 30 +++++++++++++++
 .../gravitino/cli/output/TestTableFormat.java      | 44 ++++++++++++++++++++++
 5 files changed, 80 insertions(+), 17 deletions(-)

diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java
index 951ad33cc0..91477d2a20 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/AuditCommand.java
@@ -39,17 +39,10 @@ public abstract class AuditCommand extends Command {
    * @param audit from a class that implements the Auditable interface.
    */
   public void displayAuditInfo(Audit audit) {
-    String auditInfo =
-        "creator,create_time,modified,modified_time"
-            + System.lineSeparator()
-            + audit.creator()
-            + ","
-            + audit.createTime()
-            + ","
-            + audit.lastModifier()
-            + ","
-            + audit.lastModifiedTime();
+    if (audit == null) {
+      return;
+    }
 
-    printResults(auditInfo);
+    printResults(audit);
   }
 }
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java
index 6600e1d429..b46dc50ada 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogAudit.java
@@ -59,8 +59,6 @@ public class CatalogAudit extends AuditCommand {
       exitWithError(exp.getMessage());
     }
 
-    if (result != null) {
-      displayAuditInfo(result.auditInfo());
-    }
+    displayAuditInfo(result.auditInfo());
   }
 }
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaAudit.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaAudit.java
index 17a0c31e45..db01ed437b 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaAudit.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaAudit.java
@@ -66,8 +66,6 @@ public class SchemaAudit extends AuditCommand {
       exitWithError(exp.getMessage());
     }
 
-    if (result != null) {
-      displayAuditInfo(result.auditInfo());
-    }
+    displayAuditInfo(result.auditInfo());
   }
 }
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java
index e5021d62f3..7b0e6a90eb 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java
@@ -45,6 +45,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import org.apache.gravitino.Audit;
 import org.apache.gravitino.Catalog;
 import org.apache.gravitino.Metalake;
 import org.apache.gravitino.Schema;
@@ -85,6 +86,8 @@ public abstract class TableFormat<T> extends 
BaseOutputFormat<T> {
       new TableDetailsTableFormat(context).output((Table) entity);
     } else if (entity instanceof Table[]) {
       new TableListTableFormat(context).output((Table[]) entity);
+    } else if (entity instanceof Audit) {
+      new AuditTableFormat(context).output((Audit) entity);
     } else {
       throw new IllegalArgumentException("Unsupported object type");
     }
@@ -643,4 +646,31 @@ public abstract class TableFormat<T> extends 
BaseOutputFormat<T> {
       return getTableFormat(column);
     }
   }
+
+  /**
+   * Formats a single {@link Audit} instance into a four-column table display. 
Displays audit
+   * details, including creator, create time, modified, and modify time.
+   */
+  static final class AuditTableFormat extends TableFormat<Audit> {
+    public AuditTableFormat(CommandContext context) {
+      super(context);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getOutput(Audit audit) {
+      Column columnCreator = new Column(context, "creator");
+      Column columnCreateTime = new Column(context, "creation at");
+      Column columnModified = new Column(context, "modifier");
+      Column columnModifyTime = new Column(context, "modified at");
+
+      columnCreator.addCell(audit.creator());
+      columnCreateTime.addCell(audit.createTime() == null ? "N/A" : 
audit.createTime().toString());
+      columnModified.addCell(audit.lastModifier() == null ? "N/A" : 
audit.lastModifier());
+      columnModifyTime.addCell(
+          audit.lastModifiedTime() == null ? "N/A" : 
audit.lastModifiedTime().toString());
+
+      return getTableFormat(columnCreator, columnCreateTime, columnModified, 
columnModifyTime);
+    }
+  }
 }
diff --git 
a/clients/cli/src/test/java/org/apache/gravitino/cli/output/TestTableFormat.java
 
b/clients/cli/src/test/java/org/apache/gravitino/cli/output/TestTableFormat.java
index 60e9a50eea..d1210055c3 100644
--- 
a/clients/cli/src/test/java/org/apache/gravitino/cli/output/TestTableFormat.java
+++ 
b/clients/cli/src/test/java/org/apache/gravitino/cli/output/TestTableFormat.java
@@ -26,6 +26,8 @@ import static org.mockito.Mockito.when;
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import org.apache.gravitino.Audit;
 import org.apache.gravitino.Catalog;
 import org.apache.gravitino.Metalake;
 import org.apache.gravitino.Schema;
@@ -373,6 +375,48 @@ public class TestTableFormat {
         output);
   }
 
+  @Test
+  void testAuditWithTableFormat() {
+    CommandContext mockContext = getMockContext();
+    Audit mockAudit = mock(Audit.class);
+    when(mockAudit.creator()).thenReturn("demo_user");
+    
when(mockAudit.createTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));
+    when(mockAudit.lastModifier()).thenReturn("demo_user");
+    
when(mockAudit.lastModifiedTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));
+
+    TableFormat.output(mockAudit, mockContext);
+
+    String output = new String(outContent.toByteArray(), 
StandardCharsets.UTF_8).trim();
+    Assertions.assertEquals(
+        
"+-----------+--------------------------+-----------+--------------------------+\n"
+            + "|  Creator  |       Creation at        | Modifier  |       
Modified at        |\n"
+            + 
"+-----------+--------------------------+-----------+--------------------------+\n"
+            + "| demo_user | 2021-01-20T02:51:51.111Z | demo_user | 
2021-01-20T02:51:51.111Z |\n"
+            + 
"+-----------+--------------------------+-----------+--------------------------+",
+        output);
+  }
+
+  @Test
+  void testAuditWithTableFormatWithNullValues() {
+    CommandContext mockContext = getMockContext();
+    Audit mockAudit = mock(Audit.class);
+    when(mockAudit.creator()).thenReturn("demo_user");
+    when(mockAudit.createTime()).thenReturn(null);
+    when(mockAudit.lastModifier()).thenReturn(null);
+    when(mockAudit.lastModifiedTime()).thenReturn(null);
+
+    TableFormat.output(mockAudit, mockContext);
+
+    String output = new String(outContent.toByteArray(), 
StandardCharsets.UTF_8).trim();
+    Assertions.assertEquals(
+        "+-----------+-------------+----------+-------------+\n"
+            + "|  Creator  | Creation at | Modifier | Modified at |\n"
+            + "+-----------+-------------+----------+-------------+\n"
+            + "| demo_user | N/A         | N/A      | N/A         |\n"
+            + "+-----------+-------------+----------+-------------+",
+        output);
+  }
+
   @Test
   void testOutputWithUnsupportType() {
     CommandContext mockContext = getMockContext();

Reply via email to