Skip to content

Commit

Permalink
[apache#6326] improve(CLI): Make CLI more extendable and maintainable.
Browse files Browse the repository at this point in the history
Make CLI more extendable and maintainable.
  • Loading branch information
Abyss-lord committed Jan 20, 2025
1 parent abe63a0 commit 57cd8dc
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.gravitino.cli.outputs.OutputProperty;

/* Gravitino Command line */
public class GravitinoCommandLine extends TestableCommandLine {
Expand Down Expand Up @@ -108,7 +107,7 @@ public static void displayHelp(Options options) {

/** Executes the appropriate command based on the command type. */
private void executeCommand() {
OutputProperty outputProperty = OutputProperty.fromLine(line);

if (CommandActions.HELP.equals(command)) {
handleHelpCommand();
} else if (line.hasOption(GravitinoOptions.OWNER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.common.base.Joiner;
import java.io.File;
import java.io.OutputStream;

import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.GravitinoConfig;
import org.apache.gravitino.cli.KerberosData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* Defines different styles for formatting and displaying data. Each style contains a specific set
* of characters for rendering and configuration for whether to show boundaries between data rows.
*/
public enum Style {
public enum BorderStyle {
FANCY(FANCY_ASCII, false),
FANCY2(FANCY_ASCII, true),
BASIC(BASIC_ASCII, true),
Expand All @@ -44,13 +44,13 @@ public enum Style {
private final boolean showRowBoundaries;

/**
* Constructs a {@link Style} instance.
* Constructs a {@link BorderStyle} instance.
*
* @param characters the list of characters to use for the style
* @param showRowBoundaries {@code true} to show boundaries between data rows, {@code false}
* otherwise
*/
Style(ImmutableList<Character> characters, boolean showRowBoundaries) {
BorderStyle(ImmutableList<Character> characters, boolean showRowBoundaries) {
this.characters = characters;
this.showRowBoundaries = showRowBoundaries;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class OutputProperty {
false,
false,
-1,
Style.BASIC2,
BorderStyle.BASIC2,
HorizontalAlign.CENTER,
HorizontalAlign.LEFT,
HorizontalAlign.CENTER,
Expand All @@ -48,7 +48,7 @@ public class OutputProperty {
private boolean sort;
private boolean quiet;
private int limit;
private Style style;
private BorderStyle borderStyle;
private HorizontalAlign headerAlign;
private HorizontalAlign dataAlign;
private final HorizontalAlign footerAlign;
Expand All @@ -62,7 +62,7 @@ public class OutputProperty {
* @param sort Whether to sort the output.
* @param quiet Whether to suppress output.
* @param limit Maximum number of rows (-1 for unlimited).
* @param style Border style for tables.
* @param borderStyle Border style for tables.
* @param headerAlign Header text alignment.
* @param dataAlign Data cell alignment.
* @param footerAlign Footer text alignment.
Expand All @@ -74,7 +74,7 @@ public OutputProperty(
boolean sort,
boolean quiet,
int limit,
Style style,
BorderStyle borderStyle,
HorizontalAlign headerAlign,
HorizontalAlign dataAlign,
HorizontalAlign footerAlign,
Expand All @@ -84,7 +84,7 @@ public OutputProperty(
this.sort = sort;
this.quiet = quiet;
this.limit = limit;
this.style = style;
this.borderStyle = borderStyle;
this.headerAlign = headerAlign;
this.dataAlign = dataAlign;
this.footerAlign = footerAlign;
Expand Down Expand Up @@ -130,8 +130,8 @@ public int getLimit() {
return limit;
}

public Style getStyle() {
return style;
public BorderStyle getStyle() {
return borderStyle;
}

public HorizontalAlign getHeaderAlign() {
Expand Down Expand Up @@ -174,8 +174,8 @@ public void setLimit(int limit) {
this.limit = limit;
}

public void setStyle(Style style) {
this.style = style;
public void setStyle(BorderStyle borderStyle) {
this.borderStyle = borderStyle;
}

public void setRowNumbersEnabled(boolean rowNumbersEnabled) {
Expand All @@ -200,7 +200,7 @@ public OutputProperty copy() {
sort,
quiet,
limit,
style,
borderStyle,
headerAlign,
dataAlign,
footerAlign,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
public abstract class TableFormat<T> extends BaseOutputFormat<T> {

public static final int PADDING = 1;
private final Style style;
private final BorderStyle borderStyle;
private final boolean rowNumbersEnabled;
protected final OutputProperty property;

Expand Down Expand Up @@ -99,7 +99,7 @@ public static void output(Object entity, OutputProperty property) {
public TableFormat(OutputProperty property) {
super(property.isQuiet(), property.getLimit(), property.isSort());
this.property = property;
this.style = property.getStyle();
this.borderStyle = property.getStyle();
this.rowNumbersEnabled = property.isRowNumbersEnabled();
}

Expand All @@ -109,7 +109,7 @@ public TableFormat(OutputProperty property) {
* @param columns the columns to print.
* @return the table formatted output string.
*/
public String formatTable(Column... columns) {
public String getTableFormat(Column... columns) {
checkColumns(columns);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

Expand All @@ -125,7 +125,7 @@ public String formatTable(Column... columns) {
.filter(StringUtils::isNotBlank)
.toArray(String[]::new);

List<Character> borders = style.getCharacters();
List<Character> borders = borderStyle.getCharacters();
checkHeaders(headers, columns);

if (headers.length != columns.length) {
Expand All @@ -149,10 +149,10 @@ public String formatTable(Column... columns) {
writeUpperBorder(osw, borders, System.lineSeparator(), columns);
writeHeader(osw, borders, System.lineSeparator(), columns);
writeHeaderBorder(osw, borders, System.lineSeparator(), columns);
writeData(osw, style, columns, System.lineSeparator());
writeData(osw, borderStyle, columns, System.lineSeparator());

if (footers.length > 0) {
writeRowSeparator(osw, style, System.lineSeparator(), columns);
writeRowSeparator(osw, borderStyle, System.lineSeparator(), columns);
writeFooter(osw, borders, columns);
}

Expand Down Expand Up @@ -266,15 +266,15 @@ private static void writeHeaderBorder(
* Writes the separator line between data rows.
*
* @param writer the writer for output
* @param style the table style containing border characters
* @param borderStyle the table style containing border characters
* @param lineSeparator the system-specific line separator
* @param columns the array of columns defining the table structure
* @throws IOException if an error occurs while writing to the output
*/
private static void writeRowSeparator(
OutputStreamWriter writer, Style style, String lineSeparator, Column[] columns)
OutputStreamWriter writer, BorderStyle borderStyle, String lineSeparator, Column[] columns)
throws IOException {
List<Character> borders = style.getCharacters();
List<Character> borders = borderStyle.getCharacters();
writeHorizontalLine(
writer,
borders.get(DATA_ROW_BORDER_LEFT_IDX),
Expand Down Expand Up @@ -319,15 +319,15 @@ private static void writeBottomBorder(
* </ul>
*
* @param writer the writer for output
* @param style the table style containing border characters and row boundary settings
* @param borderStyle the table style containing border characters and row boundary settings
* @param columns the array of columns containing the data to write
* @param lineSeparator the system-specific line separator
* @throws IOException if an error occurs while writing to the output
*/
private void writeData(
OutputStreamWriter writer, Style style, Column[] columns, String lineSeparator)
OutputStreamWriter writer, BorderStyle borderStyle, Column[] columns, String lineSeparator)
throws IOException {
List<Character> borders = style.getCharacters();
List<Character> borders = borderStyle.getCharacters();
int dataSize = columns[0].getCellCount();
HorizontalAlign[] dataAligns =
Arrays.stream(columns).map(Column::getDataAlign).toArray(HorizontalAlign[]::new);
Expand All @@ -344,8 +344,8 @@ private void writeData(
dataAligns,
lineSeparator);

if (i < dataSize - 1 && style.isRowBoundariesEnabled()) {
writeRowSeparator(writer, style, lineSeparator, columns);
if (i < dataSize - 1 && borderStyle.isRowBoundariesEnabled()) {
writeRowSeparator(writer, borderStyle, lineSeparator, columns);
}
}
}
Expand Down Expand Up @@ -594,7 +594,7 @@ public String getOutput(Metalake metalake) {
columnA.addCell(metalake.name());
columnB.addCell(metalake.comment());

return formatTable(columnA, columnB);
return getTableFormat(columnA, columnB);
}
}

Expand All @@ -618,7 +618,7 @@ public String getOutput(Metalake[] metalakes) {
Column columnA = new Column("METALAKE", null, property);
Arrays.stream(metalakes).forEach(metalake -> columnA.addCell(metalake.name()));

return formatTable(columnA);
return getTableFormat(columnA);
}
}
}
Expand Down Expand Up @@ -646,7 +646,7 @@ public String getOutput(Catalog catalog) {
columnC.addCell(catalog.provider());
columnD.addCell(catalog.comment());

return formatTable(columnA, columnB, columnC, columnD);
return getTableFormat(columnA, columnB, columnC, columnD);
}
}

Expand All @@ -670,7 +670,7 @@ public String getOutput(Catalog[] catalogs) {
Column columnA = new Column("METALAKE", null, property);
Arrays.stream(catalogs).forEach(metalake -> columnA.addCell(metalake.name()));

return formatTable(columnA);
return getTableFormat(columnA);
}
}
}
Expand All @@ -693,7 +693,7 @@ public String getOutput(Schema schema) {
columnA.addCell(schema.name());
columnB.addCell(schema.comment());

return formatTable(columnA, columnB);
return getTableFormat(columnA, columnB);
}
}

Expand All @@ -716,7 +716,7 @@ public String getOutput(Schema[] schemas) {
Column column = new Column("SCHEMA", null, property);
Arrays.stream(schemas).forEach(schema -> column.addCell(schema.name()));

return formatTable(column);
return getTableFormat(column);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@

public class TestPlainFormat {
public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();
com.google.common.base.Joiner NEWLINE_JOINER =
com.google.common.base.Joiner.on(System.lineSeparator()).skipNulls();
public static final Joiner NEWLINE_JOINER = Joiner.on(System.lineSeparator()).skipNulls();
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Expand Down
Loading

0 comments on commit 57cd8dc

Please sign in to comment.