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

[#6139] Refactor metalake command in Gravitino CLI #6140

Merged
merged 7 commits into from
Jan 13, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.CATALOG)) {
new CatalogCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.METALAKE)) {
handleMetalakeCommand();
new MetalakeCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.TOPIC)) {
new TopicCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.FILESET)) {
Expand All @@ -155,91 +155,6 @@ private void executeCommand() {
}
}

/**
* Handles the command execution for Metalakes based on command type and the command line options.
*/
private void handleMetalakeCommand() {
String url = getUrl();
String auth = getAuth();
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT);

Command.setAuthenticationMode(auth, userName);

if (CommandActions.LIST.equals(command)) {
newListMetalakes(url, ignore, outputFormat).validate().handle();
return;
}

String metalake = name.getMetalakeName();

switch (command) {
case CommandActions.DETAILS:
if (line.hasOption(GravitinoOptions.AUDIT)) {
newMetalakeAudit(url, ignore, metalake).validate().handle();
} else {
newMetalakeDetails(url, ignore, outputFormat, metalake).validate().handle();
}
break;

case CommandActions.CREATE:
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
newCreateMetalake(url, ignore, metalake, comment).validate().handle();
break;

case CommandActions.DELETE:
boolean force = line.hasOption(GravitinoOptions.FORCE);
newDeleteMetalake(url, ignore, force, metalake).validate().handle();
break;

case CommandActions.SET:
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
newSetMetalakeProperty(url, ignore, metalake, property, value).validate().handle();
break;

case CommandActions.REMOVE:
property = line.getOptionValue(GravitinoOptions.PROPERTY);
newRemoveMetalakeProperty(url, ignore, metalake, property).validate().handle();
break;

case CommandActions.PROPERTIES:
newListMetalakeProperties(url, ignore, metalake).validate().handle();
break;

case CommandActions.UPDATE:
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
newMetalakeEnable(url, ignore, metalake, enableAllCatalogs).validate().handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
newMetalakeDisable(url, ignore, metalake).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
comment = line.getOptionValue(GravitinoOptions.COMMENT);
newUpdateMetalakeComment(url, ignore, metalake, comment).validate().handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
force = line.hasOption(GravitinoOptions.FORCE);
newUpdateMetalakeName(url, ignore, force, metalake, newName).validate().handle();
}

break;

default:
System.err.println(ErrorMessages.UNSUPPORTED_COMMAND);
Main.exit(-1);
break;
}
}

/** Handles the command execution for Tags based on command type and the command line options. */
protected void handleTagCommand() {
String url = getUrl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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.commons.cli.CommandLine;
import org.apache.gravitino.cli.commands.Command;

/**
* Handles the command execution for Metalakes based on command type and the command line options.
*/
public class MetalakeCommandHandler extends CommandHandler {

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

/**
* Constructs a MetalakeCommandHandler instance.
*
* @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.
*/
public MetalakeCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.url = getUrl(line);
}

/** Handles the command execution logic based on the provided command. */
public void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
Command.setAuthenticationMode(getAuth(line), userName);

if (CommandActions.LIST.equals(command)) {
handleListCommand();
return;
}

metalake = name.getMetalakeName();

if (!executeCommand()) {
System.err.println(ErrorMessages.UNSUPPORTED_COMMAND);
Main.exit(-1);
}
}

/**
* Executes the specific command based on the command type.
*
* @return true if the command is supported, false otherwise
*/
private boolean executeCommand() {
switch (command) {
case CommandActions.DETAILS:
handleDetailsCommand();
return true;

case CommandActions.CREATE:
handleCreateCommand();
return true;

case CommandActions.DELETE:
handleDeleteCommand();
return true;

case CommandActions.SET:
handleSetCommand();
return true;

case CommandActions.REMOVE:
handleRemoveCommand();
return true;

case CommandActions.PROPERTIES:
handlePropertiesCommand();
return true;

case CommandActions.UPDATE:
handleUpdateCommand();
return true;

default:
return false;
}
}

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

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
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();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine.newCreateMetalake(url, ignore, 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();
}

/** 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)
.validate()
.handle();
}

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

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

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newMetalakeEnable(url, ignore, metalake, enableAllCatalogs)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newMetalakeDisable(url, ignore, metalake).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateMetalakeComment(url, ignore, 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();
}
}
}
Loading