Skip to content

Commit

Permalink
refactor: Refactor output methods for no data
Browse files Browse the repository at this point in the history
  • Loading branch information
waukin committed Dec 26, 2024
1 parent ba8df39 commit 2dcb034
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public void handle() {
try {
GravitinoClient client = buildClient(metalake);
catalogs = client.listCatalogsInfo();
if (catalogs.length == 0) {
System.out.println("No catalogs exist.");
} else {
output(catalogs);
}
output(catalogs);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public void handle() {
try {
GravitinoAdminClient client = buildAdminClient();
metalakes = client.listMetalakes();
if (metalakes.length == 0) {
System.out.println("No metalakes exist.");
} else {
output(metalakes);
}
output(metalakes);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ public void output(Metalake metalake) {
static final class MetalakesPlainFormat implements OutputFormat<Metalake[]> {
@Override
public void output(Metalake[] metalakes) {
List<String> metalakeNames =
Arrays.stream(metalakes).map(Metalake::name).collect(Collectors.toList());
String all = String.join(System.lineSeparator(), metalakeNames);
System.out.println(all);
if (metalakes.length == 0) {
System.out.println("No metalakes exist.");
} else {
List<String> metalakeNames =
Arrays.stream(metalakes).map(Metalake::name).collect(Collectors.toList());
String all = String.join(System.lineSeparator(), metalakeNames);
System.out.println(all);
}
}
}

Expand All @@ -74,10 +78,14 @@ public void output(Catalog catalog) {
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);
if (catalogs.length == 0) {
System.out.println("No catalogs exist.");
} else {
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 @@ -56,13 +56,17 @@ public void output(Metalake metalake) {
static final class MetalakesTableFormat implements OutputFormat<Metalake[]> {
@Override
public void output(Metalake[] metalakes) {
List<String> headers = Collections.singletonList("metalake");
List<List<String>> rows = new ArrayList<>();
for (int i = 0; i < metalakes.length; i++) {
rows.add(Arrays.asList(metalakes[i].name()));
if (metalakes.length == 0) {
System.out.println("No metalakes exist.");
} else {
List<String> headers = Collections.singletonList("metalake");
List<List<String>> rows = new ArrayList<>();
for (int i = 0; i < metalakes.length; i++) {
rows.add(Arrays.asList(metalakes[i].name()));
}
TableFormatImpl tableFormat = new TableFormatImpl();
tableFormat.print(headers, rows);
}
TableFormatImpl tableFormat = new TableFormatImpl();
tableFormat.print(headers, rows);
}
}

Expand All @@ -85,13 +89,17 @@ 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()));
if (catalogs.length == 0) {
System.out.println("No catalogs exist.");
} else {
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);
}
TableFormatImpl tableFormat = new TableFormatImpl();
tableFormat.print(headers, rows);
}
}

Expand Down

0 comments on commit 2dcb034

Please sign in to comment.