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 1 commit
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
106 changes: 106 additions & 0 deletions clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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;

public class CommandHandler {

public static final String DEFAULT_URL = "http://localhost:8090";

private String urlEnv;
private boolean urlSet = false;
private String authEnv;
private boolean authSet = false;

/**
* Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitio config file.
*
* @return The Gravitinno URL, or null if not found.
*/
public String getUrl(CommandLine line) {
GravitinoConfig config = new GravitinoConfig(null);

// If specified on the command line use that
if (line.hasOption(GravitinoOptions.URL)) {
return line.getOptionValue(GravitinoOptions.URL);
}

// Cache the Gravitino URL environment variable
if (urlEnv == null && !urlSet) {
urlEnv = System.getenv("GRAVITINO_URL");
urlSet = true;
}

// If set return the Gravitino URL environment variable
if (urlEnv != null) {
return urlEnv;
}

// Check if the Gravitino URL is specified in the configuration file
if (config.fileExists()) {
config.read();
String configURL = config.getGravitinoURL();
if (configURL != null) {
return configURL;
}
}

// Return the default localhost URL
return DEFAULT_URL;
}

/**
* Retrieves the Gravitinno authentication from the command line options or the GRAVITINO_AUTH
* environment variable or the Gravitio config file.
*
* @return The Gravitinno authentication, or null if not found.
*/
public String getAuth(CommandLine line) {
// If specified on the command line use that
if (line.hasOption(GravitinoOptions.SIMPLE)) {
return GravitinoOptions.SIMPLE;
}

// Cache the Gravitino authentication type environment variable
if (authEnv == null && !authSet) {
authEnv = System.getenv("GRAVITINO_AUTH");
authSet = true;
}

// If set return the Gravitino authentication type environment variable
if (authEnv != null) {
return authEnv;
}

// Check if the authentication type is specified in the configuration file
GravitinoConfig config = new GravitinoConfig(null);
if (config.fileExists()) {
config.read();
String configAuthType = config.getGravitinoAuthType();
if (configAuthType != null) {
return configAuthType;
}
}

return null;
}
}
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)) {
handleCatalogCommand();
} else if (entity.equals(CommandEntities.METALAKE)) {
handleMetalakeCommand();
new MetalakeCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.TOPIC)) {
handleTopicCommand();
} 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 Catalogs based on command type and the command line options.
*/
Expand Down
Loading
Loading