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] Refactor to add a command context #6343

Open
wants to merge 2 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
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 @@ -156,52 +156,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 @@ -902,12 +900,12 @@ protected RevokePrivilegesFromRole newRevokePrivilegesFromRole(
}

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
Loading