Skip to content

Commit

Permalink
[#6326] Refactor to add a command context to CLI (#6343)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Refactor to add a command context and simple wrappers on output to make
it easy for "global" command like a "--quiet" option to be added.

Note this in progress as this only has changed Metlake. Some duplicate
code can be removed once everything is done.

### Why are the changes needed?

For maintainability.

Fix: #6326

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Tested locally.

---------

Co-authored-by: Shaofeng Shi <[email protected]>
  • Loading branch information
justinmclean and shaofengshi authored Feb 7, 2025
1 parent 3417fc3 commit 739dcea
Show file tree
Hide file tree
Showing 23 changed files with 315 additions and 172 deletions.
104 changes: 104 additions & 0 deletions clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli;

import org.apache.gravitino.cli.commands.Command;

/* Context for a command */
public class CommandContext {
private String url;
private boolean ignoreVersions;
private boolean force;
private String outputFormat;
// Can add more "global" command flags here without any major changes e.g. a guiet flag

/**
* Command constructor.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
*/
public CommandContext(String url, boolean ignoreVersions) {
this.url = url;
this.ignoreVersions = ignoreVersions;
this.force = false;
this.outputFormat = Command.OUTPUT_FORMAT_PLAIN;
}

/**
* Command constructor.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param force Force operation.
* @param outputFormat Display output format.
*/
public CommandContext(String url, boolean ignoreVersions, boolean force, String outputFormat) {
this.url = url;
this.ignoreVersions = ignoreVersions;
this.force = force;
this.outputFormat = outputFormat;
}

/**
* Returns the URL.
*
* @return The URL.
*/
public String url() {
return url;
}

/**
* Sets the URL.
*
* @param url The URL to be set.
*/
public void setUrl(String url) {
this.url = url;
}

/**
* Indicates whether versions should be ignored.
*
* @return False if versions should be ignored.
*/
public boolean ignoreVersions() {
return ignoreVersions;
}

/**
* Indicates whether the operation should be forced.
*
* @return True if the operation should be forced.
*/
public boolean force() {
return force;
}

/**
* Returns the output format.
*
* @return The output format.
*/
public String outputFormat() {
return outputFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public static void displayHelp(Options options) {

/** Executes the appropriate command based on the command type. */
private void executeCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
CommandContext context = new CommandContext(null, ignore, force, outputFormat);

if (CommandActions.HELP.equals(command)) {
handleHelpCommand();
} else if (line.hasOption(GravitinoOptions.OWNER)) {
Expand All @@ -120,7 +124,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.CATALOG)) {
new CatalogCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.METALAKE)) {
new MetalakeCommandHandler(this, line, command, ignore).handle();
new MetalakeCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.TOPIC)) {
new TopicCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.FILESET)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public class MetalakeCommandHandler extends CommandHandler {

private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final CommandContext context;
private final String command;
private final boolean ignore;
private final String url;
private String metalake;

/**
Expand All @@ -40,15 +39,18 @@ public class MetalakeCommandHandler extends CommandHandler {
* @param gravitinoCommandLine The Gravitino command line instance.
* @param line The command line arguments.
* @param command The command to execute.
* @param ignore Ignore server version mismatch.
* @param context The command context.
*/
public MetalakeCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
GravitinoCommandLine gravitinoCommandLine,
CommandLine line,
String command,
CommandContext context) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.url = getUrl(line);
this.context = context;
this.context.setUrl(getUrl(line));
}

/** Handles the command execution logic based on the provided command. */
Expand Down Expand Up @@ -112,57 +114,48 @@ private boolean executeCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
gravitinoCommandLine.newListMetalakes(url, ignore, outputFormat).validate().handle();
gravitinoCommandLine.newListMetalakes(context).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newMetalakeAudit(context, metalake).validate().handle();
} else {
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);
gravitinoCommandLine
.newMetalakeDetails(url, ignore, outputFormat, metalake)
.validate()
.handle();
gravitinoCommandLine.newMetalakeDetails(context, metalake).validate().handle();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine.newCreateMetalake(url, ignore, metalake, comment).validate().handle();
gravitinoCommandLine.newCreateMetalake(context, metalake, comment).validate().handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine.newDeleteMetalake(url, ignore, force, metalake).validate().handle();
gravitinoCommandLine.newDeleteMetalake(context, metalake).validate().handle();
}

/** Handles the "SET" command. */
private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetMetalakeProperty(url, ignore, metalake, property, value)
.newSetMetalakeProperty(context, metalake, property, value)
.validate()
.handle();
}

/** Handles the "REMOVE" command. */
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveMetalakeProperty(url, ignore, metalake, property)
.validate()
.handle();
gravitinoCommandLine.newRemoveMetalakeProperty(context, metalake, property).validate().handle();
}

