Skip to content

Commit 8021812

Browse files
authored
[#5745] feat(CLI): Table format output for ListCatalogs command (#5759)
### What changes were proposed in this pull request? Support table format output for ListCatalogs command. ### Why are the changes needed? Issue: #5745 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? ``` gcli catalog list -m <metalake_name> gcli catalog list -m <metalake_name> --output plain gcli catalog list -m <metalake_name> --output table ```
1 parent 1d92626 commit 8021812

File tree

7 files changed

+81
-23
lines changed

7 files changed

+81
-23
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private void handleCatalogCommand() {
220220
Command.setAuthenticationMode(auth, userName);
221221

222222
if (CommandActions.LIST.equals(command)) {
223-
newListCatalogs(url, ignore, metalake).handle();
223+
newListCatalogs(url, ignore, outputFormat, metalake).handle();
224224
return;
225225
}
226226

clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ protected CatalogDetails newCatalogDetails(
197197
return new CatalogDetails(url, ignore, outputFormat, metalake, catalog);
198198
}
199199

200-
protected ListCatalogs newListCatalogs(String url, boolean ignore, String metalake) {
201-
return new ListCatalogs(url, ignore, metalake);
200+
protected ListCatalogs newListCatalogs(
201+
String url, boolean ignore, String outputFormat, String metalake) {
202+
return new ListCatalogs(url, ignore, outputFormat, metalake);
202203
}
203204

204205
protected CreateCatalog newCreateCatalog(

clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListCatalogs.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.gravitino.cli.commands;
2121

22-
import com.google.common.base.Joiner;
22+
import org.apache.gravitino.Catalog;
2323
import org.apache.gravitino.cli.ErrorMessages;
2424
import org.apache.gravitino.client.GravitinoClient;
2525
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
@@ -34,30 +34,26 @@ public class ListCatalogs extends Command {
3434
*
3535
* @param url The URL of the Gravitino server.
3636
* @param ignoreVersions If true don't check the client/server versions match.
37+
* @param outputFormat The output format.
3738
* @param metalake The name of the metalake.
3839
*/
39-
public ListCatalogs(String url, boolean ignoreVersions, String metalake) {
40-
super(url, ignoreVersions);
40+
public ListCatalogs(String url, boolean ignoreVersions, String outputFormat, String metalake) {
41+
super(url, ignoreVersions, outputFormat);
4142
this.metalake = metalake;
4243
}
4344

4445
/** Lists all catalogs in a metalake. */
4546
@Override
4647
public void handle() {
47-
String[] catalogs = new String[0];
48+
Catalog[] catalogs;
4849
try {
4950
GravitinoClient client = buildClient(metalake);
50-
catalogs = client.listCatalogs();
51+
catalogs = client.listCatalogsInfo();
52+
output(catalogs);
5153
} catch (NoSuchMetalakeException err) {
5254
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
53-
return;
5455
} catch (Exception exp) {
5556
System.err.println(exp.getMessage());
56-
return;
5757
}
58-
59-
String all = Joiner.on(",").join(catalogs);
60-
61-
System.out.println(all.toString());
6258
}
6359
}

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,26 @@
2828
public class PlainFormat {
2929
public static void output(Object object) {
3030
if (object instanceof Metalake) {
31-
new MetalakeStringFormat().output((Metalake) object);
31+
new MetalakePlainFormat().output((Metalake) object);
3232
} else if (object instanceof Metalake[]) {
33-
new MetalakesStringFormat().output((Metalake[]) object);
33+
new MetalakesPlainFormat().output((Metalake[]) object);
3434
} else if (object instanceof Catalog) {
35-
new CatalogStringFormat().output((Catalog) object);
35+
new CatalogPlainFormat().output((Catalog) object);
36+
} else if (object instanceof Catalog[]) {
37+
new CatalogsPlainFormat().output((Catalog[]) object);
3638
} else {
3739
throw new IllegalArgumentException("Unsupported object type");
3840
}
3941
}
4042

41-
static final class MetalakeStringFormat implements OutputFormat<Metalake> {
43+
static final class MetalakePlainFormat implements OutputFormat<Metalake> {
4244
@Override
4345
public void output(Metalake metalake) {
4446
System.out.println(metalake.name() + "," + metalake.comment());
4547
}
4648
}
4749

48-
static final class MetalakesStringFormat implements OutputFormat<Metalake[]> {
50+
static final class MetalakesPlainFormat implements OutputFormat<Metalake[]> {
4951
@Override
5052
public void output(Metalake[] metalakes) {
5153
List<String> metalakeNames =
@@ -55,7 +57,7 @@ public void output(Metalake[] metalakes) {
5557
}
5658
}
5759

58-
static final class CatalogStringFormat implements OutputFormat<Catalog> {
60+
static final class CatalogPlainFormat implements OutputFormat<Catalog> {
5961
@Override
6062
public void output(Catalog catalog) {
6163
System.out.println(
@@ -68,4 +70,14 @@ public void output(Catalog catalog) {
6870
+ catalog.comment());
6971
}
7072
}
73+
74+
static final class CatalogsPlainFormat implements OutputFormat<Catalog[]> {
75+
@Override
76+
public void output(Catalog[] catalogs) {
77+
List<String> catalogNames =
78+
Arrays.stream(catalogs).map(Catalog::name).collect(Collectors.toList());
79+
String all = String.join(System.lineSeparator(), catalogNames);
80+
System.out.println(all);
81+
}
82+
}
7183
}

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static void output(Object object) {
3535
new MetalakesTableFormat().output((Metalake[]) object);
3636
} else if (object instanceof Catalog) {
3737
new CatalogTableFormat().output((Catalog) object);
38+
} else if (object instanceof Catalog[]) {
39+
new CatalogsTableFormat().output((Catalog[]) object);
3840
} else {
3941
throw new IllegalArgumentException("Unsupported object type");
4042
}
@@ -80,6 +82,19 @@ public void output(Catalog catalog) {
8082
}
8183
}
8284

85+
static final class CatalogsTableFormat implements OutputFormat<Catalog[]> {
86+
@Override
87+
public void output(Catalog[] catalogs) {
88+
List<String> headers = Collections.singletonList("catalog");
89+
List<List<String>> rows = new ArrayList<>();
90+
for (int i = 0; i < catalogs.length; i++) {
91+
rows.add(Arrays.asList(catalogs[i].name()));
92+
}
93+
TableFormatImpl tableFormat = new TableFormatImpl();
94+
tableFormat.print(headers, rows);
95+
}
96+
}
97+
8398
static final class TableFormatImpl {
8499
private int[] maxElementLengths;
85100
// This expression is primarily used to match characters that have a display width of

clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void testListCatalogsCommand() {
6262
mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.LIST));
6363
doReturn(mockList)
6464
.when(commandLine)
65-
.newListCatalogs(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");
65+
.newListCatalogs(GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo");
6666
commandLine.handleCommandLine();
6767
verify(mockList).handle();
6868
}

clients/cli/src/test/java/org/apache/gravitino/cli/integration/test/TableFormatOutputIT.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.charset.StandardCharsets;
2626
import org.apache.gravitino.cli.GravitinoOptions;
2727
import org.apache.gravitino.cli.Main;
28+
import org.apache.gravitino.cli.commands.Command;
2829
import org.apache.gravitino.integration.test.util.BaseIT;
2930
import org.junit.jupiter.api.BeforeAll;
3031
import org.junit.jupiter.api.Test;
@@ -93,7 +94,7 @@ public void testMetalakeListCommand() {
9394
"metalake",
9495
"list",
9596
commandArg(GravitinoOptions.OUTPUT),
96-
"table",
97+
Command.OUTPUT_FORMAT_TABLE,
9798
commandArg(GravitinoOptions.URL),
9899
gravitinoUrl
99100
};
@@ -125,7 +126,7 @@ public void testMetalakeDetailsCommand() {
125126
commandArg(GravitinoOptions.METALAKE),
126127
"my_metalake",
127128
commandArg(GravitinoOptions.OUTPUT),
128-
"table",
129+
Command.OUTPUT_FORMAT_TABLE,
129130
commandArg(GravitinoOptions.URL),
130131
gravitinoUrl
131132
};
@@ -144,6 +145,39 @@ public void testMetalakeDetailsCommand() {
144145
output);
145146
}
146147

148+
@Test
149+
public void testCatalogListCommand() {
150+
// Create a byte array output stream to capture the output of the command
151+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
152+
PrintStream originalOut = System.out;
153+
System.setOut(new PrintStream(outputStream));
154+
155+
String[] args = {
156+
"catalog",
157+
"list",
158+
commandArg(GravitinoOptions.METALAKE),
159+
"my_metalake",
160+
commandArg(GravitinoOptions.OUTPUT),
161+
Command.OUTPUT_FORMAT_TABLE,
162+
commandArg(GravitinoOptions.URL),
163+
gravitinoUrl
164+
};
165+
Main.main(args);
166+
167+
// Restore the original System.out
168+
System.setOut(originalOut);
169+
// Get the output and verify it
170+
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
171+
assertEquals(
172+
"+-----------+\n"
173+
+ "| catalog |\n"
174+
+ "+-----------+\n"
175+
+ "| postgres |\n"
176+
+ "| postgres2 |\n"
177+
+ "+-----------+",
178+
output);
179+
}
180+
147181
@Test
148182
public void testCatalogDetailsCommand() {
149183
// Create a byte array output stream to capture the output of the command

0 commit comments

Comments
 (0)