Skip to content

Commit

Permalink
[#5745] feat(CLI): Table format output for ListCatalogs command (#5759)
Browse files Browse the repository at this point in the history
### 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
```
  • Loading branch information
waukin authored Dec 16, 2024
1 parent 1d92626 commit 8021812
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private void handleCatalogCommand() {
Command.setAuthenticationMode(auth, userName);

if (CommandActions.LIST.equals(command)) {
newListCatalogs(url, ignore, metalake).handle();
newListCatalogs(url, ignore, outputFormat, metalake).handle();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ protected CatalogDetails newCatalogDetails(
return new CatalogDetails(url, ignore, outputFormat, metalake, catalog);
}

protected ListCatalogs newListCatalogs(String url, boolean ignore, String metalake) {
return new ListCatalogs(url, ignore, metalake);
protected ListCatalogs newListCatalogs(
String url, boolean ignore, String outputFormat, String metalake) {
return new ListCatalogs(url, ignore, outputFormat, metalake);
}

protected CreateCatalog newCreateCatalog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.apache.gravitino.cli.commands;

import com.google.common.base.Joiner;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -34,30 +34,26 @@ public class ListCatalogs extends Command {
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param outputFormat The output format.
* @param metalake The name of the metalake.
*/
public ListCatalogs(String url, boolean ignoreVersions, String metalake) {
super(url, ignoreVersions);
public ListCatalogs(String url, boolean ignoreVersions, String outputFormat, String metalake) {
super(url, ignoreVersions, outputFormat);
this.metalake = metalake;
}

/** Lists all catalogs in a metalake. */
@Override
public void handle() {
String[] catalogs = new String[0];
Catalog[] catalogs;
try {
GravitinoClient client = buildClient(metalake);
catalogs = client.listCatalogs();
catalogs = client.listCatalogsInfo();
output(catalogs);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

String all = Joiner.on(",").join(catalogs);

System.out.println(all.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@
public class PlainFormat {
public static void output(Object object) {
if (object instanceof Metalake) {
new MetalakeStringFormat().output((Metalake) object);
new MetalakePlainFormat().output((Metalake) object);
} else if (object instanceof Metalake[]) {
new MetalakesStringFormat().output((Metalake[]) object);
new MetalakesPlainFormat().output((Metalake[]) object);
} else if (object instanceof Catalog) {
new CatalogStringFormat().output((Catalog) object);
new CatalogPlainFormat().output((Catalog) object);
} else if (object instanceof Catalog[]) {
new CatalogsPlainFormat().output((Catalog[]) object);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
}

static final class MetalakeStringFormat implements OutputFormat<Metalake> {
static final class MetalakePlainFormat implements OutputFormat<Metalake> {
@Override
public void output(Metalake metalake) {
System.out.println(metalake.name() + "," + metalake.comment());
}
}

static final class MetalakesStringFormat implements OutputFormat<Metalake[]> {
static final class MetalakesPlainFormat implements OutputFormat<Metalake[]> {
@Override
public void output(Metalake[] metalakes) {
List<String> metalakeNames =
Expand All @@ -55,7 +57,7 @@ public void output(Metalake[] metalakes) {
}
}

static final class CatalogStringFormat implements OutputFormat<Catalog> {
static final class CatalogPlainFormat implements OutputFormat<Catalog> {
@Override
public void output(Catalog catalog) {
System.out.println(
Expand All @@ -68,4 +70,14 @@ public void output(Catalog catalog) {
+ catalog.comment());
}
}

static final class CatalogsPlainFormat implements OutputFormat<Catalog[]> {
@Override
public void output(Catalog[] catalogs) {
List<String> catalogNames =
Arrays.stream(catalogs).map(Catalog::name).collect(Collectors.toList());
String all = String.join(System.lineSeparator(), catalogNames);
System.out.println(all);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static void output(Object object) {
new MetalakesTableFormat().output((Metalake[]) object);
} else if (object instanceof Catalog) {
new CatalogTableFormat().output((Catalog) object);
} else if (object instanceof Catalog[]) {
new CatalogsTableFormat().output((Catalog[]) object);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -80,6 +82,19 @@ public void output(Catalog catalog) {
}
}

static final class CatalogsTableFormat implements OutputFormat<Catalog[]> {
@Override
public void output(Catalog[] catalogs) {
List<String> headers = Collections.singletonList("catalog");
List<List<String>> rows = new ArrayList<>();
for (int i = 0; i < catalogs.length; i++) {
rows.add(Arrays.asList(catalogs[i].name()));
}
TableFormatImpl tableFormat = new TableFormatImpl();
tableFormat.print(headers, rows);
}
}

static final class TableFormatImpl {
private int[] maxElementLengths;
// This expression is primarily used to match characters that have a display width of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void testListCatalogsCommand() {
mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.LIST));
doReturn(mockList)
.when(commandLine)
.newListCatalogs(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");
.newListCatalogs(GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo");
commandLine.handleCommandLine();
verify(mockList).handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.StandardCharsets;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.cli.Main;
import org.apache.gravitino.cli.commands.Command;
import org.apache.gravitino.integration.test.util.BaseIT;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -93,7 +94,7 @@ public void testMetalakeListCommand() {
"metalake",
"list",
commandArg(GravitinoOptions.OUTPUT),
"table",
Command.OUTPUT_FORMAT_TABLE,
commandArg(GravitinoOptions.URL),
gravitinoUrl
};
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testMetalakeDetailsCommand() {
commandArg(GravitinoOptions.METALAKE),
"my_metalake",
commandArg(GravitinoOptions.OUTPUT),
"table",
Command.OUTPUT_FORMAT_TABLE,
commandArg(GravitinoOptions.URL),
gravitinoUrl
};
Expand All @@ -144,6 +145,39 @@ public void testMetalakeDetailsCommand() {
output);
}

@Test
public void testCatalogListCommand() {
// Create a byte array output stream to capture the output of the command
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outputStream));

String[] args = {
"catalog",
"list",
commandArg(GravitinoOptions.METALAKE),
"my_metalake",
commandArg(GravitinoOptions.OUTPUT),
Command.OUTPUT_FORMAT_TABLE,
commandArg(GravitinoOptions.URL),
gravitinoUrl
};
Main.main(args);

// Restore the original System.out
System.setOut(originalOut);
// Get the output and verify it
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
"+-----------+\n"
+ "| catalog |\n"
+ "+-----------+\n"
+ "| postgres |\n"
+ "| postgres2 |\n"
+ "+-----------+",
output);
}

@Test
public void testCatalogDetailsCommand() {
// Create a byte array output stream to capture the output of the command
Expand Down

0 comments on commit 8021812

Please sign in to comment.