-
Notifications
You must be signed in to change notification settings - Fork 510
refactor(cli): Improve CLI commands with better error handling and output formatting #6573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 9 commits
2550c42
95077c1
552d15b
51483cb
a8cc2a4
686a9f3
163515e
409049e
d5ba1b0
69d5223
42ca8c9
349b343
108c398
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,10 +49,14 @@ public ListCatalogProperties(CommandContext context, String metalake, String cat | |
/** List the properties of a catalog. */ | ||
@Override | ||
public void handle() { | ||
Catalog gCatalog = null; | ||
GravitinoClient client = buildClient(metalake); | ||
|
||
if (gCatalog != null) { | ||
printProperties(gCatalog.properties()); | ||
} | ||
|
||
try { | ||
GravitinoClient client = buildClient(metalake); | ||
gCatalog = client.loadCatalog(catalog); | ||
Catalog gCatalog = client.loadCatalog(catalog); | ||
} catch (NoSuchMetalakeException err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh No. This code won't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will loadCatalog throw NoSuchMetalakeException? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On checking loadCatalog it will generally not throw NoSuchMetalakeException, and hence removing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original code |
||
exitWithError(ErrorMessages.UNKNOWN_METALAKE); | ||
} catch (NoSuchCatalogException err) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.gravitino.cli.commands; | ||
|
||
import com.google.common.base.Joiner; | ||
|
@@ -51,18 +50,41 @@ public ListColumns( | |
/** Displays the details of a table's columns. */ | ||
@Override | ||
public void handle() { | ||
Column[] columns = null; | ||
|
||
try { | ||
NameIdentifier name = NameIdentifier.of(schema, table); | ||
columns = tableCatalog().loadTable(name).columns(); | ||
Column[] columns = tableCatalog().loadTable(name).columns(); | ||
|
||
if (columns == null || columns.length == 0) { | ||
exitWithError("No columns found for the specified table."); | ||
return; | ||
} | ||
|
||
StringBuilder all = new StringBuilder(); | ||
all.append("name,datatype,comment,nullable,auto_increment").append(System.lineSeparator()); | ||
|
||
for (Column column : columns) { | ||
if (column == null) { | ||
continue; | ||
} | ||
|
||
all.append(column.name()).append(","); | ||
all.append(column.dataType() != null ? column.dataType().simpleString() : "UNKNOWN") | ||
.append(","); | ||
all.append(column.comment() != null ? column.comment() : "N/A").append(","); | ||
all.append(column.nullable() ? "true" : "false").append(","); | ||
all.append(column.autoIncrement() ? "true" : "false"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this column is okay and consistent with other format of outputs. |
||
all.append(System.lineSeparator()); | ||
} | ||
|
||
printResults(all.toString()); | ||
|
||
} catch (NoSuchTableException noSuchTableException) { | ||
exitWithError( | ||
ErrorMessages.UNKNOWN_TABLE + Joiner.on(".").join(metalake, catalog, schema, table)); | ||
ErrorMessages.UNKNOWN_TABLE | ||
+ " " | ||
+ Joiner.on(".").join(metalake, catalog, schema, table)); | ||
} catch (Exception exp) { | ||
exitWithError(exp.getMessage()); | ||
exitWithError("An error occurred while retrieving column details: " + exp.getMessage()); | ||
Brijeshthummar02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
printResults(columns); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,17 +46,21 @@ public ListMetalakeProperties(CommandContext context, String metalake) { | |
@Override | ||
public void handle() { | ||
Metalake gMetalake = null; | ||
try { | ||
GravitinoAdminClient client = buildAdminClient(); | ||
|
||
try (GravitinoAdminClient client = buildAdminClient()) { // Ensure resource cleanup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is likely to close the client too early. |
||
gMetalake = client.loadMetalake(metalake); | ||
} catch (NoSuchMetalakeException err) { | ||
exitWithError(ErrorMessages.UNKNOWN_METALAKE); | ||
} catch (Exception exp) { | ||
exitWithError(exp.getMessage()); | ||
} | ||
|
||
Map<String, String> properties = gMetalake.properties(); | ||
if (gMetalake == null) { | ||
exitWithError("Metalake not found: " + metalake); | ||
return; | ||
} | ||
|
||
Map<String, String> properties = gMetalake.properties(); | ||
printProperties(properties); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ public class ListTables extends TableCommand { | |
* @param context The command context. | ||
* @param metalake The name of the metalake. | ||
* @param catalog The name of the catalog. | ||
* @param schema The name of the schenma. | ||
* @param schema The name of the schema. | ||
*/ | ||
public ListTables(CommandContext context, String metalake, String catalog, String schema) { | ||
super(context, metalake, catalog); | ||
|
@@ -47,43 +47,49 @@ public ListTables(CommandContext context, String metalake, String catalog, Strin | |
/** List the names of all tables in a schema. */ | ||
@Override | ||
public void handle() { | ||
NameIdentifier[] tables = null; | ||
Namespace name = Namespace.of(schema); | ||
|
||
try { | ||
tables = tableCatalog().listTables(name); | ||
} catch (Exception exp) { | ||
exitWithError(exp.getMessage()); | ||
} | ||
Namespace name = Namespace.of(schema); | ||
NameIdentifier[] tables = tableCatalog().listTables(name); | ||
|
||
if (tables.length == 0) { | ||
printInformation("No tables exist."); | ||
return; | ||
} | ||
if (tables == null || tables.length == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code should be outside the try block |
||
printInformation("No tables exist."); | ||
return; | ||
} | ||
|
||
Table[] gTables = new Table[tables.length]; | ||
for (int i = 0; i < tables.length; i++) { | ||
String tableName = tables[i].name(); | ||
gTables[i] = | ||
new Table() { | ||
Table[] gTables = new Table[tables.length]; | ||
for (int i = 0; i < tables.length; i++) { | ||
String tableName = tables[i].name(); | ||
gTables[i] = createTableStub(tableName); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return tableName; | ||
} | ||
printResults(gTables); | ||
} catch (Exception exp) { | ||
exitWithError(exp.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public Column[] columns() { | ||
return new Column[0]; | ||
} | ||
/** | ||
* Creates a stub Table instance with only the table name. | ||
* | ||
* @param tableName The name of the table. | ||
* @return A minimal Table instance. | ||
*/ | ||
private Table createTableStub(String tableName) { | ||
return new Table() { | ||
@Override | ||
public String name() { | ||
return tableName; | ||
} | ||
|
||
@Override | ||
public Audit auditInfo() { | ||
return null; | ||
} | ||
}; | ||
} | ||
@Override | ||
public Column[] columns() { | ||
return new Column[0]; // Empty columns since only table names are needed | ||
} | ||
|
||
printResults(gTables); | ||
@Override | ||
public Audit auditInfo() { | ||
return null; // No audit info needed for listing tables | ||
} | ||
}; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.