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 085745526 [#5927] improvement(CLI): fix cli get multiple "Malformed 
entity name." (#5943)
085745526 is described below

commit 085745526564894c5d3c4a44901fa2337d7be7fa
Author: Lord of Abyss <103809695+abyss-l...@users.noreply.github.com>
AuthorDate: Tue Dec 24 05:36:24 2024 +0800

    [#5927] improvement(CLI): fix cli get multiple "Malformed entity name." 
(#5943)
    
    ### What changes were proposed in this pull request?
    
    If an entity name is malformed, the CLI should output 'Malformed entity
    name.' only once, instead of multiple times.
    
    ### Why are the changes needed?
    
    Fix: #5927
    
    ### Does this PR introduce _any_ user-facing change?
    
    NO
    
    ### How was this patch tested?
    
    ```bash
    bin/gcli.sh column list  -m demo_metalake --name Hive_catalog -i
    # output: Malformed entity name.
    ```
---
 .../java/org/apache/gravitino/cli/FullName.java    | 20 +++++++++-
 .../org/apache/gravitino/cli/TestFulllName.java    | 45 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
index 46a3bb92d..a2be2e52c 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
@@ -29,6 +29,8 @@ public class FullName {
   private final CommandLine line;
   private String metalakeEnv;
   private boolean matalakeSet = false;
+  private boolean hasDisplayedMissingNameInfo = true;
+  private boolean hasDisplayedMalformedInfo = true;
 
   /**
    * Constructor for the {@code FullName} class.
@@ -159,14 +161,14 @@ public class FullName {
       String[] names = line.getOptionValue(GravitinoOptions.NAME).split("\\.");
 
       if (names.length <= position) {
-        System.err.println(ErrorMessages.MALFORMED_NAME);
+        showMalformedInfo();
         return null;
       }
 
       return names[position];
     }
 
-    System.err.println(ErrorMessages.MISSING_NAME);
+    showMissingNameInfo();
     return null;
   }
 
@@ -224,4 +226,18 @@ public class FullName {
   public boolean hasColumnName() {
     return hasNamePart(4);
   }
+
+  private void showMissingNameInfo() {
+    if (hasDisplayedMissingNameInfo) {
+      System.err.println(ErrorMessages.MISSING_NAME);
+      hasDisplayedMissingNameInfo = false;
+    }
+  }
+
+  private void showMalformedInfo() {
+    if (hasDisplayedMalformedInfo) {
+      System.err.println(ErrorMessages.MALFORMED_NAME);
+      hasDisplayedMalformedInfo = false;
+    }
+  }
 }
diff --git 
a/clients/cli/src/test/java/org/apache/gravitino/cli/TestFulllName.java 
b/clients/cli/src/test/java/org/apache/gravitino/cli/TestFulllName.java
index ecde923a3..4b5e1fed7 100644
--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestFulllName.java
+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestFulllName.java
@@ -25,20 +25,37 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.MissingArgumentException;
 import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 public class TestFulllName {
 
   private Options options;
+  private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+  private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
+  private final PrintStream originalOut = System.out;
+  private final PrintStream originalErr = System.err;
 
   @BeforeEach
   public void setUp() {
     options = new GravitinoOptions().options();
+    System.setOut(new PrintStream(outContent));
+    System.setErr(new PrintStream(errContent));
+  }
+
+  @AfterEach
+  public void restoreStreams() {
+    System.setOut(originalOut);
+    System.setErr(originalErr);
   }
 
   @Test
@@ -152,4 +169,32 @@ public class TestFulllName {
     assertTrue(fullName.hasTableName());
     assertTrue(fullName.hasColumnName());
   }
+
+  @Test
+  @SuppressWarnings("DefaultCharset")
+  public void testMissingName() throws ParseException {
+    String[] args = {"column", "list", "-m", "demo_metalake", "-i"};
+    CommandLine commandLine = new DefaultParser().parse(options, args);
+    FullName fullName = new FullName(commandLine);
+    fullName.getCatalogName();
+    fullName.getSchemaName();
+    fullName.getTableName();
+    fullName.getColumnName();
+    String output = new String(errContent.toByteArray(), 
StandardCharsets.UTF_8).trim();
+    assertEquals(output, ErrorMessages.MISSING_NAME);
+  }
+
+  @Test
+  @SuppressWarnings("DefaultCharset")
+  public void testMalformedName() throws ParseException {
+    String[] args = {"column", "list", "-m", "demo_metalake", "-i", "--name", 
"Hive_catalog"};
+    CommandLine commandLine = new DefaultParser().parse(options, args);
+    FullName fullName = new FullName(commandLine);
+    fullName.getCatalogName();
+    fullName.getSchemaName();
+    fullName.getTableName();
+    fullName.getColumnName();
+    String output = new String(errContent.toByteArray(), 
StandardCharsets.UTF_8).trim();
+    assertEquals(output, ErrorMessages.MALFORMED_NAME);
+  }
 }

Reply via email to