From 286286d78642958cd1223de46d07b2e27003fc49 Mon Sep 17 00:00:00 2001 From: Lord of Abyss <103809695+Abyss-lord@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:15:06 +0800 Subject: [PATCH] [#5808] fix(CLI): Fix improper exception throwing When a malformed name is passed to the CLI command (#5836) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What changes were proposed in this pull request? No exception should be thrown when a malformed name is passed to the CLI. Currently, passing a malformed name causes an IllegalNamespaceException. I’ve added error messages to inform the user when necessary arguments are missing. Additionally, the `FullName.getNamePart()` method no longer prints error messages, as the information it provides is limited. I think performing fine-grained argument validation in each method and providing specific hints is a better way to hint users. ### Why are the changes needed? Fix: #5808 ### Does this PR introduce _any_ user-facing change? NO ### How was this patch tested? ```bash bin/gcli.sh table list -i # output: Missing required argument(s): METALAKE, CATALOG, SCHEMA bin/gcli.sh table list -i --metalake demo_metalake # output: Missing required argument(s): CATALOG, SCHEMA bin/gcli.sh table list -i --metalake demo_metalake --name Hive_catalog # output: Missing required argument(s): SCHEMA bin/gcli.sh table list -i --metalake demo_metalake --name Hive_catalog.default # output: correct result ``` --- .../gravitino/cli/GravitinoCommandLine.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 0df9eab82ab..ab22a5d96a8 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 @@ -27,8 +27,11 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; @@ -321,6 +324,19 @@ private void handleTableCommand() { Command.setAuthenticationMode(auth, userName); if (CommandActions.LIST.equals(command)) { + List missingEntities = + Stream.of( + metalake == null ? CommandEntities.METALAKE : null, + catalog == null ? CommandEntities.CATALOG : null, + schema == null ? CommandEntities.SCHEMA : null) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + if (!missingEntities.isEmpty()) { + System.err.println( + "Missing required argument(s): " + Joiner.on(", ").join(missingEntities)); + return; + } + newListTables(url, ignore, metalake, catalog, schema).handle(); return; }