Skip to content

Commit

Permalink
[apache#5927] improvement(CLI): fix cli get multiple "Malformed entit…
Browse files Browse the repository at this point in the history
…y name." (apache#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: apache#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.
```
  • Loading branch information
Abyss-lord authored Dec 23, 2024
1 parent fb78e65 commit 0857455
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
20 changes: 18 additions & 2 deletions clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -159,14 +161,14 @@ public String getNamePart(int position) {
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;
}

Expand Down Expand Up @@ -224,4 +226,18 @@ public boolean hasTableName() {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,37 @@
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
Expand Down Expand Up @@ -152,4 +169,32 @@ public void hasPartNameColumn() throws Exception {
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);
}
}

0 comments on commit 0857455

Please sign in to comment.