Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyss-lord committed Jan 20, 2025
1 parent 2bd2251 commit abe63a0
Show file tree
Hide file tree
Showing 28 changed files with 2,493 additions and 273 deletions.
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 Down Expand Up @@ -128,8 +129,11 @@ 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.defaultOutputProperty();
property.setOutputFormat(outputFormat);
gravitinoCommandLine
.newCatalogDetails(url, ignore, outputFormat, metalake, catalog)
.newCatalogDetails(url, ignore, property, metalake, catalog)
.validate()
.handle();
}
Expand Down Expand Up @@ -219,6 +223,9 @@ 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.defaultOutputProperty();
property.setOutputFormat(outputFormat);
gravitinoCommandLine.newListCatalogs(url, ignore, property, metalake).validate().handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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 @@ -107,6 +108,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 @@ -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 @@ -113,19 +114,22 @@ 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.defaultOutputProperty();
property.setOutputFormat(outputFormat);
gravitinoCommandLine.newListMetalakes(url, ignore, property).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle();
} else {
// TODO: move this to GravitinoCommandLine class
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
gravitinoCommandLine
.newMetalakeDetails(url, ignore, outputFormat, metalake)
.validate()
.handle();
OutputProperty property = OutputProperty.defaultOutputProperty();
property.setOutputFormat(outputFormat);
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,20 @@

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 +44,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 +55,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 +64,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 +215,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

0 comments on commit abe63a0

Please sign in to comment.