/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine.newListMetalakeProperties(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newListMetalakeProperties(context, metalake).validate().handle();
}

/** Handles the "UPDATE" command. */
Expand All @@ -174,28 +167,21 @@ private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newMetalakeEnable(url, ignore, metalake, enableAllCatalogs)
.newMetalakeEnable(context, metalake, enableAllCatalogs)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newMetalakeDisable(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newMetalakeDisable(context, metalake).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateMetalakeComment(url, ignore, metalake, comment)
.validate()
.handle();
gravitinoCommandLine.newUpdateMetalakeComment(context, metalake, comment).validate().handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newUpdateMetalakeName(url, ignore, force, metalake, newName)
.validate()
.handle();
gravitinoCommandLine.newUpdateMetalakeName(context, metalake, newName).validate().handle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,52 +157,50 @@ protected ServerVersion newServerVersion(String url, boolean ignore) {
return new ServerVersion(url, ignore);
}

protected MetalakeAudit newMetalakeAudit(String url, boolean ignore, String metalake) {
return new MetalakeAudit(url, ignore, metalake);
protected MetalakeAudit newMetalakeAudit(CommandContext context, String metalake) {
return new MetalakeAudit(context, metalake);
}

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

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

protected CreateMetalake newCreateMetalake(
String url, boolean ignore, String metalake, String comment) {
return new CreateMetalake(url, ignore, metalake, comment);
CommandContext context, String metalake, String comment) {
return new CreateMetalake(context, metalake, comment);
}

protected DeleteMetalake newDeleteMetalake(
String url, boolean ignore, boolean force, String metalake) {
return new DeleteMetalake(url, ignore, force, metalake);
protected DeleteMetalake newDeleteMetalake(CommandContext context, String metalake) {
return new DeleteMetalake(context, metalake);
}

protected SetMetalakeProperty newSetMetalakeProperty(
String url, boolean ignore, String metalake, String property, String value) {
return new SetMetalakeProperty(url, ignore, metalake, property, value);
CommandContext context, String metalake, String property, String value) {
return new SetMetalakeProperty(context, metalake, property, value);
}

protected RemoveMetalakeProperty newRemoveMetalakeProperty(
String url, boolean ignore, String metalake, String property) {
return new RemoveMetalakeProperty(url, ignore, metalake, property);
CommandContext context, String metalake, String property) {
return new RemoveMetalakeProperty(context, metalake, property);
}

protected ListMetalakeProperties newListMetalakeProperties(
String url, boolean ignore, String metalake) {
return new ListMetalakeProperties(url, ignore, metalake);
CommandContext context, String metalake) {
return new ListMetalakeProperties(context, metalake);
}

protected UpdateMetalakeComment newUpdateMetalakeComment(
String url, boolean ignore, String metalake, String comment) {
return new UpdateMetalakeComment(url, ignore, metalake, comment);
CommandContext context, String metalake, String comment) {
return new UpdateMetalakeComment(context, metalake, comment);
}

protected UpdateMetalakeName newUpdateMetalakeName(
String url, boolean ignore, boolean force, String metalake, String newName) {
return new UpdateMetalakeName(url, ignore, force, metalake, newName);
CommandContext context, String metalake, String newName) {
return new UpdateMetalakeName(context, metalake, newName);
}

protected CatalogAudit newCatalogAudit(
Expand Down Expand Up @@ -908,12 +906,12 @@ protected RevokeAllPrivileges newRevokeAllPrivileges(
}

protected MetalakeEnable newMetalakeEnable(
String url, boolean ignore, String metalake, boolean enableAllCatalogs) {
return new MetalakeEnable(url, ignore, metalake, enableAllCatalogs);
CommandContext context, String metalake, boolean enableAllCatalogs) {
return new MetalakeEnable(context, metalake, enableAllCatalogs);
}

protected MetalakeDisable newMetalakeDisable(String url, boolean ignore, String metalake) {
return new MetalakeDisable(url, ignore, metalake);
protected MetalakeDisable newMetalakeDisable(CommandContext context, String metalake) {
return new MetalakeDisable(context, metalake);
}

protected CatalogEnable newCatalogEnable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.client.GravitinoAdminClient;

public class AllMetalakeDetails extends Command {

/**
* Parameters needed to list all metalakes.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param context The command context.
*/
public AllMetalakeDetails(String url, boolean ignoreVersions) {
super(url, ignoreVersions);
public AllMetalakeDetails(CommandContext context) {
super(context);
}

/** Displays the name and comment of all metalakes. */
Expand All @@ -55,6 +55,6 @@ public void handle() {

String all = Joiner.on(System.lineSeparator()).join(metalakeDetails);

System.out.print(all);
printResults(all);
}
}
Loading

0 comments on commit 739dcea

Please sign in to comment.