Skip to content
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

[#6326] improve(CLI): Make CLI more extendable and maintainable. #6327

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clients/cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation(libs.guava)
implementation(libs.slf4j.api)
implementation(libs.slf4j.simple)
implementation(libs.commons.lang3)
implementation(project(":api"))
implementation(project(":clients:client-java"))
implementation(project(":common"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.commands.Command;
import org.apache.gravitino.cli.outputs.OutputProperty;

/**
* Handles the command execution for Catalogs based on command type and the command line options.
Expand All @@ -38,7 +39,6 @@ public class CatalogCommandHandler extends CommandHandler {
private final FullName name;
private final String metalake;
private String catalog;
private final String outputFormat;

/**
* Constructs a {@link CatalogCommandHandler} instance.
Expand All @@ -58,7 +58,6 @@ public CatalogCommandHandler(
this.url = getUrl(line);
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
}

/** Handles the command execution logic based on the provided command. */
Expand Down Expand Up @@ -128,8 +127,10 @@ private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newCatalogAudit(url, ignore, metalake, catalog).validate().handle();
} else {
// TODO: move this to GravitinoCommandLine class
OutputProperty property = OutputProperty.fromLine(line);
gravitinoCommandLine
.newCatalogDetails(url, ignore, outputFormat, metalake, catalog)
.newCatalogDetails(url, ignore, property, metalake, catalog)
.validate()
.handle();
}
Expand Down Expand Up @@ -219,6 +220,8 @@ private void handleUpdateCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListCatalogs(url, ignore, outputFormat, metalake).validate().handle();
// TODO: move this to GravitinoCommandLine class
OutputProperty property = OutputProperty.fromLine(line);
gravitinoCommandLine.newListCatalogs(url, ignore, property, metalake).validate().handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public static void displayHelp(Options options) {

/** Executes the appropriate command based on the command type. */
private void executeCommand() {

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 @@ -24,13 +24,17 @@

/* Gravitino Command line options */
public class GravitinoOptions {
public static final String ALIAS = "alias";
public static final String ALL = "all";
public static final String AUDIT = "audit";
public static final String AUTO = "auto";
public static final String COLUMNFILE = "columnfile";
public static final String COMMENT = "comment";
public static final String DATATYPE = "datatype";
public static final String DEFAULT = "default";
public static final String DISABLE = "disable";
public static final String DISTRIBUTION = "distribution";
public static final String ENABLE = "enable";
public static final String FILESET = "fileset";
public static final String FORCE = "force";
public static final String GROUP = "group";
Expand All @@ -49,6 +53,7 @@ public class GravitinoOptions {
public static final String PROPERTIES = "properties";
public static final String PROPERTY = "property";
public static final String PROVIDER = "provider";
public static final String QUIET = "quiet";
public static final String RENAME = "rename";
public static final String ROLE = "role";
public static final String SERVER = "server";
Expand All @@ -59,10 +64,6 @@ public class GravitinoOptions {
public static final String USER = "user";
public static final String VALUE = "value";
public static final String VERSION = "version";
public static final String ALL = "all";
public static final String ENABLE = "enable";
public static final String DISABLE = "disable";
public static final String ALIAS = "alias";
public static final String URI = "uri";

/**
Expand Down Expand Up @@ -91,6 +92,7 @@ public Options options() {
options.addOption(createSimpleOption(null, SORTORDER, "display sortorder information"));
options.addOption(createSimpleOption(null, ENABLE, "enable entities"));
options.addOption(createSimpleOption(null, DISABLE, "disable entities"));
options.addOption(createSimpleOption("q", QUIET, "disable command output"));

// Create/update options
options.addOption(createArgOption(RENAME, "new entity name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.commands.Command;
import org.apache.gravitino.cli.outputs.OutputProperty;

/**
* Handles the command execution for Metalakes based on command type and the command line options.
Expand Down Expand Up @@ -112,20 +113,19 @@ private boolean executeCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
gravitinoCommandLine.newListMetalakes(url, ignore, outputFormat).validate().handle();
// TODO: move this to GravitinoCommandLine class
OutputProperty property = OutputProperty.fromLine(line);
gravitinoCommandLine.newListMetalakes(url, ignore, property).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
OutputProperty property = OutputProperty.fromLine(line);
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle();
} else {
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
gravitinoCommandLine
.newMetalakeDetails(url, ignore, outputFormat, metalake)
.validate()
.handle();
// TODO: move this to GravitinoCommandLine class
gravitinoCommandLine.newMetalakeDetails(url, ignore, property, metalake).validate().handle();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
import org.apache.gravitino.cli.commands.UpdateTopicComment;
import org.apache.gravitino.cli.commands.UserAudit;
import org.apache.gravitino.cli.commands.UserDetails;
import org.apache.gravitino.cli.outputs.OutputProperty;

/*
* Methods used for testing
Expand All @@ -162,12 +163,12 @@ protected MetalakeAudit newMetalakeAudit(String url, boolean ignore, String meta
}

protected MetalakeDetails newMetalakeDetails(
String url, boolean ignore, String outputFormat, String metalake) {
return new MetalakeDetails(url, ignore, outputFormat, metalake);
String url, boolean ignore, OutputProperty property, String metalake) {
return new MetalakeDetails(url, ignore, property, metalake);
}

protected ListMetalakes newListMetalakes(String url, boolean ignore, String outputFormat) {
return new ListMetalakes(url, ignore, outputFormat);
protected ListMetalakes newListMetalakes(String url, boolean ignore, OutputProperty property) {
return new ListMetalakes(url, ignore, property);
}

protected CreateMetalake newCreateMetalake(
Expand Down Expand Up @@ -211,13 +212,13 @@ protected CatalogAudit newCatalogAudit(
}

protected CatalogDetails newCatalogDetails(
String url, boolean ignore, String outputFormat, String metalake, String catalog) {
return new CatalogDetails(url, ignore, outputFormat, metalake, catalog);
String url, boolean ignore, OutputProperty property, String metalake, String catalog) {
return new CatalogDetails(url, ignore, property, metalake, catalog);
}

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

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

import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -35,14 +36,17 @@ public class CatalogDetails 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 property The output format.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
*/
public CatalogDetails(
String url, boolean ignoreVersions, String outputFormat, String metalake, String catalog) {

super(url, ignoreVersions, outputFormat);
String url,
boolean ignoreVersions,
OutputProperty property,
String metalake,
String catalog) {
super(url, ignoreVersions, property);
this.metalake = metalake;
this.catalog = catalog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

package org.apache.gravitino.cli.commands;

import static org.apache.gravitino.cli.outputs.OutputProperty.OUTPUT_FORMAT_PLAIN;
import static org.apache.gravitino.cli.outputs.OutputProperty.OUTPUT_FORMAT_TABLE;
import static org.apache.gravitino.client.GravitinoClientBase.Builder;

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;
import org.apache.gravitino.cli.Main;
import org.apache.gravitino.cli.OAuthData;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.cli.outputs.PlainFormat;
import org.apache.gravitino.cli.outputs.TableFormat;
import org.apache.gravitino.client.DefaultOAuth2TokenProvider;
Expand All @@ -39,8 +43,7 @@

/* The base for all commands. */
public abstract class Command {
public static final String OUTPUT_FORMAT_TABLE = "table";
public static final String OUTPUT_FORMAT_PLAIN = "plain";

public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();

protected static String authentication = null;
Expand All @@ -51,7 +54,7 @@ public abstract class Command {
private static final String KERBEROS_AUTH = "kerberos";
private final String url;
private final boolean ignoreVersions;
private final String outputFormat;
private final OutputProperty outputProperty;

/**
* Command constructor.
Expand All @@ -60,22 +63,21 @@ public abstract class Command {
* @param ignoreVersions If true don't check the client/server versions match.
*/
public Command(String url, boolean ignoreVersions) {
this.url = url;
this.ignoreVersions = ignoreVersions;
this.outputFormat = OUTPUT_FORMAT_PLAIN;
this(url, ignoreVersions, OutputProperty.defaultOutputProperty());
}

/**
* Command constructor.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param outputFormat output format used in some commands
* @param outputProperty The output property to use for the command.
*/
public Command(String url, boolean ignoreVersions, String outputFormat) {
public Command(String url, boolean ignoreVersions, OutputProperty outputProperty) {
this.url = url;
this.ignoreVersions = ignoreVersions;
this.outputFormat = outputFormat;
this.outputProperty =
outputProperty == null ? OutputProperty.defaultOutputProperty() : outputProperty;
}

/**
Expand Down Expand Up @@ -212,15 +214,28 @@ protected <T extends GravitinoClientBase> Builder<T> constructClient(Builder<T>
* @param <T> The type of entity.
*/
protected <T> void output(T entity) {
if (outputFormat == null) {
PlainFormat.output(entity);
return;
if (OUTPUT_FORMAT_PLAIN.equals(outputProperty.getOutputFormat())) {
PlainFormat.output(entity, outputProperty);
} else if (OUTPUT_FORMAT_TABLE.equals(outputProperty.getOutputFormat())) {
TableFormat.output(entity, outputProperty);
} else {
throw new IllegalArgumentException("Unsupported output format");
}
}

protected void outputInfo(String message) {
output(message, System.out);
}

protected void outputError(String message) {
output(message, System.err);
}

if (outputFormat.equals(OUTPUT_FORMAT_TABLE)) {
TableFormat.output(entity);
} else if (outputFormat.equals(OUTPUT_FORMAT_PLAIN)) {
PlainFormat.output(entity);
protected void output(String message, OutputStream os) {
if (OUTPUT_FORMAT_PLAIN.equals(outputProperty.getOutputFormat())) {
PlainFormat.output(message, os);
} else if (OUTPUT_FORMAT_TABLE.equals(outputProperty.getOutputFormat())) {
TableFormat.output(message, os);
} else {
throw new IllegalArgumentException("Unsupported output format");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.gravitino.Catalog;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

Expand All @@ -34,11 +35,12 @@ 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 property The output property to use.
* @param metalake The name of the metalake.
*/
public ListCatalogs(String url, boolean ignoreVersions, String outputFormat, String metalake) {
super(url, ignoreVersions, outputFormat);
public ListCatalogs(
String url, boolean ignoreVersions, OutputProperty property, String metalake) {
super(url, ignoreVersions, property);
this.metalake = metalake;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import org.apache.gravitino.Metalake;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.client.GravitinoAdminClient;

/** Lists all metalakes. */
Expand All @@ -30,10 +31,10 @@ public class ListMetalakes 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 property The output format.
*/
public ListMetalakes(String url, boolean ignoreVersions, String outputFormat) {
super(url, ignoreVersions, outputFormat);
public ListMetalakes(String url, boolean ignoreVersions, OutputProperty property) {
super(url, ignoreVersions, property);
}

/** Lists all metalakes. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.gravitino.Metalake;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.outputs.OutputProperty;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

Expand All @@ -33,11 +34,12 @@ public class MetalakeDetails 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 property The output property to use.
* @param metalake The name of the metalake.
*/
public MetalakeDetails(String url, boolean ignoreVersions, String outputFormat, String metalake) {
super(url, ignoreVersions, outputFormat);
public MetalakeDetails(
String url, boolean ignoreVersions, OutputProperty property, String metalake) {
super(url, ignoreVersions, property);
this.metalake = metalake;
}

Expand Down
Loading